• EUR€
  • £GBP
  • $USD

Arduino Project 5: Color RGB LED

DFRobot Apr 14 2017 3673

Related Product: Beginner Kit for Arduino


The Arduino Starter kit with 15 Arduino tutorials, lesson 5: Color RGB LED


Let’s start with a new component: an RBG LED. This component combines red, blue, and green LEDs and can display various colors by adjusting the different values of each light. A computer monitor uses many RBG LEDs to display an image. We will learn how to create different colors with RGB LED randomly in this lesson.





Before building the circuit, try to identify whether your RGB LED is a common cathode or a common anode. If you don’t know how to do it, skip to the last part of this lesson. In this Arduino project, we assume that you are using a common cathode RBG LED.


Arduino Code

Sample code 5-1:

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
void loop(){
//R:0-255 G:0-255 B:0-255
void colorRGB(int red, int green, int blue){

You should see the RGB LED blinking with random colors after uploading this code.



First, we will configure the 3 LEDs contained within the RGB LED to 3 PWM pins so we can adjust them to different colors by declaring 3 pins as an OUTPUT .
The main part of this program is to create a new command: colorRGB() which has 3 parameters to assign a value to red, green and blue light between the values of 0 and 255.
This way, when we want to configure a color, we can simply assign values to this command instead of repeating the analogWrite() command constantly.
Here we will introduce constrain() and random() . Do try to look them up with websites we mentioned in the last homework first and see if you can understand them.
The format of the constrain function is as follows:

The constrain() function requires three parameters: x, a and b.
"x" is a constraint number here, a is the minimum, and b is the maximum.
If the value is less than a , it will return to a . If it is greater than b , it will return to b .
Red, green and blue are our constrained parameters. They are constrained between 0 and 255 (which falls into the range of PWM values). Values are generated at random using the random() function.
The format of random() is as below:

The first variable of this function is the minimum value and the second is the maximum. So we configure as "random(0,255)" in this program.




The RGB LED has four leads. If you are using a common cathode RGB LED, there is one lead going to the positive connection of each of the single LEDs and a single lead that is connected to all three negative sides of the LEDs. That’s why it is called common cathode. There is no difference in appearance between common cathode and common anode RGB LEDs, however, you do need to pay attention when assigning color values. For example, for the common cathode RGB red is B-0 .
For the common anode RGB LED, red is "R-255, G-0 B-0". For the common anode RGB LED, red is is R-0, G-255, B-255". How can we adjust the RGB LED to change to different colors?


By assigning different values of brightness levels to 3 primary colors using the function analogWrite(value) , you can configure any color you like!


You can configure 255x255x255 (16777216) kinds of colors by assigning different PWM values on these 3 LEDs!


The difference between common anode and common cathode RBG LEDs

What is the difference between common anode and common cathode RGB LEDs in application? According to the figure below, there is no difference between common anode and common cathode in terms of their appearance. However, there are two key differences in their application:
(1) Different connections: for the common anode, the common port should be connected to 5V but bot GND, otherwise the LED fails to be lit.
(2) Colour matching: the common anode RGB LED is totally different than the common cathode RGB LED. The common anode RGB LED decodes in the opposite way: “R-0, G-255 and B-255”.



1. Based on the program above, create a color changing rainbow sequence. Try to configure various colors by playing with different values.
TIP: You only need to change variables of “colorRGB()”.
2. Take your rainbow se uence LED and make each color fade so that the transition between each color is smoother.


How to load libraries

Download a library from the internet and unzip it to the libraries directory inside the Arduino IDE directory on your computer.
Beware that inside the folder will be a .cpp file, a .h file and often a keywords.txt file. Be sure that these are within the same directory otherwise the Arduino IDE will not recognize the library.


Then open the program in the example. Check whether you need to change the pins in the program if necessary.


Related category: arduino kits > education kits

Last Arduino Tutorial 4: Breathing LED
Next Arduino Tutorial 6: Alarm