-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Demo of PWM 100% problem on ESP32 (for discussion) #11786
Conversation
@arendst please put on hold. We are discussing this issue on Discord and I would favor a skipping 1 at mid range (512) rather than at end of range |
I've recently added an AnalogWrite library that will resolve the missing full-on PWM issue. For any bit width, the missing step is included. For example, 8-bit resolution has 257 steps (zero-based) 0 is fully off, 1-255 is the PWM duty cycle range and 256 is fully on (high).
|
@Dlloydev This is already how the ESP32 analogWrite(), based on lecdWrite(), is behaving whereas the ESP8266 has 100% duty cycle at 255. |
Actually, I agree that 255 should be 100% full on. I'm new with the ESP32, but it seems that the hardware will support this as I briefly looked at their technical reference manual. |
@barbudor discovered this issue on new Denky D4 board which as a Common Cathode RGB Led (so that is why it's PWM inverted). The fact is with common Anode RGB led, setting PWM to 255 (or 1023) even if it's not really full illuminated it could be 99.6% or 99.9% so I can imagine no human eye can see the difference (so the issue) The deal is that As @barbudor state the easier fix is that if we are in PWM inverted mode and PWM value is the latest of the range, add one or set it to 1024 on ESP32. But I'll try to open a issue on ESP32 core team, worth trying to fix at the source. |
As you pointed, tasmota is not using Arduino for analogWrite on ESP32 but a compatibility layer. |
TL;DR;
Best place to adapt is probably in libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h DetailsESP8266The esp8266/Arduino code is referring to the Arduino official doc for
The code defines the uint32_t high = (analogPeriod * val) / analogScale;
uint32_t low = analogPeriod - high; Where
ESP32On the ESP32 the hardware PWM is based on a
(ESP32 Technical Reference Manual section 14.2.2 and 14.2.3) The ESP32 immplementation is in https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c Initialisation
analogWrite() => ledcWrite()
|
Description:
This is to demo and track a problem with ESP32's PWM not able to reach 100% duty cycle
The most annoying problem is not able to reach 0% on PWM_i inverted outputs which prevent powering off the output.
How to see the problem :
Looking with a scope the GPIO signal shows that while we would expect the signal to be a continuous HI to reach 0% duty cycle for a PWM_i, a 1us pulse to LOW remains every cycle (1/1023th) but that is enough to slighly lite the LED.
The sample patch I provide force the pwm value to 1024 if the calculated value is 1023 which is enough to do the trick.
While this seems to fix the problem for ligthts (using color or channel), the same problem can be seen everywhere analogWrite() is used : PWM command, LedPWM, ....
I'm not sure to understand all the intrinsic around PWMrange and what impact modifying the whole PWMrange logic would have on Tuya dimmers for example
I can't tell this is a bug of the ESP32 HAL has I haven't found a definite description of what the ESP32 LedControl module should do. But this is clearly for now a difference with the ESP8266 that do not show this behavior.
Checklist:
NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass