9 years, 9 months ago.

How do I switch off a PWM-regulated LED

When I create the PwmOut-object and set it to 1, the LED is still glowing a little bit. It is not completely off. I understood the tutorials, that the LED should be off, when setting the PwmOut to 1.

How can I switch off the LED completely?

my program

#include "mbed.h"

PwmOut r (LED_RED);

int main()
{
    r.period(0.001);


    while (true) {

        r = 1.0f;
        wait(1);
        r = 0.99;
        wait (0.2);

    }
}

Question relating to:

1 Answer

9 years, 9 months ago.

The issue is that the mbed lib sets the pwm period register to be equal to the period, while in fact it should be one smaller. This keeps it very slightly on. I submitted pull request to the mbed github, although for some reason it can't be automatically merged. Still I expect it to be fairly soon in mbed-src lib, and in the next mbed release.

In the meantime you can use .pulsewidth function, and simply set pulsewidth to be larger than the period. Or use http://mbed.org/users/Sissors/code/FastPWM/, which does not suffer from this.

Accepted Answer

you can also set the PWM.write(0) which will set the PWM to gnd.

posted by james McMurtrie 17 Nov 2017