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
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.
Parts
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






Colors

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);
- }
- 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);
- }
Using Internet 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).
- setColor(0x4B, 0x0, 0x82); // indigo
Theory (PWM)

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.