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)

Hardware:

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); 
  
                   
}

PWM LED Brightness Control

Pulse Width Modulation at last! In our last example, we read a potentiometer (pot) to get a analog value of 0-1023, and used that number to delay the digital on and off times of a LED. This example will do the same read, but output a analog value (PWM) of 0-255 to control the brightness of a LED. It's still controlling the on and off times, but so quickly the eye doesn't see off, it just sees decreased or increased brightness. this is much more power efficient than using a resistor to shed voltage, dimming a bulb. We will use this same method in an upcoming post to control the speed of a motor.

(video uploaded to youtube)

int sensorPin = A0;    // Analog input pin to pot
int ledPin = 7;      // PWM pin to LED
int sensorValue = 0;  // variable to store pot value


void setup() {}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);  
  // converts 0-1023 to 0-255
  sensorValue /=4;   
  // outputs PWM signal to LED
  analogWrite(ledPin, sensorValue);  
  
                   
}

LED Blink Speed / Analog Sketch

I'm doing a series of mini projects that will build up to a motor controller, but need to get some basics across first. This first sketch is a Arduino, reading a potentiometer on an analog port, and using that data to control the blink speed of a LED on a PWM port. The potentiometer (pot) connects to ground and +5 on the outside legs, and Analog 0 on the center leg. The LED connects + to pin 7 and - to a 270 ohm resistor connected to ground. The value of the pot determines the delay on the LED. This not true PWM, that's coming next.

(video uploaded to youtube)

int sensorPin = A0;    // Analog input pin to pot
int ledPin = 7;      // PWM pin to LED
int sensorValue = 0;  // variable to store pot value

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  
}