In this project, I will discuss what an RGB LED is, how to interface an RGB LED with Arduino and finally, how to drive an RGB LED using Arduino UNO.
When you begin your adventure with Arduino, the first Arduino program you will write or start your Arduino experience with is the Blink program. In this program, you will Blink an LED on and off at a certain delay.
So, from the first Arduino program itself, you will begin working around LEDs. Coming to an LED, it is short for Light Emitting Diode, which is a semiconductor device, that emits light when current passes through it.
There are several types of LEDs available today and RGB LED is one such fascinating variant of LED.
What is an RGB LED?
RGB LED is a type LED which emit multiple colors i.e. Red, Green and Blue to be specific. Hence, it is called RGB LED (RGB stands for Red, Green and Blue). Appearance wise, an RGB LED looks very similar to a regular LED except that an RGB LED has three LEDs, each for Red, Green and Blue lights and all these are housed in a single package.
Since an RGB LED consists of three individual LEDs in a single package, the number of leads is different than that of a regular LED, which has two leads (one for cathode and the other for anode).
The through-hole variant of an RGB LED has 4 leads: one lead for each individual color (Red, Green and Blue) and the fourth one is the common lead (which can be either a cathode or anode).
Generally, the common cathode variant is found more frequently than the common anode variant. In this project, I will use a Common Cathode type RGB LED. I will show you how to drive both the variants.
Related Post: RGB LED Bulb
How to Drive an RGB LED?
Common Anode type RGB LED
The following image shows the internal layout of a Common Anode type RGB LED and also its driving circuit.
Common Cathode type RGB LED
Coming to a Common Cathode type RGB LED, you might have guessed the internal layout and the driving circuit by now. The following image shows a Common Cathode RGB LED along with its driving circuit.
SMD RGB LED
As mentioned earlier, a through-hole RGB LED has 4 leads. With the increasing popularity of SMD components, even RGB LEDs are being manufactured. I happen to have a SMD type 3W RGB LED and I felt that it is important to mention a little bit about that.
As you can see from the above image, the SMD type RGB LED has 6 leads, in contrast to only 4 leads in its through-hole counterpart.
An SMD RGB LED also contain three individual LEDs, each for Red, Green and Blue lights but each internal LED has its dedicated anode and cathode leads. Hence, the SMD variant has 6 leads.
RGB LED Colors
Using an RGB LED, you can ideally generate any color by controlling the brightness of the individual Red, Green and Blue LEDs. The following image shows a few possible colors of light that can be generated using an RGB LED.
- Red – Only Red is LED ON
- Green – Only Green is LED ON
- Blue – Only Blue LED is ON
- Yellow – Both Red and Green LEDs are ON
- Cyan – Both Blue and Green LEDs are ON
- Magenta – Both Red and Blue LEDs are ON
- White – All the three Red, Green and Blue LEDs are ON
How to Generate Colors using RGB LED?
As I said before, you can generate a wide range of colors using an RGB LED just by varying the brightness of the individual R, G and B LEDs.
You might already have performed an experiment with dimming an LED using Arduino, where the brightness of LED is controlled either directly using the program or by interfacing a potentiometer.
The technique used to control the brightness of an LED is called Pulse Width Modulation or simply PWM.
PWM is a technique where the amount of power delivered to a device can be controlled accurately and efficiently. PWM technique can be used to control the brightness of an LED, the speed of a motor or the direction of a servo motor.
There are two important factors to consider when dealing with PWM Technique: Duty Cycle and Frequency.
Duty cycle indicates the duration for which the pulse is HIGH over it period. It is measured in percentage and it indicates the voltage between OFF and ON levels (usually 0V and 5V).
Frequency of the pulse or the PWM Signal plays an important role as well and it must be sufficiently high than what the device feels its affect.
PWM in Arduino
Arduino UNO has 6 Pins that can be used to generate PWM Signals: 3, 5, 6, 9, 10 and 11. In order to generate PWM signal of a specific duty cycle, you need to use the analogWrite(pin, value); function.
Here, the value varies from 0 – 255 and corresponds to 0 – 100% Duty cycle.
Interfacing RGB LED with Arduino
Now, let me take you through the steps involved in interfacing an RGB LED with Arduino UNO.
First, an important point is that, since we will be using the PWM technique to control the brightness of the LEDs (Red, Green and Blue separately), make sure that you connect the Red, Green and Blue Anodes of the RGB LED to three PWM supported pins of Arduino (assuming you have a common cathode RGB LED).
NOTE: The RGB LED which I have used in this project is a Clear LED and I suggest you to get a diffused LED.
Circuit Diagram of Arduino RGB LED Interface
The following image shows the circuit connections of Arduino RGB LED Interface. A Common Cathode type RGB LED is used here.
- Arduino UNO
- RGB LED
- 1KΩ Resistor x 3
- Mini Breadboard
- Connecting Wires
PWM Pins 6, 5 and 3 are used in the project and they are connected to Red, green and Blue anode terminals of the RGB LED through individual current limiting resistors (1KΩ resistor are used but 330Ω will be fine).
The common terminal i.e. the cathode is connected to the GND pin of Arduino.
The code for interfacing RGB LEDs with Arduino is given below.
|const int redPin = 6;|
|const int greenPin = 5;|
|const int bluePin = 3;|
|primaryColors(1,0,0); // Red|
|primaryColors(0,1,0); // Green|
|primaryColors(0,0,1); // Blue|
|primaryColors(1,1,0); // Yellow|
|primaryColors(1,0,1); // Magenta|
|primaryColors(0,1,1); // Cyan|
|primaryColors(1,1,1); // White|
|void primaryColors(int redValue, int greenValue, int blueValue)|
|for (x = 0; x < 768; x++)|
|if (x <= 255)|
|redBrightness = 255 – x;|
|greenBrightness = x;|
|blueBrightness = 0;|
|else if (x <= 511)|
|redBrightness = 0;|
|greenBrightness = 255 – (x – 256);|
|blueBrightness = (x – 256);|
|redBrightness = (x – 512);|
|greenBrightness = 0;|
|blueBrightness = 255 – (x – 512);|
After making the connections as per the circuit diagram, copy the code and upload it to Arduino UNO. Initially, the RGB LED display individual colors Red, Green and Blue then followed by combination colors Yellow, Magenta and Cyan. White is also displayed.
Following this, a continuous display of the color spectrum possible with RGB Primary colors is implemented using PWM technique.