Monday, January 31, 2011

Multicolor (RGB) LED with Arduino PWM

This is the next step from our last post, where we take the single color example of PWM brightness control, and expand it to a RGB LED with a whole spectrum of color outputs. Instead of having one potentiometer, we now have three. Instead of using one analog ports, we use 3, and the same on the pwm ports.

The wiring is similar, each pot connects to +5 and ground on the outside legs, and each center pin connects to a analog port. The RGB LED has 4 legs, one for each color and a common anode. Arduino pins 7,6, and 5 connect to R, G, and B respectively (through a 270 ohm resistor). The code is similar, just duplicated lines for the other two colors.

Now you can make pink, turquoise, and more. The next step will be to upgrade to a 1 watt RGB LED, which requires 3 power transistors to handle the increased current.

(video uploaded to youtube)


3 Linear Taper 5k ohm potentiometers
3 270 ohm resistors
1 RGB common anode LED
1 Arduino

Download .pde

int sensorPinR = A0;    // red pot
int sensorPinG = A1;    // green pot
int sensorPinB = A2;    // blue pot
int ledPinR = 7;      // red LED
int ledPinG = 6;      // green LED
int ledPinB = 5;      // blue LED
int sensorValueR = 0;  // red variable
int sensorValueG = 0;  // green variable
int sensorValueB = 0;  // blue variable

void setup() {}

void loop() {
  // read the value from the sensor:
  sensorValueR = analogRead(sensorPinR);  
    sensorValueG = analogRead(sensorPinG); 
      sensorValueB = analogRead(sensorPinB); 
  // converts 0-1023 to 0-255
  sensorValueR /=4;  
   sensorValueG /=4;  
    sensorValueB /=4;   
  // outputs PWM signal to LED
  analogWrite(ledPinR, sensorValueR);  
    analogWrite(ledPinG, sensorValueG); 
      analogWrite(ledPinB, sensorValueB);