Overview
In this lesson, you will learn how to use a RGB (Red Green Blue) LED with an Arduino.
You will use the analogWrite function of Arduino to control the color of the LED.
We mix colors just like you would mix audio with a 'mixing board' or paint on a palette - by adjusting the brightness of each of the three LEDs. The hard way to do this would be to use different value resistors (or variable resistors) as we played with in lesson 2. That's a lot of work! Fortunately for us, the Arduino has an analogWrite function that you can use with pins marked with a ~ to output a variable amount of power to the appropriate LEDs.
Page last edited October 02, 2012
Text editor powered by tinymce.
Parts
Page last edited October 02, 2012
Text editor powered by tinymce.
Breadboard Layout
Each LED inside the package requires its own 270Ω resistor to prevent too much current flowing through it. The three positive leads of the LEDs (one red, one green and one blue) are connected to Arduino output pins using these resistors.
Page last edited October 02, 2012
Text editor powered by tinymce.
Colors
In a way, by using the three LEDs we are playing a trick on the eye. This same idea is used in TVs, where the LCD has red, green and blue color dots next to each other making up each pixel.
We can control the brightness of each of the red, green and blue parts of the LED separately, making it possible to mix any color we like.
Black is not so much a color as an absense of light. So the closest we can come to black with our LED is to turn off all three colors.
Page last edited October 02, 2012
Text editor powered by tinymce.
Arduino Sketch
/*
Adafruit Arduino - Lesson 3. RGB LED
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
The sketch starts by specifying which pins are going to be used for each of the colors:
int redPin = 11; int greenPin = 10; int bluePin = 9;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0);// yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255);// aqua
delay(1000);
}
Page last edited October 02, 2012
Text editor powered by tinymce.
Using Internet Colors
If you have done any Internet programming, you will probably be aware that colors are often represented as a 'hex' number. For example the color red has the number #FF0000. You can find the numbers associated with a particular color using tables like these: https://htmlcolorcodes.com/color-names/
The six digits of the number are actually three pairs of numbers; the first pair being the red component of the color, the next two digits the green part and the final pair the blue part. Red is #FF0000, because its maximum red (FF is 255 in hex) and it has no green or blue part.
It would be pretty useful to be able to dial up one of these color numbers so that it is displayed on the RGB LED.
Let's try and make the color indigo (#4B0082).
setColor(0x4B, 0x0, 0x82); // indigo
Try adding a few colors of your own to the 'loop' function. Don't forget to add a delay after each one.
Page last edited October 02, 2012
Text editor powered by tinymce.
Theory (PWM)
The diagram below shows the signal from one of the PWM pins on the Arduino.
If we specify a value in the analogWrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power.Â
If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing.Â
Page last edited October 02, 2012
Text editor powered by tinymce.
Other Things to Do
- Try putting a ping-pong ball over the LED
- Try changing the delays to speed up or slow down the color changing
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino, 30 Arduino Projects for the Evil Genius and Programming the Raspberry Pi.
Page last edited October 02, 2012
Text editor powered by tinymce.




