RGB LEDs with Arduino


RGB LED look just like regular LED, but there are actually three LEDs: red, green and blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want.

Source: https://learn.adafruit.com; Autor: Simon Monk

[adrotate group=”3″]

How all this works

Using the analogWrite function of Arduino we control the color of the LED. By adjusting the brightness of each of LEDs it is possible to mix colors.

This can work with different values ​​of resistors, which is complicated. Andruino functions analogWrite that use pins marked with “~” sign can send different voltage values to the appropriate LEDs.


To build the project, we need the following parts:

  • 1x Diffuse RGB LED 10mm
  • 3x 270 Ω Resistors (red, purple, brown stripes) – you can use up to 1K ohm although it will be a little dimmer
  • 1x Half-size Breadboard
  • 1x Arduino Uno R3
  • 1x Jumper wire pack


1x Diffuse RGB LED 10mm
3x 270 Ω Resistors (red, purple, brown stripes) – you can use up to 1K ohm although it will be a little dimmer
1x Half-size Breadboard
1x Arduino Uno R3
Jumper wire pack


learn_arduino_rdb_led_cct_symbol.jpgThe RGB LED has four leads. There is one lead going to the positive connection of each of the single LEDs within the package and a single lead that is connected to all three negative sides of the LEDs.
The common negative connection of the LED package is the second pin from the flat side of the LED package. It is also the longest of the four leads. This lead will be connected to ground.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.
If you are using a common ANODE LED instead of common CATHODE, connect the long pin to +5 instead of ground


learn_arduino_rgb_color.pngThe reason that you can mix any color you like by varying the quantities of red, green and blue light is that your eye has three types of light receptor in it (red, green and blue). Your eye and brain process the amounts of red, green and blue and convert it into a color of the spectrum.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.
If we set the brightness of all three LEDs to be the same, then the overall color of the light will be white. If we turn off the blue LED, so that just the red and green LEDs are the same brightness, then the light will appear yellow.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.

Arduino Sketch

The following test sketch will cycle through the colors red, green, blue, yellow, purple, and aqua. These colors being some of the standard Internet colors.
  1. /*
  2. Adafruit Arduino – Lesson 3. RGB LED
  3. */
  5. int redPin = 11;
  6. int greenPin = 10;
  7. int bluePin = 9;
  9. //uncomment this line if using a Common Anode LED
  10. //#define COMMON_ANODE
  12. void setup()
  13. {
  14. pinMode(redPin, OUTPUT);
  15. pinMode(greenPin, OUTPUT);
  16. pinMode(bluePin, OUTPUT);
  17. }
  19. void loop()
  20. {
  21. setColor(255, 0, 0); // red
  22. delay(1000);
  23. setColor(0, 255, 0); // green
  24. delay(1000);
  25. setColor(0, 0, 255); // blue
  26. delay(1000);
  27. setColor(255, 255, 0); // yellow
  28. delay(1000);
  29. setColor(80, 0, 80); // purple
  30. delay(1000);
  31. setColor(0, 255, 255); // aqua
  32. delay(1000);
  33. }
  35. void setColor(int red, int green, int blue)
  36. {
  37. #ifdef COMMON_ANODE
  38. red = 255 red;
  39. green = 255 green;
  40. blue = 255 blue;
  41. #endif
  42. analogWrite(redPin, red);
  43. analogWrite(greenPin, green);
  44. analogWrite(bluePin, blue);
  45. }
Try the sketch out and then we will dissect it in some detail……The sketch starts by specifying which pins are going to be used for each of the colors:
  1. int redPin = 11;
  2. int greenPin = 10;
  3. int bluePin = 9;
The next step is to write the ‘setup’ function. As we have learnt in earlier lessons, the setup function runs just once after the Arduino has reset. In this case, all it has to do is define the three pins we are using as being outputs.
  1. void setup()
  2. {
  3. pinMode(redPin, OUTPUT);
  4. pinMode(greenPin, OUTPUT);
  5. pinMode(bluePin, OUTPUT);
  6. }
Before we take a look at the ‘loop’ function, lets look at the last function in the sketch.
  1. void setColor(int red, int green, int blue)
  2. {
  3. analogWrite(redPin, red);
  4. analogWrite(greenPin, green);
  5. analogWrite(bluePin, blue);
  6. }
This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case the number will be in the range 0 to 255, where 0 means off and 255 means maximum brightness. The function then calls ‘analogWrite’ to set the brightness of each LED.
If you look at the ‘loop’ function you can see that we are setting the amount of red, green and blue light that we want to display and then pausing for a second before moving on to the next color.
  1. void loop()
  2. {
  3. setColor(255, 0, 0); // red
  4. delay(1000);
  5. setColor(0, 255, 0); // green
  6. delay(1000);
  7. setColor(0, 0, 255); // blue
  8. delay(1000);
  9. setColor(255, 255, 0);// yellow
  10. delay(1000);
  11. setColor(80, 0, 80); // purple
  12. delay(1000);
  13. setColor(0, 255, 255);// aqua
  14. delay(1000);
  15. }
Try adding a few colors of your own to the sketch and watch the effect on your LED.
If you are using a Common Anode RGB LED, then you need to change the analog write values so that the color is subtracted from 255, Uncomment the line #define COMMON_ANODE in the sketch!

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: http://www.devguru.com/features/colors
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).
The red, green and blue parts of indigo are (in hex) 4B, 00 and 82 respectively. We can plug those into the ‘setColor’ function like this:
  1. setColor(0x4B, 0x0, 0x82); // indigo
We have used hex numbers for the three parts of the color by putting ‘0x’ in front of them.Try adding a few colors of your own to the ‘loop’ function. Don’t forget to add a delay after each one.

Theory (PWM)

learn_arduino_how_pwm_works.jpgPulse Width Modulation (or PWM) is a technique for controlling power.  We also use it here to control the brightness of each of the LEDs.The diagram below shows the signal from one of the PWM pins on the Arduino.
Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the ‘analogWrite’ function. So ‘analogWrite(0)’ will not produce any pulse at all and ‘analogWrite(255)’ will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time.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.

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

There are lots of things you can do with RGB LEDs. Check out some of the projects on the Internet using RGB LEDs and you will find multi-color persistence of vision devices, as well as all sorts of lighting effects.

[adrotate group=”3″]