Instructables
Picture of RGB LED Tutorial (using an Arduino) (RGBL)
IMG_6601-1.JPG

LEDs are great. But with any project there comes a point where flashing is simply not enough. For these cases an RGB (Red, Green, BLue) LED is the answer.

With an RGB LED you'll be able to produce any colour glow your heart desires.

At first using an RGB LED seems quite complex, but it quite quickly becomes clear that its no more difficult than controlling one of their single colour counter parts.

What follows is a quick guide to get you started controlling an RGB LED first with wires then with an Arduino microcontroller.

If you'd like to jump ahead to any part here's your chance.

Step 1 The Parts
Step 2 Testing
Step 3 Arduino Controlled Example Circuit
Step 4 Digital Control of Colour
Step 5 Analog Control of Colour

(Shamless Plug)
Interested in getting a few RGB LEDs to play around with and in the UK? a component bundle can be bought at our online store oomlout.co.uk
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts

Only a few parts are required.

RGB LED (common anode)
  • A common anode RGB LED is nothing more complicated than three one colour LEDs (one red, one green, and one blue) housed in a single package.
  • Rather than having 6 leads (a cathode and anode for each LED) it has only 4 one cathode for each colour, and one common anode. (see the schematic diagram below)
  • A common anode RGB LED is the most popular type. It is most commonly found in either a 5mm bulb size or as a 5mm pirahna form factor.

Current Limiting Resistors (270 ohm) (red-purple-brown)
  • Most LEDs are designed to work with a voltage between 1.5v and 3v. As most microcontrollers (including the Arduino) operate on 5 volts a current limiting resistor is required.
  • Consult your LEDs datasheet for maximum ratings but we like to use 270 ohm resistors. This limits the current to ~20mA, well within most LEDs and microcontroller ratings.

Arduino Microcontroller & Breadboard
  • A great open source microcontroller platform (for more details visit arduino.cc)
dwhogamer1 month ago

I'm using a 5mm rgb led and its not DOING anything. Sorry for bad picture quality.

Picture 38.jpgPicture 37.jpg
dcms42 months ago

if I wanted to have an Arduino control multiple RGB LED's, but have them all be the same color, how many would I be able to have in my project? I'm shooting for like 6-10. What else would I need besides the Arduino, current limiter and RGB LED's? Thanks for the help.

jscrane6 months ago

I've made a little arduino library based on the code below, here: https://github.com/jscrane/RGB

jj.inc9 months ago
So why do common anodes exist, I have yet to find any benefit in them, I have common cathode and instead of just 7 colors, I can vary the power to each LED achieving 767 I think is what it was. I think there are 250+ some steps on the pwm and then 3 leds. The common anode only seems feasible on 7-segments and even then I prefer common cathode.
I also made a post about this topic, explaining how to use serial communication between the Arduino UNO and a C# .NET based program.
http://jonastranberg.blogspot.dk/2013/05/arduino-rgb-control-with-c-net.html
pjr.11 year ago
Below is the code that is OK,


//--- bof RGBL - RGB Digital Preamble
//RGB LED pins
int ledDigitalOne[] = {14, 15, 16};
//the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePin
int ledDigitalTwo[] = {9, 10, 11};
//the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePin
int ledDigitalThree[] = {3, 5, 6};
//the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePin
const boolean ON = LOW;
//Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;
//Define off as HIGH
//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};
//--- eof RGBL - RGB Digital Preamble

void setup(){
for(int i = 0; i < 3; i++) {
pinMode(ledDigitalOne[i], OUTPUT);
//Set the three LED pins as outputs
pinMode(ledDigitalTwo[i], OUTPUT);
//Set the three LED pins as outputs
pinMode(ledDigitalThree[i], OUTPUT);
//Set the three LED pins as outputs
}
}

void loop(){

/* Example - 1 Set a color Set the three LEDs to any predefined color*/
setColor(ledDigitalOne, RED);
//Set the color of LED one
setColor(ledDigitalTwo, GREEN);
//Set the color of LED two
setColor(ledDigitalThree, BLUE);
//Set the color of LED three

/* Exampe - 2 Go through Random Colors Set the LEDs to a random color*/
int rand = random(0, sizeof(COLORS) / 2);
//get a random number within the range of colors
setColor(ledDigitalOne, COLORS[rand]);
//Set the color of led one to a random color
rand = random(0, sizeof(COLORS) / 2);
//Set the color of LED 2 to a random color //
setColor(ledDigitalTwo, COLORS[rand]);
rand = random(0, sizeof(COLORS) / 2);
//Set the color of LED 3 to a random color //
setColor(ledDigitalThree, COLORS[rand]);
delay(1000);
}

/* Sets an led to any color led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin) color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)*/
void setColor(int* led, boolean* color){
for(int i = 0; i < 3; i++) {
digitalWrite(led[i], color[i]);
}
}
/* A version of setColor that allows for using const boolean colors*/
void setColor(int* led, const boolean* color) {
boolean tempColor[] = {color[0], color[1], color[2]};
setColor(led, tempColor);
}
AquaRay2 years ago
Hi folks, Hi oomlot, thanks for sharing.
Does anyone know if this type of color changing led setup will work with a basic stamp 2, if so, any links to information or stamp code.
Thanks much.
AquaRay
sarfata2 years ago
Thanks for the inspiration!

We took this a step further and are adding wifi connectivity and a little iPhone app to let you control this lamp for a smartphone.

It is great for ambient lighting and still hackable if you want to! We will be starting to ship them soon from http://www.tbideas.com

thomas
Nyox4 years ago
The code is badly embedded in the page, I suggest to save  a .pde file and add it into the project page.
the code is a pain to deal with.
Here's _RGBL_AnalogTest, no guarantees on it compiling. Also I suggest downloading Notepad++, makes doing this kind of thing much much easier.

//--- bof RGBL - RGB Digital Preamble//
RGB LED pinsint ledDigitalOne[] = {14, 15, 16};
//the three digital pins of the first digital
LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalTwo[] = {9, 10, 11};
//the three digital pins of the first digital
LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalThree[] = {3, 5, 6};
//the three digital pins of the first digital
LED 14 = redPin, 15 = greenPin, 16 = bluePinconst boolean ON = LOW;
//Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;
//Define off as HIGH
//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};
//--- eof RGBL - RGB Digital Preamble

void setup(){
for(int i = 0; i < 3; i++) {
pinMode(ledDigitalOne[i], OUTPUT);
//Set the three LED pins as outputs
pinMode(ledDigitalTwo[i], OUTPUT);
//Set the three LED pins as outputs
pinMode(ledDigitalThree[i], OUTPUT);
//Set the three LED pins as outputs
}
}

void loop(){

/* Example - 1 Set a color Set the three LEDs to any predefined color*/
setColor(ledDigitalOne, RED);
//Set the color of LED one
setColor(ledDigitalTwo, GREEN);
//Set the color of LED two
setColor(ledDigitalThree, BLUE);
//Set the color of LED three

/* Exampe - 2 Go through Random Colors Set the LEDs to a random color*/
int rand = random(0, sizeof(COLORS) / 2);
//get a random number within the range of colors
setColor(ledDigitalOne, COLORS[rand]);
//Set the color of led one to a random color
rand = random(0, sizeof(COLORS) / 2);
//Set the color of LED 2 to a random color //
setColor(ledDigitalTwo, COLORS[rand]);
rand = random(0, sizeof(COLORS) / 2);
//Set the color of LED 3 to a random color //
setColor(ledDigitalThree, COLORS[rand]);
delay(1000);
}

/* Sets an led to any color led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin) color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)*/
void setColor(int* led, boolean* color){
for(int i = 0; i < 3; i++) {
digitalWrite(led[i], color[i]);
}
}
/* A version of setColor that allows for using const boolean colors*/
void setColor(int* led, const boolean* color) {
boolean tempColor[] = {color[0], color[1], color[2]};
setColor(led, tempColor);
}
Thank you.
scooterboi4 years ago
hey there , im planning on doing this on a larger scale , maybe 20-30 rgb led's for a project , id really like the user to be able to turn a dial  and as the dial is turned the colours change on all of the led's at once , is this possible? well , i know it is , but how do i i go about it , what sort of dial ? i really dont know, in the project i really dont have any space for a computer , hence the dial ,  thanks
A little bit late in reply but just in case anyone else wanted do the same bigclive has a product that's perfect for your application! http://bigclive.com/knob.htm A neat kit and cheaper than using and Arduino
Hi scooterboi,
I'm sure this is possible, and something like an Arduino would be the way to go, at least to test the idea. However a basic Arduino (like the one I'm still learning to play around with) won't have enough sockets for that many individual LEDs in series - if I'm reading your comment right then maybe you could just have a single socket connected to multiple LEDs in parallel (well, four sockets, since the LEDs have four pins). You won't be able to control them individually but perhaps that won't matter to you.
The dial itself could be pretty much any random dial that physically fits - as far as I'm aware they all work as variable resistors, so the Arduino would just figure out how far the dial was turned. No problem.

The only other problem I can think of is power consumption - the Arduino may not put out enough power for that many LEDs, so you might need to plug in a separate power source, and make a power circuit to deal with that too.

Perhaps someone more qualified could be more help. :)

Good luck! Let us know when you've posted your own 'structable for this project!
I may have just missed this in the 'ible, but how is the Arduino able to control the ground (cathode) parts of each LED? Because the LED has a common anode, the cathodes would be connecting to the digital output pins on the board, right? I'm trying to control one RGB LED and the only way I can think to control it would be with a transistor on each color's cathode. Thanks in advance!
oomlout (author)  Radioactive_Legos5 years ago
Hey Radioactive; Controlling a common anode RGB LED It is a little bit counter intuitive but once understood no more difficult than a common cathode one. To control it with an Arduino you simply reverse the controls, this means when your pin is high the LED is off and when your pin is low the LED is on. Hope that was helpful Stuart
cyzen oomlout3 years ago
oh thank you! i thought i could put it away and bought the wrong leds ... but what you say makes really sense. width a little bit of thinking about i should find it out myself- but sometimes i ask the net ... i have to pull on my nose :-)
Starting to make sense... so would the common anode lead go to a digital pin that would always stay on? and then the three cathodes would each go to another digital pin that stays HIGH to turn the LED off, and LOW to turn it on?
oomlout (author)  Radioactive_Legos5 years ago
Hi RadioActive; Pretty much, but the common (cathode) can be connected permanently to the supply voltage. (there is a schematic on step 3.
Oomlout, you have just saved me some money (I had a RGB led with a common anode that I somehow couldn't control). I have rigged it accordingly to your scheme in step 3 and it works. Thanks!
However, my LCD display uses the same power supply (on a breadboard) and it doesn't work when the LED is on (strange signs appear as opposed to the normal text). Should I isolate it somehow?
My mistake! (forgot the resistors on the cahtode)...
Thanks anyway.
ok, cool. thanks! My LED is working its magic now!
peasant543 years ago
Old instructable but posting this nevertheless because I found this instructable useful.

Here's a link to the fixed, properly formatted code for the analog step:

http://pastebin.com/YwMXYH2i

BES1054 years ago
This code works well thanks http://reprap.org/wiki/PWM_Driver_1_1#Upload_Firmware_to_Arduino

In the above link the site has a nice pcb to connct more LEDs to it.

int pwm_a = 9;
int pwm_b = 10;
int pwm_c = 11;
int i;

void setup()
{
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(pwm_c, OUTPUT);

Serial.begin(9600);
}

void loop()
{
//
//fade all channels up and down.
//
Serial.println("Fading all pwm channels up to max.");
for (i=0; i<=255; i++)
{
analogWrite(pwm_a, i);
analogWrite(pwm_b, i);
analogWrite(pwm_c, i);

delay(100);
}

Serial.println("All pwm channels at max.");
delay(1000);
Serial.println("Fading all channels to 0");

for (i=255; i>=0; i--)
{
analogWrite(pwm_a, i);
analogWrite(pwm_b, i);
analogWrite(pwm_c, i);

delay(100);
}

Serial.println("All pwm channels at zero.");
delay(1000);

fade_channel(pwm_a);
fade_channel(pwm_b);
fade_channel(pwm_c);
}

void fade_channel(int channel)
{
Serial.println("Fading pwm channel to max: ");
Serial.println(channel);

for (i=0; i<=255; i++)
{
analogWrite(channel, i);
delay(100);
}

Serial.println("pwm channel at max.");
delay(1000);
Serial.println("fading down.");

for (i=255; i>=0; i--)
{
analogWrite(channel, i);
delay(100);
}

Serial.println("pwm channel at 0.");
delay(1000);
}



A plus is....... the serial link
mathman475 years ago
Very nice job. And thank you for including everything we need to make it including the code, without having to ask for it. One thing I noticed though, and maybe it was because of the ambient light, but in Step 6 it seemed to run slow. Maybe I couldn't see some of the colors. Thanks again, Dennis F.
oomlout (author)  mathman475 years ago
Hi Mathman I'm afraid we had some trouble filming these LEDs due to their brightness. In step six it cycles from red to green to blue, however the point source of light is bright enough to cause the cameras sensor to be saturated, and show as white. We're trying to work out a way to film them better. Regards Stuart
Maybe put a diffuser over them? Also, something that may be of interest to those reading this: The NXP PCA9635 can be placed on a Sparkfun SSOP-28 breakout board ( http://www.sparkfun.com/commerce/product_info.php?products_id=500 ) in order to control 5 RGB LEDs with one channel to spare. Multiple 9635s can also be chained off of a single Arduino's I2C bus too.
Interesting chip.
I've always been partial to TI's TLC5940, a very common 16 channel PWM driver. Far as I can tell, the differences are thus: the 5940 is sink-only, adjustable frequency, SPI, and can sink 120mA per channel at best.
I do like the sink-source ability though. Pity it's only 10mA source.
Photo-Worx4 years ago
Could you please link to or post the code so that it is usable?
Maybe try attaching the actual pde files.
Thanx.
robot+more4 years ago
Where did you get that glowing USB cord or did u make it ?
 Thank you for posting this info and the sample code! It's a big help.

I'm using some 4 pin Common Anode RGB LEDs w/ 220ohm resistors to good effect. My LED pins are the straight kind, different from your example but same enough. With the long pin Pin2 (second in from the left) they are Pin1.Green; Pin2.Power(+5v); Pin3.Blue; Pin4.Red. I'd been having trouble because I'd connected Pin2 to Ground, which might be true for Common Cathode? Anyway, your example gave me some concepts to play with and I got it working. Thanks!!!

I've integrated my PWM RGB LED with some random value generating code to produce an Arduino mood light that changes into a notification light when signaled from "Processing" over the Serial Connection.

Maybe I'll turn it into my first instructable. :)
Cheers!
Portland, Oregon.
Matt LaVoie4 years ago
Could you attach the .pde file of the saved code or a text file of it?  That'd be much easier to handle.

Thanks!
PoisonMondo4 years ago
can you please make a pde file i try to compile and it doesnt work
xVirus4 years ago
where is the code ?
Jodex xVirus4 years ago
Look closer... There:
Appendix 1: _RGBL_AnalogTest
//---bof---RGBL-Analog Preamble//RGB LED pinsint ledAnalogOne[] = {3, 5, 6}; //the three pins of the first analog LED 3 = redPin, 5 = greenPin, 6 = bluePin //These pins must be PWMint ledAnalogTwo[] = {9, 10, 11}; //the three pins of the second analog LED 9 = redPin, 10 = greenPin, 11 = bluePin //These pins must be PWM//Defined Colors (different RGB (red, green, blue) values for colors//(to add your own ie. fuscia experiment and then add to the list)const byte RED[] = {255, 0, 0}; const byte ORANGE[] = {83, 4, 0}; const byte YELLOW[] = {255, 255, 0}; const byte GREEN[] = {0, 255, 0}; const byte BLUE[] = {0, 0, 255}; const byte INDIGO[] = {4, 0, 19}; const byte VIOLET[] = {23, 0, 22}; const byte CYAN[] = {0, 255, 255}; const byte MAGENTA[] = {255, 0, 255}; const byte WHITE[] = {255, 255, 255}; const byte BLACK[] = {0, 0, 0}; const byte PINK[] = {158, 4, 79}; //---eof---RGBL-Analog Preamblevoid setup(){ for(int i = 0; i < 3; i++){ pinMode(ledAnalogOne[i], OUTPUT); //Set the three LED pins as outputs pinMode(ledAnalogTwo[i], OUTPUT); //Set the three LED pins as outputs } setColor(ledAnalogOne, BLACK); //Turn off led 1 setColor(ledAnalogTwo, BLACK); //Turn off led 2}void loop(){/* Example 1 - Defined Colors Set to a known color (you can use any of the above defined colors)*/ setColor(ledAnalogOne, MAGENTA); /* Example 2 - Any Color Set the LED to any color you like*/ //byte tempColor[] = {12,34,12}; //the RGB (red, gren blue) value for a color to display //setColor(ledAnalogOne, tempColor); /*Example 3 - Fading Fade the LED between two colors (this will go from red to green to blue then back to red)*/ //fadeToColor(ledAnalogOne, RED, GREEN, 10); //fadeToColor takes 4 parameters //ledAnalogOne - an array with 3 values defining the red, green and blue pins of the LED //RED - This is the start color //GREEN - This is the end color //10 - the delay (in milliseconds between updates) (determines the fade speed) //fadeToColor(ledAnalogOne, GREEN, BLUE, 10); //Fades from Green to Blue //fadeToColor(ledAnalogOne, BLUE, RED, 10); //Fades from Blue to Red}/* Sets the color of the LED to any RGB Value led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)) color - (byte array of three values defing an RGB color to display (color[0] = new Red value, color[1] = new Green value, color[2] = new Red value*/void setColor(int* led, byte* color){ for(int i = 0; i < 3; i++){ //iterate through each of the three pins (red green blue) analogWrite(led[i], 255 - color[i]); //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color) //we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0) //and off when we output analogWrite(pin, 255). }}/* A version of setColor that takes a predefined color (neccesary to allow const int pre-defined colors */void setColor(int* led, const byte* color){ byte tempByte[] = {color[0], color[1], color[2]}; setColor(led, tempByte);}/* Fades the LED from a start color to an end color at fadeSpeed led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)) startCcolor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value endCcolor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade*/ void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed){ int changeRed = endColor[0] - startColor[0]; //the difference in the two colors for the red channel int changeGreen = endColor[1] - startColor[1]; //the difference in the two colors for the green channel int changeBlue = endColor[2] - startColor[2]; //the difference in the two colors for the blue channel int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change for(int i = 0 ; i < steps; i++){ //iterate for the channel with the maximum change byte newRed = startColor[0] + (i * changeRed / steps); //the newRed intensity dependant on the start intensity and the change determined above byte newGreen = startColor[1] + (i * changeGreen / steps); //the newGreen intensity byte newBlue = startColor[2] + (i * changeBlue / steps); //the newBlue intensity byte newColor[] = {newRed, newGreen, newBlue}; //Define an RGB color array for the new color setColor(led, newColor); //Set the LED to the calculated value delay(fadeSpeed); //Delay fadeSpeed milliseconds before going on to the next color } setColor(led, endColor); //The LED should be at the endColor but set to endColor to avoid rounding errors}/* A version of fadeToColor that takes predefined colors (neccesary to allow const int pre-defined colors */void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){ byte tempByte1[] = {startColor[0], startColor[1], startColor[2]}; byte tempByte2[] = {endColor[0], endColor[1], endColor[2]}; fadeToColor(led, tempByte1, tempByte2, fadeSpeed);}
flemron4 years ago
where is this code please?
http://www.evilmadscientist.com/article.php/LEDpics

From what I can tell, you might get better luck using a mirror and filming the reflection of the LEDs rather than filming the LEDs themselves. The link above mentions that it works for photography, so I'm not certain whether or not it would help with film, but it's worth a shot.