Wednesday, May 9, 2012

Using RGB LED With Arduino


Hardware:

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


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

0 comments:

Post a Comment