Explore two basic examples of controlling RGB LED with Arduino uno to create vibrant lighting effects.
RGB LED is the abbreviation of ‘’Red Green Blue Light Emitting Diode’’. RGB LED is the most amazing type of LED that can create millions of different shades of light colors using red, green, and blue colors.
RGB LED looks just like a regular LED or we can say that an RGB LED has three LEDs (red, green, and blue colored LED) inside a single packaged LED with four legs. We can generate any color by adjusting the brightness of each of the three LEDs of the RGB LED.
If you want to produce a red color, you will set the red LED at the highest intensity along with the green and blue LED with minimal (zero) intensity. Similarly, if you need to generate a white color, you would set all three LEDs with the highest intensity.
Types of RGB LED
There are two types of RGB LED one is a common anode type and the other one is a common cathode type. The RGB LED has four legs, the first one is for red-colored LED, and the second one is for common anode or common cathode. The third one is for green-colored LED and the fourth one is for blue-colored LED.
You can identify the type of RGB LED by using the multimeter in continuity mode or by reading the component data sheet provided by the manufacturing company.
Common Anode type
In the common anode type, RGB LED all three colored LED legs share a (positive) anode connection. You can understand the structure of common anode-type RGB LEDs using the image illustrated below.
Common cathode type
In the common cathode type, RGB LED all three colored LED’s legs share a (negative) cathode connection. You can understand the structure of common cathode-type RGB LED using the image illustrated below.
In this article, I will explain to you how an RGB LED can be operated using the Arduino Uno using two very basic examples for the same circuit connection. At the end of this article, you will be able to play with multiple colors generated by the RGB LED.
Example: RGB LED with Arduino Uno
Required components
- 1 x Arduino Uno board: BUY ON AMAZON
- 1 x breadboard: BUY ON AMAZON
- 1 x RGB LED common anode mode: BUY ON AMAZON
- 3 x 1 K ohm resistors: Y ON AMAZON
- Few (male to male) jumper wires: BUY ON AMAZON
Circuit diagram
Circuit description
As shown in the above circuit diagram, the first terminal of the RGB LED along with the 1 K ohm resistor is connected to the PWM pin 11 of the Arduino board. The second pin (common anode) of the RGB LED is connected to the 5-volt power supply pin of the Arduino board.
The third pin of the RGB LED along with the 1 K ohm resistor is connected to the PWM pin 9 of the Arduino board. The last pin of the RGB LED is also connected with the PWM pin 6 of the Arduino board along with the 1 K ohm resistor. I have preferred this circuit connection for both codes to make things easier for you to understand.
Code 1
This code example will help you to understand how different colors (red, green, blue, and white) can be generated by RGB-LED using Arduino uno.
PWM or pulse width modulation is the technique that is used to control the brightness of the LED, speed, and direction of different types of motors. Arduino Uno has six pins (3, 5, 6, 9, 10, and 11) to generate PWM signals of the specific duty cycle. The duty cycle specifies the duration for which the pulse is HIGH.
It measures in percentage and defines the voltage between 0 and 5 volts. analogWrite(pin, value) function is used to generate the PWM signal of a specific duty cycle where the value varies from 0 to 255 for 0 to 100% duty cycle (for example, 25% duty cycle: analogWrite(64), 100% duty cycle: analogWrite(255)).
According to this code, I have used the PWM pins 11, 9, and 6 for LED outputs. I have used the analogWrite() function to generate output that accepts values between 0 to 255.
In the “common anode” type if the value is 0 means the LED has the highest brightness on the other side the value is 255 means the LED has the lowest brightness (off). As shown in the connection diagram I used this common anode mode to generate output using RGB LED.
However, in “common cathode” type 0 values indicate the lowest brightness and the 255 value indicates the highest brightness.
According to the code in the output red, green, blue, and white-colored will be produced by the RGB LED. You can check the output according to the provided input values in the analogWrite() function.
/*1st code
*How to operate the RGB LED using Arduino uno
*(how red, green, blue, white colors can be generated)
*
*for more detail about this project please visit:https://www.arduinounomagic.com/2019/05/how-to-control-rgb-led-using-arduino-uno.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit:https://www.arduinounomagic.com/
*/
//configure the PWM pins
int redInput=11;
int greenInput=9;
int blueInput=6;
void setup() //define the output pins
{
pinMode(redInput, OUTPUT);
pinMode(greenInput, OUTPUT);
pinMode(blueInput, OUTPUT);
}
void loop() //generates the different color according to the provided values
{
ledColor(0, 255, 255);// red
delay(1000);
ledColor(255, 0, 255); //green
delay(1000);
ledColor(255, 255, 0);// blue
delay(1000);
ledColor(0, 0, 0);//white
delay(1000);
}
void ledColor(int red, int green, int blue)
{
analogWrite(redInput, red);
analogWrite(greenInput, green);
analogWrite(blueInput, blue);
}
Code 2
In this example code, I have tried to produce four different colors by mixing the values of red, green, and blue pins. You can change these values and try to produce a new color. I used the same circuit connection based on common anode mode for this code to generate output.
/*2nd code
*How to operate the RGB LED using Arduino uno
*(how differnt colors can be generated by mixing the values)
*
*for more detail about this project please visit:https://www.arduinounomagic.com/2019/05/how-to-control-rgb-led-using-arduino-uno.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more projects based on Arduino uno please visit:https://www.arduinounomagic.com/
*/
//intialize the PWM pins
int RED_COLOR=11;
int GREEN_COLOR=9;
int BLUE_COLOR=6;
void setup() {
int redInput=11; //connect first pin to pin 11
int greenInput=9;//connect third pin to pin 6
int blueInput=6;//connect fourth pin to pin 9
}
void loop()
{
//generates four different colors
analogWrite(RED_COLOR, 200);
analogWrite(GREEN_COLOR, 50);
analogWrite(BLUE_COLOR, 100);
delay(1000);
analogWrite(RED_COLOR, 50);
analogWrite(GREEN_COLOR, 200);
analogWrite(BLUE_COLOR, 50);
delay(1000);
analogWrite(RED_COLOR, 0);
analogWrite(GREEN_COLOR, 200);
analogWrite(BLUE_COLOR, 250);
delay(1000);
analogWrite(RED_COLOR, 0);
analogWrite(GREEN_COLOR, 25);
analogWrite(BLUE_COLOR, 255);
delay(1000);
}
Output
Check out the output video, this will help you to understand how the circuit is behaving according to the code. I have provided the output of code 1 and after that code 2. Therefore you can identify the difference between both codes easily according to the results.
Important points
- In a common anode type, the second pin of the RGB LED is always connected to the 5-volt power supply pin.
- In a common cathode type of mode, the second pin of the RGB LED is always associated with the GND pin.
- In a common anode type, the analogWrite() function generates the highest brightness for the value 0 and the lowest for the value 255.
- In a common cathode type, the analogWrite() function generates the highest brightness for the value 255
and the lowest brightness for the value 0.
I hope all important points are covered regarding basic RGB functionality.
This was a beginner-level example, Hope you enjoyed the article.
If you like the article, let me know in the comments.
If you have any queries, let me know in the comments, and I will try to give my best.