“It’s hard to be a bright light in a dim world.” 
 – Gary Starta

A common 5mm LED isn’t the best in every situation, often a bit more power is required. That’s why I’ve connected a 3W power RGB LED to an STM32F0-Discovery board.



The first problem when connecting the power LEDs is provide the right amount of current. Ad-hoc chips are available but I was focused on make all the things work with material I already have, so I chosen a less professional approach: power resistors.

My RGB LED is rated for 350mA for each color and I am assuming to provide 12Vdc. The forwarding voltage is different from color to color.

  • RED has a Vf of 2.2V
  • GREEN and BLUE have a Vf of 3.4V


The common series resistor law is:

R = (V – Vf) / I

In my case I have:

  • For RED: R = (V – Vf) / I = (12V – 2.2V) / 0.35A = 28Ω
  • For BLUE and GREEN: R = (V – Vf) / I = (12V – 3.4V) / 0.35A = 25Ω

The powers to dissipate are instead:

  • For RED: P = R * I * I = 28Ω * 0.35A * 0.35A = 3.5W
  • For GREEN and BLUE: P = R * I * I = 25Ω * 0.35A * 0.35A = 3W

I strongly suggest using HIGHER wattage resistors – due to heating – easily available at RadioShack or other distributors.

However, I am using 47Ω 5W resistors (the only available at my home) for all channels. The current flow with this value is (assuming an average Vf of 3V):

I = (V – Vf) / R = (12V – 3V) / 47Ω = 0.2A

and the power to dissipate:

P = R * I * I = 47Ω * 0.2A * 0.2A = 1.9W

Obviously in this way I’m not using all the LED’s power but about 65%. The calculations are correct and verified with a multimeter, exactly 0.62A are absorbed during emission of white color (each RGB channel near 100% duty cycle).

The LED itself must be cooled, I have mounted it on a non-common heatsink… Have you ever dismounted an XBOX 360? Well, the joystick’s receiver antenna is a really nice thing to make a heatsink 😉 – see image below –

To drive each R-G-B channel I have used two transistors and two resistor, in Darlington configuration


R3, R6, R9 are power resistors (read above) of 5W

The circuitry described until now is perfectly suitable for a broad range of microcontrollers (from low cost Arduino boards to high performace Raspberry’s).

I’ve realized all the circuitry needed on a prototype board  and I’ve mounted it on a STM32F0-Discovery board, I have also added an LM7805 power regulator with 1000uF capacitor and a NRF24L01+ module for wireless control.


Using the excellent STM32CubeMX software provided by ST, I planned each connection in this way



  • PA0 is connected to the USER button on the STM32F0 board
  • PA5 PA6 PA7 are used by NRF24L01+ module (SPI protocol)
  • PB10 PB11 PB12 are used by NRF24L01+ module (control signals)
  • PC8 PC9 are connected to the LEDs on STM32F0 board
  • PA8 PA9 PA10 PA11 are PWM outputs of Timer1 – we will use only the first 3 signals to drive the RGB LED
  • PB6 PB7 are connected the USART1 module – used for debugging

Let’s take a look at the code.

Here we initialize GPIO PA8-PA9-PA10 and the timer. This part is a modified version of “PWM generation” sample code available in the Standard Peripheral Libraries

 Now the microcontroller is ready to generate the needed 3 PWM signals but is configured to produce a 0% duty cycle, so the LED seems powered off. The Standard Peripheral Library doesn’t provide a fast way to change PWM duty so I have used a trick. I wrote a function that directly write on the CCR registers of Timer1, so that the PWM duty cycle is instantly changed

 After initializing the system with PWM_Config, is possible to generate every color just invoking updateRGB with correct parameters.

The effect is exactly what I expect, but not what I like. In commercial RGB application there is a very nice fading effect from a color to another and I want to achieve the same result. Another function in needed

 With this function is possible to change color in a progressive way, and within a given time, leading to an eye-candy effect.

This is the effect when powered by 5V USB source

This is instead the effect when powered with 12V

The complete source code is available HERE. Please note that nrf24 library was not written by me but only modified (refer to library’s header) and the zip contains also the Standard Peripheral Library from ST but with a modified folder structure.

If you liked this article, please share 😀

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.