Driving RGB led

14 Apr 2011

Hi, I'm coming over from Arduino.

I have been driving two RGB leds on the Arduino via the 6 PWM pins and I want to replicate that now from an mbed but unsure whether the Mbed PWM is sufficient to drive these without uprating the output in some way. I want complete control to flash the LED's at a frequency of 1-63hz and do brightness and smooth colour sweeps.

Any advice appreciated on the best way to achieve this, whether it be with PWM or some other method.

Accept I probably need a resistor on the red leg's

Red - Drive voltage 1.95-2.5v Luminous flux 500-1200mcd Green -Drive voltage 3.3-3.8v Luminous flux 1200-3000mcd Blue - Drive voltage 3.3-3.8v Luminous flux 1000-2500mcd (forward current 20mA on all)

Many thanks for your help


14 Apr 2011

Just noticed another thing, as I want to flash the LED's at different times, does the following quote still apply so I cannot independently change the periods of each PWM output e,g. Red, Green or Blue on each LED?

"On the mbed LPC2368 and LPC1768, the PwmOut hardware is limited to share the period value between all outputs. Therefore, if you change the period of one output, you change them all. The pulsewidth can be set independently for each output."

If thats the case, I may need to use something other than the PWM outputs to drive these LED's so any advice appreciated.

14 Apr 2011

You can still flash the LEDs at different rates by using Ticker.

#include "mbed.h"

DigitalOut LEDR(LED1);  //DigitalOuts for LEDs
DigitalOut LEDG(LED2);
DigitalOut LEDB(LED3);

Ticker Tick1, Tick2, Tick3;  // A Ticker for each LED

void ticFunc1() {  // A corresponding function for each Ticker
    LEDR=!LEDR;  // When the Ticker calls this function, it will toggle the LED value
void ticFunc2() {
void ticFunc3() {

int main (void) {

    Tick1.attach(&ticFunc1, 1.0);  // Here we attach each ticFunc1 to a Tick1, ticFunc is called every 1.0 second
    Tick2.attach(&ticFunc2, 0.5);  // Here we attach each ticFunc2 to a Tick2, ticFunc is called every 0.5 seconds
    Tick3.attach(&ticFunc3, 1.5);  // Here we attach each ticFunc3 to a Tick3, ticFunc is called every 1.5 seconds

    while (1) { // While loop is needed in order to keep the program running. Tickers alone aren't enough to keep the program running
        ;//Do nothing


In this example I only used DigitalOut (instead of PWM), but with a little more effort, you can use PWM. This will allow you to control the intensity of the led (via the PWM duty cycle) and the flashing/updating of LEDs intensity via the Ticker period.. If you haven't looked into using a Ticker yet, it's pretty simple. A ticker will call a specific function periodically until you re-attach the ticker or attach it to NULL to stop. See: Ticker

I hope that helps!

14 Apr 2011

Thanks. My main concern is whether the actual output of the PWM will drive the LEDs above into their upper brightness limits or whether theres a better way using i2c or something

15 Apr 2011

I2C/SPI driver would be a good idea. Or at least a transistor buffer for each led. I wouldn't count on the mbed to sink/source 120ma in total. 20 ma for a single led is ok. But not 6 of them..

15 Apr 2011

Craig, changing the pulsewidth is how PWM work. All your PWM should at the same frequency (ie same period). You change the pulsewidth to change the intensity of the LED.

19 Apr 2011

_|___________________ 0%
 |                   |
_|_ _ _ _ _ _ _ _ _ _ 100%
 |                   |
 |       |
_|_ _ _ _|___________ 40%
 |       |           |
         D           T

I couldn't resist. Perfect font for it ;) As you see, the period time stays the same, the duty cycle changes, this gives you different intensities. Setting the period above 20ms or something will make the PWM visible to the eye and anoying to look at. You can set it to 1us as well, but below 10ms you can't realy see the difference with the naked eye.

However, you seem to want three different period times for some other reason then intensity? Can you explain?

Do you want the LED's to flicker at different frequencies? In that case you might want to use Igor's example.

PWM cannot drive the LED's above 100%, so you need not be concerned.