RBG LED Color Chooser

This is a basic tutorial for using Common Anode RGB LEDs, PWM, and 10K potentiometers. By the end, you will have three potentiometers that control each of the three colors of the LED.

Connect the LED's anode lead to +5v and connect the 3 RGB cathode leads to digital pins 9, 10, 11 respectively. Use 220 Ohm resistors on each pin of the cathode pins.

Wire up each potentiometer's first pin to +5v, the third pin to ground, and the second pin to analog pins 0, 1, 2. 0 is for Red, 1 is for Green, and 2 is for Blue.

Upload this source code to the arduino. Then turn up and down each pot to change the color of the LED.

Questions or problems: ziptiesispro@yahoo.com

  1.  
  2. // Init the Pins used for PWM
  3. const int redPin = 9;
  4. const int greenPin = 10;
  5. const int bluePin = 11;
  6.  
  7. // Init the Pins used for 10K pots
  8. const int redPotPin = 0;
  9. const int greenPotPin = 1;
  10. const int bluePotPin = 2;
  11.  
  12. // Init our Vars
  13. int currentColorValueRed;
  14. int currentColorValueGreen;
  15. int currentColorValueBlue;
  16.  
  17. void setup()
  18. {
  19.   pinMode(redPin, OUTPUT);
  20.   pinMode(greenPin, OUTPUT);
  21.   pinMode(bluePin, OUTPUT);
  22. }
  23.  
  24. void loop()
  25. {
  26. // Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
  27.   currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
  28.   currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
  29.   currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
  30.  
  31. // Write the color to each pin using PWM and the value gathered above
  32.   analogWrite(redPin, currentColorValueRed);
  33.   analogWrite(bluePin, currentColorValueBlue);
  34.   analogWrite(greenPin, currentColorValueGreen);
  35.  
  36. }
  37.  

Share