In this tutorial we are going to learn how to use an RGB LED with the Arduino. A few things to note about the RGB LED.
- An RGB LED is made up of three LEDs - Red, Green, and Blue, hence the name.
- They are housed in a single package.
- There are two broad types, a common cathode and a common anode.
- Rather than having 6 leads (2 for each LED) it has only 4, one for each colour, and a common cathode or anode.
- The symbol for the common cathode RGB is given below. Its similar to a standard LED symbol only all LEDs are joined at a single point.
Common Cathode Symbol
This tutorial requires the following parts:
- Arduino Microcontroller
- A great open source microcontroller platform
- 5mm RGB LED
- Common cathode preferably
- Most LEDs are designed to work with a voltage between 1.5V and 3V.
- Consult your datasheet to find out which pins are Red, Green and Blue.
- 270 Ohm Resistors (red-purple-brown)
- Microcontrollers such as the Arduino operate on 5V. A current limiting resistor for each colour is needed.
- 270 Ohm resistors limit the current to ~20mA which is well within most LED and microcontroller ratings. Please consult your datasheet for maximum ratings.
- Larger resistors can be used but they will produce dimmer lights.
- Jumper Wires
- Male to male jumper cables to connect everything up
- To make up your circuit
In much the same way that an artist mixes colours in a palette, by adjusting the brightness of each of the RGB colours you can create any colour you like. One way to do this is to change the current-limiting resistors connected to each RGB LED. This will adjust the brightness of each colour by increasing or decreasing the power it receives.
However, a simpler way to do it is to use the Arduino 'analogWrite' software function. This is used with Arduino board pins that have the '~' symbol beside them. It uses PWM to control the brightness of each colour by adjusting the power output to each pin. PWM is explained in more detail here: http://arduino.cc/en/Tutorial/PWM.
Wire up the circuit as illustrated in the diagram. The longest pin of the RGB LED will be the common cathode. This pin should be connected to ground. By using your datasheet you should be able to determine which of the other pins are Red, Green and Blue. Usually, they go in sequence but make sure to confirm this. Connect each of them to the board via the current-limiting resistors as follows:
- Red to Pin 11
- Green to Pin 10
- Blue to Pin 9
- Common Cathode to the Gnd pin
Code for Arduino RGB Example
The following is some example code that will work with a Common Cathode RGB LED.
RGB LED Arduino Example
/* Define the PWM pins used with each colour */
const int RPin = 11;
const int GPin = 10;
const int BPin = 9;
/* This code performs an initial setup. It defines the PWM pins as being output only */
/* This code is the body of the example and will loop continuously. */
setColour(255, 0, 0); // Red
setColour(0, 255, 0); // Green
setColour(0, 0, 255); // Blue
setColour(255, 255, 0); // Yellow
setColour(80, 0, 80); // Purple
/*This code calls the analogWrite function for each pin. It uses the red, green and blue values passed to it to define the pin value. */
void setColour(int red, int green, int blue)
How to change the colour?
We can control the brightness of each of the red, green and blue parts of the LED separately, making it possible to mix any colour we like; except black which is not a colour. A Red, Green, or Blue LED receives full power if the respective pin is set to 255 using the analogWrite() function as in the example above. It will be turned off if the respective pin is set to 0. Setting a pin to 127 provides half power. If we set the brightness of all three LEDs to be 255, the overall colour of the light will be white. If we turn off the blue LED by setting it to 0, so that only the red and green LEDs are still at 255, we get the colour yellow. Some interesting combinations are given in the table below.
|Red Value||Green Value||Blue Value||Resultant Colour|
A full list of colour codes can be found here: http://cloford.com/resources/colours/500col.htm