Introduction: How to Use an RGB LED

About: Technologist, Electronic Engineer, sometime Coderdojo mentor.

Using an RGB LED you can cycle through all colours. Useful to make, Mood Lamp, expressive robot or cool light effects.

But to simply set the colour (Hue) can be a little tricky because you need to convert that to individual brightness value for each of the Red, Green and Blue LEDs.

Wire the circuit as shown below:

Red LED Anode : D3

Green LED Anode : D5

Blue LED Anode : D6

Common Cathode -> 220 Ohm resistor -> Gnd

NOTE: Whist a common Cathode resistor gives you the simplest wiring, the colour matching to the Hue value can be a little inaccurate because the forward Voltage drop of the coloured LEDs is very different. For more accurate colour rendition use 3 220Ohm resistors from the Arduino pins to the Anodes and connect the Cathode directly to Gnd.

Step 1: Hue and Brightness

Hue is a value from 0 to 360 (degrees) that describes the location of the chosen colour on the colour wheel.

See the picture to see the various colours (source: http://en.wikipedia.org/wiki/File:HueScale.svg)
Brightness is controlled separately.

We need some code to convert Hue and Brightness to RGB levels to go to the LEDs.

Note: The max value an 8bit number can hold is 255. So the Hue values are scaled to go from 0 to 255 rather than 0 to 360.


Step 2: Here's the Code

/*
* From Arduino Cookbook, Recipe 7.4 (Open Source), Michael Margolis
* RGB_LEDs sketch
* RGB LEDs driven from analog output ports
*/

const int redPin   = 3;        // choose the pin for each of the LEDs
const int greenPin = 5;
const int bluePin  = 6;
const boolean invert = false; // set true if common anode, false if common cathode

int color = 0; // a value from 0 to 255 representing the hue
int R, G, B;  // the Red Green and Blue color components

void setup()
{
  // pins driven by analogWrite do not need to be declared as outputs
}

void loop()
{
  int brightness = 255; // 255 is maximum brightness
  hueToRGB( color, brightness);  // call function to convert hue to RGB
  // write the RGB values to the pins
  analogWrite(redPin, R);
  analogWrite(greenPin, G);
  analogWrite(bluePin, B );

  color++;           // increment the color
  if(color > 255)
     color = 0;
       delay(50);
}

// function to convert a color to its Red, Green, and Blue components.

void hueToRGB( int hue, int brightness)
{
    unsigned int scaledHue = (hue * 6);
    unsigned int segment = scaledHue / 256; // segment 0 to 5 around the
                                            // color wheel
    unsigned int segmentOffset =
      scaledHue - (segment * 256); // position within the segment

    unsigned int complement = 0;
    unsigned int prev = (brightness * ( 255 -  segmentOffset)) / 256;
    unsigned int next = (brightness *  segmentOffset) / 256;

    if(invert)
    {
      brightness = 255-brightness;
      complement = 255;
      prev = 255-prev;
      next = 255-next;
    }

    switch(segment ) {
    case 0:      // red
        R = brightness;
        G = next;
        B = complement;
    break;
    case 1:     // yellow
        R = prev;
        G = brightness;
        B = complement;
    break;
    case 2:     // green
        R = complement;
        G = brightness;
        B = next;
    break;
    case 3:    // cyan
        R = complement;
        G = prev;
        B = brightness;
    break;
    case 4:    // blue
        R = next;
        G = complement;
        B = brightness;
    break;
   case 5:      // magenta
    default:
        R = brightness;
        G = complement;
        B = prev;
    break;
    }
}

Step 3: Try These Things to Make It Better

  1. Add a Potentiometer to change the colour depending on how the Potentiometer is turned
  2. Make the colour change depending on an LDR
  3. Program the colour from a web page
  4. Try more projects here