Introduction: 6 Button RGB Controller.

About: I'm a jack of all trades and a master of none. I like to tweak, mod and improvise whenever possible!
In this instructable we will learn how to use 6 buttons to control the color of an RGB LED with the use of an Arduino Micro Controller. This is a fun and easy little project that should take you no more then 20 minutes to finish. 


Materials Needed:
  • Arduino
  • Beadboard
  • Jumper Wires
  • 6x push buttons
  • RGB LED with common Anode
  • 220 Ohm Resistor
  • 470 Ohm Resistor
  • 1K Ohm Resistor

Step 1: Connecting an RGB LED

Before we connect the LED to the breadboard we first need to identify the Anode and Cathodes. If you pick up your LED at a retailer there should be a diagram on the package indicating the Anode and Cathode as well as which pin goes to which color. As with every LED the longest pin is the Anode. On my LED the pins are arranged as Red Cathode, Anode, Blue Cathode, and Green Cathode. We can now plug the LED into the breadboard. 

Another important fact about LEDs is the Anode always has the voltage going into it and the Cathode goes to ground. So we will want to place a jumper from the Anode to the 5V rail on the breadboard. Next we need to add resistors to the cathodes. Resistors are needed to limit the current flowing to the LEDs. Since we are connecting the LED to 5V we need to limit the current going into it otherwise we could burn out the LED. If we go from one cathode to the next plugin the same resistor between it and ground we can see that the different colors are not shining at the same intercity. In the pictures i used a 220 Ohm resistor to demonstrate the differences in brightness. Its easy to see that the Red is the dimmest. The difference in the Blue and Green are hard to tell in the picture but the green is actually brighter. So we will use a 220 Ohm Resistor on the Red, 470 Ohm on the Blue and a 1k Ohm on the Green. We want to plug them in with the LED and on the other side of the board. Finally we will connect some longer jumper wires to the resistors. 

Now we will take the wire on the Red LED and connect it to pin 9 on the Arduino. We put the Blue LED wire on pin 10 and the Green LED wire on Pin 11.

Step 2: Connecting the 6 Buttons

Now we move onto the buttons. We want to place them on the board with 3 on the top half and 3 on the bottom half. We want one leg of each button tied to ground with a jumper wire.

On the 3 lower button we want to place a jumper wire on there other leg to run up to the top side of the bread board. This will help keep the wiring good and tidy. Now we can take a 6 good lengths of jumper wire and attach them to the other leg of all the buttons. I used a piece of ribbon cable to help keep the wires organized. 

The buttons can be wired to ground and don't need a pull up resistor if you are using a Arduino Nove or newer since they have built in pull up resistors.

Now we need to pay attention to what wire we are handling as we connect them to the Arduino. The wire will go in as follows:
  • Upper Left button to Pin 2
  • Lower Left button to Pin 3
  • Upper Middle button to Pin 4
  • Lower Middle button to pin 5
  • Upper Right button to Pin 6
  • Lower Right button to Pin 7
Make sure you have a couple of jumpers going from the 5V and Grd pin on the Arduino to the 5V and ground rails on the bread board and we are ready to write a sketch to control this mess.

Step 3: Understanding the Sketch

The file at the bottom of this page is the Arduino Sketch so you don't have to copy and paste it from here. I think i did a fairly good job of noting the sketch to make it easy to understand. If i'm missing anything or am totally wrong on something plaese let me know. 

/*
  RGB Color Wheel w/ Serial Comunication

  Indivigually control the brightness of each color in an RGB LED
  using 6 pushbuttons attached to pins 2 thru 7. The pushbuttons
  on the even number pins will brighten the corosponding
  LED and the odd numbered ones will dim them. RGB will be attached
  to pins 9, 10, and 11.

  The Circuit:
  * RBGB LED with common anode on +5V
  * Red cathode connected to pin 9 threw 220 Ohm resistor
  * Blue cathode connected to pin 10 threw 470 Ohm resistor
  * Green cathode connected to pin 11 threw 1000 Ohm resistor
  * pushbutton 1 attached to pin 2 from ground
  * pushbutton 2 attached to pin 3 from ground
  * pushbutton 3 attached to pin 4 from ground
  * pushbutton 4 attached to pin 5 from ground
  * pushbutton 5 attached to pin 6 from ground
  * pushbutton 6 attached to pin 7 from ground

  If the RGB LED in use has a common cathode then the roles of the
  buttons become reversed and the brightness of the LED will be
  full on at the start of code.

  The Serial Monitor will display the Red, Green, and Blue LED
  level starting at 255. Each button press rases or lowers the
  brightness in icrements of 5.

  created 13 Jan 2010
  by mpilchfamily
  modified 16 Jan 2010
  by mpilchfamily
  updated for Arduino 1.0 8 Feb 2010
  by Mpilchfamily
  */

boolean ledpin[] = {9, 10, 11};  // assign LEDs to pins

boolean button[] = {2, 3, 4, 5, 6, 7};  // assign pushbuttons to pins

boolean buttonstate = 0;

int fadered = 255;    // starting brightness values for each led
int fadegreen = 255;  // 255 sets the LEDs to the off position
int fadeblue = 255;   // when an RGB with a common anode is in use

void setup ()
{    
  Serial.begin(9600); // designates serial comunication rate
  for(int x=0; x<6; x++) //starts a loop to set pin mode for all pins
  {
    pinMode(button[x], INPUT);  //initialize pushbutton pins as input:
  }
  for(int x=0; x<6; x++) //starts a loop to digital wrie all teh buttons
  {
    digitalWrite(button[x], HIGH);  // pushbuttons are read as high till pressed
  }
  analogWrite(ledpin[0], fadered);    //lights the LED at current brightness
  analogWrite(ledpin[1], fadegreen);    //lights the LED at current brightness
  analogWrite(ledpin[2], fadeblue);    //lights the LED at current brightness   
  }

void loop()
{
  for(int x=0; x<6; x++) //loop for checking all the buttons
  {
    buttonstate = digitalRead(button[x]);    //check button state
    if(buttonstate == LOW && fadered >0 && button[x] == 2)
    {
      // if button is pressed increase brightness by 5
      fadered -=5;
      analogWrite(ledpin[0], fadered);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    }

    if(buttonstate == LOW && fadered < 255 && button[x] == 3)
    {
      // if button is pressed decrease brightness by 5
      fadered +=5;
      analogWrite(ledpin[0], fadered);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    }

    if (buttonstate == LOW && fadegreen > 0 && button[x] == 4)
    {
      // if button is pressed increase brightness by 5
      fadegreen -=5;
      analogWrite(ledpin[1], fadegreen);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    } 

    if (buttonstate == LOW && fadegreen < 255 && button[x] == 5)
    { 
      // if button is pressed decrease brightness by 5
      fadegreen +=5;
      analogWrite(ledpin[1], fadegreen);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    }

    if (buttonstate == LOW && fadeblue > 0 && button[x] == 6)
    { 
      // if button is pressed increase brightness by 5
      fadeblue -= 5;
      analogWrite(ledpin[2], fadeblue);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    }

    if (buttonstate == LOW && fadeblue < 255 && button[x] == 7)
    { 
      // if button is pressed decrease brightness by 5
      fadeblue += 5;
      analogWrite(ledpin[2], fadeblue);  // lights LED at current brightness level
      delay(250);  // allows time so button won't be detected multiple times
    }
    if(buttonstate == LOW)  // checks if any button has ben pressed
    {  // prints brightness levels to the PC. 0 being full on and 255 being off
      Serial.print("Red LED level is at: ");
      Serial.println(fadered);
      Serial.print("Green LED level is at: ");
      Serial.println(fadegreen);
      Serial.print("Blue LED level is at: ");
      Serial.println(fadeblue);
    }
  }
}

Step 4: Conclusion

So there you have it a six button interface for controlling an RGB LED through an Arduino. I hope you learned a little something 



Here is a link to the old Arduino Forums where is was originally documenting this project. I wanted to include the link so you could get a better idea of what has gown into creating this interactive little light show. 
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263501476/0
Arduino Challenge

Participated in the
Arduino Challenge