Skip to content

PWM duty cycle calculation broken? #5025

Closed

Description

@andreaskoelsch

What happened?

I built a lamp with a single led strip controlled via PWM. I use a brightness gamma correction factor of 2,8. When I set the brightness to any value <56, the led strip completely turns off.
If I understand correctly, this would mean (56/255)^2.8 ~= 1.4% duty cycle.

I also flashed the lamp with ESPHome which can debug-log the actual duty cycle and I see that the led strip already turns on with a very dim light at a duty cycle of 2/16363 ~= 0.01%.

Same lamp and wiring, of course.

To Reproduce Bug

Wire up an LED strip, set gamma brightness to 2.8, set brightness to a value <56. Watch the LED strip turn off.

Expected Behavior

Have the led strip light up at values down to ~10/255, not stop at 55/255, given a gamma factor of 2.8.

Install Method

Binary from WLED.me

What version of WLED?

v0.15.1 build 2507300

Which microcontroller/board are you seeing the problem on?

ESP32-C3

Relevant log/trace output

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. DedeHai commented on Oct 25, 2025

    @DedeHai
    Collaborator

    Set lowest frequency, enable dithering or use better gate driver

  2. andreaskoelsch commented on Oct 25, 2025

    @andreaskoelsch
    Author

    Thanks, that improved it a little bit. I can now go down to 28/255. 0-27 is no light.
    However, I noticed, if I change the "color" channel of my single white light to for example 50:50 black/white, it is possible to dim the light further. I feel like in a single light scenario, there should not be a color selection that influences brightness, but only a brightness selection

  3. softhack007 commented on Nov 10, 2025

    @softhack007
    Member

    @andreaskoelsch do you have "Use Gamma correction for brightness (not recommended)" enabled in led settings ?
    If yes it would explain why you see a sharp drop at lower values - gamma for brightness is calculated in 8bit, instead of ~12bit used for PWM duty cycle. In 8bit, gamma=2.8 is really strong - values below 40 are all mapped to 0.

    • reducing gamma will give you better brightness control for low brightness colours (try down to 2.3)
    • not using "gamma for brightness" will prevent 8bit "killing" of low values
  4. added
    workaround
    on Nov 10, 2025
  5. andreaskoelsch commented on Nov 17, 2025

    @andreaskoelsch
    Author

    Ohh, I didn't know that. For a better WAF, I'd like to resemble the ESPHome behavior. Is there a way to calculate the pwm duty cycle with the full resolution?

  6. DedeHai commented on Nov 18, 2025

    @DedeHai
    Collaborator

    Ohh, I didn't know that. For a better WAF, I'd like to resemble the ESPHome behavior. Is there a way to calculate the pwm duty cycle with the full resolution?

    you'd need to check the code, the calculation is complex.

  7. softhack007 commented on Nov 18, 2025

    @softhack007
    Member

    @andreaskoelsch you need to study the implementation of these two functions

    BusPwm::BusPwm(const BusConfig &bc)

    void BusPwm::show() {

    The code calculates PWM parameters based on what you set in LED settings, and adds dithering in case you also select "off refresh".
    Beware that the calculation is not straightforward, and i doubt that its comparable to a static duty cycle assignment.

    Edit 1:
    You should also consider the frequency limits of your MOSFET driver - some of these cannot cope with very high frequency PWM signals (=short duty cycles) - reducing the PWM frequency might actually improve the behaviour at low brightness.

    Edit 2:
    A logic analyzer might help to understand the PWM signal at PIN level. These things are available for cheap (~10€), and might be better to invest the small money, instead of tricking the source code into showing the duty cycle at software level.

  8. andreaskoelsch commented on Nov 27, 2025

    @andreaskoelsch
    Author

    thanks for your inputs. I think this will get too complicated for a simple monochrome light for me, so I'll stick with ESPHome for now even though it's not really ideal for travelling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugworkaround

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions