RGB-LED

From ArduinoInfo
Jump to navigation Jump to search

Red-Green-Blue (RGB) LEDs:

Thanks to Matthew Beckler for allowing us to use his material here.

Matthew did this cool animation of the way Red,Green and Blue mix to make many colors.

RGB-animation.gif

Abstract: This is similar to the RGB LED available here. Using this RGB LED with PWM output will be a fun way to get introduced to the Arduino way. Using three external potentiometers, we are able to mix different proportions of red, green, and blue in the same LED, producing nearly any color imaginable.
General Description: The Arduino is a pretty slick platform for microcontroller development based on the Atmel line of microcontrollers. The system comes with some convenient functions for initialization and operation, that really help decrease the development time for most projects. For this little project, we will to connect a tri-color RGB LED to the microcontroller with PWM ("analog") outputs to control the brightness of each color, and use three analog inputs to set each color's brightness.
More Details (Tri-color RGB LED's): An RGB LED is simply three separate LEDs crammed into a single 5mm LED package. There is one each of red, green, and blue LED elements. These three LEDs share the same positive (anode) terminal, which means that this RGB LED has a "common anode" connection. To control each color, simply connect its cathode pin to ground (through a resistor - 220 Ohms is usually good), and it will light up. Be sure to use current-limiting resistors to protect the LEDs from burning out, and you can also use pulse-width modulation (see below) to change the brightness of an LED. the correct pin-out is here:

LED Pinout

LED Pinout


More Details (Pulse-width Modulation): The brightness of an LED is proportional to the current going through it, but it would be rather difficult to use a microcontroller to accurately control the current flowing through an LED. Fortunately, human vision has a nice phenomenon called persistence of vision. Persistence of vision is the phenomenon where an image that is seen for only a fraction of a second will continue to be "seen" by your brain even after the original image has vanished or moved. This this the same principle behind film and television, where a rapidly changing image tricks your brain into seeing continuous motion. By turning our LED on and off rapidly, we can trick the brain into seeing an "average" value of brightness based on the duty cycle of the driving PWM signal.

Pulse-width modulation (PWM) is the process of modulating the duty cycle of a signal, used in this application to control the average power sent to each LED. In the following figure, we show three different duty cycles, first with 50% duty cycle, then 10% and 90% duty cycle. During the 10% duty cycle, the signal is at the logic high level for only a brief time each cycle, but with 90% duty cycle, most of the signal's period is spent at logic high level. If the frequency of the signal is fast enough, then there will be no visible flicker, and the LED's brightness will be proportional to the signal's duty cycle.


Pulse-width modulation

Pulse-width modulation


Circuit Schematics: The circuit is divided into two sections, the Input and Output sections. The Input circuit has three potentiometers, which are simply variable resistors like this one. or This One. For this application, we are using the potentiometers to produce a controllable analog voltage by connecting the potentiometer's ends to power and ground, and connecting the third terminal (the "wiper") to the analog input pin. When we turn the potentiometer, the measured voltage will vary between ground and power (here, 5 volts). There are three potentiometers, one for each color, and they are connected to the Arduino's analog inputs 0, 1, and 2, corresponding to red, green, and blue.

The Output circuit has the LED's common anode connected directly to power (also known as Vdd). The cathode pin of each color is connected through a current-limiting resistor to a digital output pin on the Arduino. The current-limiting resistors are used to set the current through the LEDs. If you want more information about driving LEDs with and without resistors, there is a good guide at the Tinkerlog website. We use the Arduino's pins 9, 10, and 11 specifically because these pins are able to do PWM output, via the analogWrite() function. We can set the brightness for each color independently.

Circuit Schematics

Circuit Schematics


Results: This project turned out very nice. We cut a small hole into a ping pong ball, which we then placed over the LED, to act as a light diffuser. This made the light much more uniform with fewer spots of individual, un-mixed color. Matthew created a little color-shifter program to cycle through lots of different color combinations, and recorded a video shown below.



Source Code: There are two programs to download here. The first is for the project where you use the three potentiometers to control the output color of the RGB LED. The second program ignores the potentiometers and simply cycles between colors over and over again.

Here is the Arduino software sketch for potentiometer control. Cut and paste into an IDE page.

// RGB LED - Color Control With Potentiometers
//
// Matthew L Beckler 
// matthew at mbeckler dot org

int redPin = 11;
int bluePin = 10;
int greenPin = 9;

int redIn = 0;
int greenIn = 1;
int blueIn = 2;

int redVal;
int greenVal;
int blueVal;

void setup()
{
  // nothing to do here
}

void loop()
{
  redVal = analogRead(redIn);
  greenVal = analogRead(greenIn);
  blueVal = analogRead(blueIn);

  // analogRead returns a value between 0 and 1023
  // analogWrite wants a value between 0 and 255
  // That means we need to map the input range to
  // the correct output range.
  redVal = map(redVal, 0, 1023, 0, 255);
  greenVal = map(greenVal, 0, 1023, 0, 255);
  blueVal = map(blueVal, 0, 1023, 0, 255);

  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}



And here is the Arduino software sketch for automatic software control. Cut and paste into an IDE page.

// RGB LED - Automatic Color Cycling
//
// Matthew L Beckler 
// matthew at mbeckler dot org

int redPin = 11;
int bluePin = 10;
int greenPin = 9;

int redIn = 0;
int greenIn = 1;
int blueIn = 2;

int redVal;
int greenVal;
int blueVal;

void setup()
{
  redVal = 255;
  greenVal = 255;
  blueVal = 255;
  update();
}

// This function updates the LED outputs.
void update()
{
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}

// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int* value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      (*value)--;
    else
      (*value)++;

    update();
    delay(10);
  }
}

void loop()
{
  // start out at black (all off)
  color_morph(&redVal,   1); // transition to red
  color_morph(&greenVal, 1); // transition to yellow
  color_morph(&redVal,   0); // transition to green
  color_morph(&blueVal,  1); // transition to aqua
  color_morph(&redVal,   1); // transition to white
  color_morph(&greenVal, 0); // transition to violet
  color_morph(&redVal,   0); // transition to blue
  color_morph(&blueVal,  0); // transition to black (all off)
}



One useful tip: Full illumination of each color is at 0, not 255. If you want to light your LED to bright green, write 0 to the appropriate pin. Writing 255 to the pin turns the color off. The brightest yellow is at analogWrite(bluePin,0); + analogWrite(redPin,0);. This is crucial if you are using your RGB LED to communicate the state of a device.