This tutorial explains all you need about RGB LED and Arduino. You can set any color to RGB LED and can transform from one color to another with different speed. This Tutorial includes Header file for RGB control and that makes it very easy to use.
1. Introduction:
A step by step illustrated basic tutorial for Arduino. This tutorial explains how to control RGB LED on Arduino platform. We have included two examples here.
1.1 RGB LED:
When three different LEDs i.e. Red, Green and blue are merged to one single LED that LED is known as RGB LED. So RGB led comprises 3 LEDs in itself. It could be two types
1. Common Cathode – Cathode(-) is common for all three Red, green and Blue LEDs.
2. Common Anode – Anode(+) is common for all three Red, green and Blue LEDs.
In this tutorial we are dealing with common Anode type LED.
Pin diagram of RGB LED –
1.2 RGB LED Arduino:
We have included a header file to make it easy to control RGB LED on Arduino. It is – RGBled.h.
Funtions of RGB LED Headers.
1. Declartion of RGB object.
RGB Object_Name;
You may choose any name for this object, later on in the programme it will be called by the name you declare here.
2. Object_Name.init(9, 10, 11);
This function initialize RGB Object. You need to tell that RGB pins are connected to which Pins of Arduino. Here 9, 10 and 11 are connected to Red, green and blue respectively.
3. Object_Name.fadeToColor(RED, GREEN, 10);
This function transforms to Green from Red at speed of 10(speed is decreased with increase in this number).
4. Object_Name.setColor(RED);
This function sets RGB LED to defined color.
1.2.1 Defined colors in header:
- RED
- ORANGE
- YELLOW
- GREEN
- BLUE
- INDIGO
- VIOLET
- CYAN
- MAGENTA
- WHITE
- BLACK
- PINK
You can call these colors by name in the programme.
This tutorial is made on Original Arduino UNO and Robo India’s R-Board(UNO compatible.)
2. Required Hardware
S.No. | Item | Quantity |
1 | Arduino UNO | 1 |
2 | Breadboard | 1 |
3 | RGB LED | 1 |
4 | Resistor 1k | 3 |
5 | Jumper Male to male | 6 |
3. Building Circuit
Schematic of circuit
Layout of circuit
4. Programming:
Once we are done with circuit part, here is our programme to this circuit. All of the commands are explained in the comment section. Output video is attached at the last of this tutorial, This example will be clear after watching the video.
You may download codes (Arduino Sketch) and RGBled Library from here.
4.1 Installation of RGBled Header file-
Extract the downloaded file from above link.
The extracted folder contains three folders in it as mentioned in the below snapshot.
first two folders are codes of examples. The third folder contains Header file. This folder is to be pasted in Library folder of Arduino, it is generally found of the following location-
mydocuments/Arduino/Libraries/
or similar location for Mac and Linux users.
Once you have pasted this folder to the above mentioned location restart your Arduino Software and you are ready to start with programming.
4.2 Example – 1:
This code has got three function to perform. RGB LED changes its color to three different combination. Each command of the code is explained in comment section.
//Tutorial on RGB LED // Robo India // http://roboindia.com/ // Header file courtesy- www.github.com/jscrane/RGB #include <RGBled.h> // Header file RGB myLED; // defining RGB LED object void setup() { myLED.init(9, 10, 11); // Pin for (Red, Green, Blue) } void loop() { myLED.fadeToColor(RED, GREEN, 10); // Transform to green from Red. 10 is the speed myLED.fadeToColor(GREEN, BLUE, 10);//Transform to Blue from Red. 10 is speed. myLED.fadeToColor(BLUE, RED, 10); // Tranform to Red from Blue. 10 is speed. } // You may try with another set of colors and speed.
4.3. Example -2:
In this example RGB LED is set to different colors, it observes a delay of 1 second between each change. Code is explained in the comment section.
//Tutorial on RGB myLED // Robo India // http://roboindia.com/ // Header file courtesy- www.github.com/jscrane/RGB #include <RGBled.h> // Header file RGB myLED; // defining RGB myLED object void setup() { myLED.init(9, 10, 11); // Pin for (Red, Green, Blue) } void loop() { myLED.setColor(RED); // set.Color function sets the color. delay(1000); // Halt for one second. myLED.setColor(ORANGE); delay(1000); myLED.setColor(YELLOW); delay(1000); myLED.setColor(GREEN); delay(1000); myLED.setColor(BLUE); delay(1000); myLED.setColor(INDIGO); delay(1000); myLED.setColor(VIOLET); delay(1000); myLED.setColor(MAGENTA); delay(1000); myLED.setColor(WHITE); delay(1000); myLED.setColor(PINK); delay(1000); }
5. output:
Here is the output of this tutorial. This video contains both of the practicals we have done above.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com
Can we do this using python and arduino in the same way which you showed us LED Blinking using pyhton and arduino?