Using RGB LEDs


RGB LEDs are, as the name implies, LEDs that are capable of producing any color from a combination of red, green and blue.

They're actually made up of three individual LEDs packaged together in the same plastic. However, they don't have 6 pins - only 4.
That's because instead of having anode (+) and cathode (-) for each LED, one of these will be shared between the 3 LEDs.

Therefore, two variants exist:

  • Common cathode (shared "-")
  • Common anode (shared "+")


Note: many different pinouts exist for RGB LEDs, each manufacturer doing it their own way. The longest pin is always the common.
The recommendation is usually to test the pins of your RGB LEDs on a breadboard to determine whether they're common-anode or common-cathode, as well as the pinout.

Example 1 - Common-cathode

Let's start with a common-cathode RGB LED - shared "-".
Start by connecting the longest pin to the ground (GND) and connect each channel to one of the PWM output pins of your ioNode.
Note: Don't forget to use resistors between the LED itself and your PWM outputs (one for each channel)! 1k is a common value for 3.3V ~ 5V circuits. Connecting it directly will fry the LED.
Remember the following diagram, and keep in mind that each channel in an RGB LED is actually an independent LED:


Now, we can control the intensity of each channel (red, green, blue) using the pwm library as presented in PWM (Pulse Width Modulation).
However, the rgb_led library provided with the SDK allows controlling the color of an RGB LED with just a single method call:

void rgb_led(uint8_t r_pin, uint8_t g_pin, uint8_t b_pin, uint8_t r, uint8_t g, uint8_t b).

  • r_pin -> PWM output pin for red channel
  • g_pin -> PWM output pin for green channel
  • b_pin -> PWM output pin for blue channel
  • r -> red channel value
  • g -> green channel value
  • b -> blue channel value

Let's add the library to the list of dependencies in our dfe.conf file:

# Firmware Element Configuration
# ...
  - rgb_led
Now, we can set the colour of our LED.
Assuming we've connected our three channels as such:
  • PWM0 -> Red
  • PWM2 -> Green
  • PWM3 -> Blue

(Note how we didn't use PWM1 - as explained in the PWM tutorial, PWM1 is connected to the onboard activity LED on the ioNode.)

#include <rgb_led/rgb_led.h>

void main()
    // Set RGB LED color to Orange (HTML #ff7f00)
    rgb_led(0, 2, 3, 0xff, 0x7f, 0x00);

    // Loop forever

Example 2 - Common-anode

Using a common-anode RGB LED is a little different, but just as simple.
Instead of providing power from the pin (sourcing - what we did in the first example), we'll be allowing power to flow into the pin (sinking).

This time, connect the common "+" (longest pin) to a voltage source, and again, connect each channel to a PWM output (using a resistor in between!).

Now, when our PWM output is high (logic "1"), this blocks the current from flowing and the LED is off. When our PWM output is low (logic "0"), current is allowed to flow and the LED lights up.
So basically, it's the same as before, but inverted.

Let's modify the code from example 1 to use a common-anode RGB LED:

#include <rgb_led/rgb_led.h>

void main()
    // Set RGB LED color to Orange (HTML #ff7f00)
    rgb_led(0, 2, 3, 0xff - 0xff, 0xff - 0x7f, 0xff - 0x00);

    // Loop forever

As you can see, to invert the outputs we subtract the desired value for each channel from 0xff (255 - the maximum).

rgb_large.jpg View (13.5 KB) eresse, 09/13/2017 09:11 AM

led-hookup.png View (4.8 KB) eresse, 09/13/2017 09:44 AM