Arduino Code

Load the following sketch onto your Arduino board.

To start with, all the LEDs will be off. If you press and hold one of the buttons, then the LED will gradually get brighter. The color will be red for the top button, green for the middle one and blue for the bottom button.

When you have got enough of one color, try pressing another button and see how they mix together.

If you want to start over, then press the reset button on the Arduino. This is the red button, next to the USB connector.

Download: file
/*
Adafruit Arduino - Lesson 7. RGB Fader
*/

int redLEDPin = 11;
int greenLEDPin = 10;
int blueLEDPin = 9;

int redSwitchPin = 7;
int greenSwitchPin = 6;
int blueSwitchPin = 5;

int red = 0;
int blue = 0;
int green = 0;

void setup()
{
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);  
  pinMode(redSwitchPin, INPUT_PULLUP);
  pinMode(greenSwitchPin, INPUT_PULLUP);
  pinMode(blueSwitchPin, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(redSwitchPin) == LOW)
  {
    red ++;
    if (red > 255) red = 0;
  }
  if (digitalRead(greenSwitchPin) == LOW)
  {
    green ++;
    if (green > 255) green = 0;
  }
  if (digitalRead(blueSwitchPin) == LOW)
  {
    blue ++;
    if (blue > 255) blue = 0;
  }
  analogWrite(redLEDPin, red);
  analogWrite(greenLEDPin, green);
  analogWrite(blueLEDPin, blue);  
  delay(10);
}

The sketch is similar to that of lesson 3. Again, we have three output pins to control the LED. These are PWM pins so that we can control how much power goes to each color of the LED.

There are three other pins needed, one for each of the buttons and these are configured in 'setup' to be inputs that are pulled up to HIGH, unless the button is pressed, which will cause them to become LOW.

After the pin definition, there is another little group of variables called 'red', 'green' and 'blue'.

Download: file
int red = 0;
int blue = 0;
int green = 0;

These variables are used to hold the current values for the intensity of each of the LED channels. So, if 'red' is 0 then the red part of the LED will be off. However, if it is 255 then it will be at maximum brightness.

The 'loop' function is in two parts. The first part checks the buttons and makes any necessary changes to 'red', 'green' or 'blue' depending on the button. Each button works in the same way, just for a different color. The section for checking the red button is like this:

Download: file
  if (digitalRead(redSwitchPin) == LOW)
  {
    red ++;
    if (red > 255) red = 0;
  }

If, when we carry out a 'digitalRead' on the red switch pin, we find it is LOW, then that means the button is pressed, so we add 1 to 'red'. The command ++ adds one to a variable.

However, we need to be careful here, because the maximum value that you can use for PWM is 255. So on the next line, we check to see if we have gone over the limit, and if we have then red is set back to 0, so we start over.

The second part of the 'loop' function carries out the 'analogWrite's to each of the LED colors.

Download: file
  analogWrite(redLEDPin, red);
  analogWrite(greenLEDPin, green);
  analogWrite(blueLEDPin, blue);  

Finally there is a short delay at the end of the loop, that slows down the color changing to a manageable speed.

This guide was first published on Dec 07, 2012. It was last updated on Dec 07, 2012. This page (Arduino Code) was last updated on Dec 13, 2019.