# Arduino Lesson 7. Make an RGB LED Fader

## Overview

In this lesson, you will be combining some of the things that you learnt in earlier lessons to use three push switches (from lesson 6) to control the color of the RGB LED that you used in lesson 3 in order to make a Color Organ!

![](https://cdn-learn.adafruit.com/assets/assets/000/002/224/medium800/learn_arduino_overview.jpg?1396780637)

# Arduino Lesson 7. Make an RGB LED Fader

## Parts

To carry out the experiment described in this lesson, you will need the following parts.

# Arduino Lesson 7. Make an RGB LED Fader

## Breadboard Layout

The breadboard layout is shown below. The RGB LED should have the longest lead in row 2 of the breadboard. This will be connected to GND.

This wiring diagram assumes you have a **common cathode** LED - if you have a common anode LED, connect the longest pin to +5V instead of ground. Note that with a common anode display the LED color cycling will be reversed.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/231/medium800/learn_arduino_fritzing.jpg?1396780690)

# Arduino Lesson 7. Make an RGB LED Fader

## 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.

```auto
/*
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 &gt; 255) red = 0;
  }
  if (digitalRead(greenSwitchPin) == LOW)
  {
    green ++;
    if (green &gt; 255) green = 0;
  }
  if (digitalRead(blueSwitchPin) == LOW)
  {
    blue ++;
    if (blue &gt; 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'.

```auto
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:

```auto
  if (digitalRead(redSwitchPin) == LOW)
  {
    red ++;
    if (red &gt; 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.

```auto
  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.

# Arduino Lesson 7. Make an RGB LED Fader

## Other Things to Do

Try removing the delay at the end of 'loop'. You can do this by 'commenting out' the delay line by putting // at the start of the line like this:

```auto
  analogWrite(blueLEDPin, blue);  
  // delay(10);
}
```

This is a useful technique, because when you find out that you need to put the line back in again, you can do so, just by taking the // away again.

Without the delay, you will find that basically, whenever you release the button, you will be left with a random level of brightness for that color. What is happening is that the it is cycling through the brightness levels so fast, that you have no chance of actually controlling it.

Another experiment you could make would be to change the function of the three buttons so that they control the LED as if it was a traffic signal.

Try to make it so that the top button turns the LED fully green, the middle button Orange and the bottom button red.

[Click Here for the Next Lesson](http://learn.adafruit.com/adafruit-arduino-lesson-8-analog-inputs)
 **About the Author**  
  
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit:&nbsp;[Programming Arduino](https://www.adafruit.com/products/1019),&nbsp;[30 Arduino Projects for the Evil Genius](https://www.adafruit.com/products/868)&nbsp;and&nbsp;[Programming the&nbsp;Raspberry Pi](https://www.adafruit.com/index.php?main_page=adasearch&q=programming+raspberry+pi "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=programming+raspberry+pi").
## Featured Products

### Tactile Button switch (6mm) x 20 pack

[Tactile Button switch (6mm) x 20 pack](https://www.adafruit.com/product/367)
Little clicky switches are standard input "buttons" on electronic projects. These work best in a PCB but [can be used on a solderless breadboard as shown in this tutorial](https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs?view=all). The pins are normally...

In Stock
[Buy Now](https://www.adafruit.com/product/367)
[Related Guides to the Product](https://learn.adafruit.com/products/367/guides)
### USB Cable - Standard A-B

[USB Cable - Standard A-B](https://www.adafruit.com/product/62)
This here is your standard A-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Arduino, USBtinyISP (among other things).  
  
3 feet / 1 meter long  
  
Color may vary!

In Stock
[Buy Now](https://www.adafruit.com/product/62)
[Related Guides to the Product](https://learn.adafruit.com/products/62/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)

## Related Guides

- [IR Sensor](https://learn.adafruit.com/ir-sensor.md)
- [Low Power Coin Cell Voltage Logger](https://learn.adafruit.com/low-power-coin-cell-voltage-logger.md)
- [Reverse Geocache Box](https://learn.adafruit.com/reverse-geocache-engagement-box.md)
- [Adafruit 16-channel PWM/Servo Shield](https://learn.adafruit.com/adafruit-16-channel-pwm-slash-servo-shield.md)
- [Remote controlled door lock using a fingerprint sensor & Adafruit IO](https://learn.adafruit.com/remote-controlled-door-lock-using-a-fingerprint-sensor-and-adafruit-io.md)
- [WiFi Candy Bowl Monitor](https://learn.adafruit.com/wifi-candy-bowl.md)
- [3D Printed Animatronic Robot Head](https://learn.adafruit.com/3d-printed-animatronic-robot-head.md)
- [Adafruit INA219 Current Sensor Breakout](https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout.md)
- [Babel Fish](https://learn.adafruit.com/babel-fish.md)
- [Geofencing with the FONA 808 & Adafruit IO](https://learn.adafruit.com/geofencing-with-the-fona-808-and-adafruit-io.md)
- [Digital Circuits 7: MCUs... how do they work?](https://learn.adafruit.com/mcus-how-do-they-work.md)
- [Electronic Demon Costume](https://learn.adafruit.com/electronic-demon-costume.md)
- [Arduino Lesson 3. RGB LEDs](https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds.md)
- [Tiny Arduino Music Visualizer](https://learn.adafruit.com/piccolo.md)
- [Introducing Adafruit Trellis ](https://learn.adafruit.com/adafruit-trellis-diy-open-source-led-keypad.md)
