Pages

01 April, 2013

Arduino - RGB LED


In almost every project I make, I need the Arduino to respond to me when something happens. If you are debuging, you can easily use the serial monitor or a LCD screen. If your project has different modes or if there are more than one outcome, you may want to use something like a RGB LED. You probably now how to use a regular LED allready, and a RGB LED is not that much harder to use.

Specifications
Type: Common Anode
Datasheet: BL-L515RGBW-CA
Supply voltage red: 2.1-2.5V
Supply voltage green: 3.8-4.5V
Supply voltage blue: 3.8-4.5V
Current: 20mA

Connecting to Arduino
The RGB LED has four pins. The pin next to the flat side of the diode is the red pin. Connect this to a PWM pin on your Arduino (I will be using pin 11) with a 220Ω resistor. The next pin, the longest one, is the anode, and will be connected to 5V. If your RGB LED is common cathode, you will connect it to GND. The third pin is the blue pin, and this should be connected the same way as the red one, but to another PWM pin (I am using pin 9). The final pin is the green pin, and this should be connected the exact same way as the red and the blue pin. Use a 220Ω resistor and connect it to another PWM pin (I am using pin 10).

Code
#define LED_R 11 //RED pin is hooked up to pin 11
#define LED_G 10 //GREEN pin is hooked up to pin 10
#define LED_B 9  //BLUE pin is hooked up to pin 9

void setup()
{
  pinMode(LED_R, OUTPUT); //Prepares the RED pin to be output
  pinMode(LED_G, OUTPUT); //Prepares the GREEN pin to be output
  pinMode(LED_B, OUTPUT); //Prepares the BLUE pin to be output
}

void loop()
{
  int R = 255; //The value to control the brightness of the RED LED
  int G = 255; //The value to control the brightness of the GREEN LED
  int B = 0;   //The value to control the brightness of the BLUE LED

  R = map(R, 0, 255, 255, 0); //The values of the colors are mapped
  G = map(G, 0, 255, 255, 0); //the opposite way, because this RGB LED
  B = map(B, 0, 255, 255, 0); //is "Common-Anode"

  analogWrite(LED_R, R); //Writing the mapped values to RED pin (11)
  analogWrite(LED_G, G); //Writing the mapped values to GREEN pin (10)
  analogWrite(LED_B, B); //Writing the mapped values to BLUE pin (9)

  delay(1000);
}

Color chart (RGB-values)

Color
Red
Green
Blue
White
255
255
255
Off
0
0
0
Red
255
0
0
Green
0
255
0
Blue
0
0
255
Light blue
0
255
255
Pink
255
0
255
Yellow
255
255
0
Orange
255
48
0
Cyan
0
255
48
Purple
48
0
255

No comments:

Post a Comment