/arduino-rgb-tutorial

Introductory-level tutorial explaining how to use an Arduino to control an RGB LED.

Primary LanguageArduino

Instructions for using an Arduino to control RGB LEDs

These instructions cover how to use the Arduino to control an RGB LED. There are two stages to this process. The first stage is to take all the physical components (like the Arduino and LED) and assemble them into a circuit. The second stage is to program the Arduino to control the electrical signals in the circuit.

Hookup Guide

To hook up all the physical components, first you will gather the parts, then you will connect them with wires.

Overview of the parts

  • 1 x Arduino Uno (pic). (Buy on Adafruit) (Buy on Sparkfun)
  • 3 x resistors (pic) with the right resistance values (we provide 220 Ohm resistors). (Buy)
  • 1 x RGB LED (pic). LEDs come in "cathode" and "anode" flavors. The only difference is that cathode LEDs will be connected to ground (0V) and are turned on by setting the pin voltage high (e.g., 5V), whereas anode LEDs will be connected to high voltage (e.g., 5V) and turned on by setting the voltage low (0V). We suggest that beginners use cathodes because they are more intuitive to work with, but we happened to buy anodes for this exercise. (Buy cathode) (Buy anode)
  • 4 x male-to-male jumper wires (pic) or stripped wires (pic). If the wire colors are red, blue, green, and black, it will make debugging easier, but is not strictly necessary. Don't be fooled by male-to-female or female-to-female jumpers! (Buy)
  • 1 x breadboard. They come in half size and full size, either one is fine. (Buy)
  • 1 x USB A to B cable (pic). (Buy)

Arduino Uno: The Arduino Uno is a board that allows you to connect a programmable microcontroller (the ATmega328, which is the big rectangle labeled on the right-hand side in this diagram) to electrical circuits through pins. You can program the Arduino Uno using the Arduino IDE application. Depending on the code you upload, the microcontroller will change the voltage on its pins in certain ways, which can be used to transmit information or control other components on any connected circuits.

Resistors: The most important thing about resistors for the purposes of this tutorial is that they can keep our LEDs and Arduino from burning out. Our resistors are 220 Ohms. You can tell by putting their band color sequence (red, red, brown, gold) into a resistor value calculator. We picked 220 Ohms based on the properties of the particular RGB LEDs that we bought. A different LED might need a different resistor. If you want to learn more about resistors and how we picked the resistance value, feel free to ask!

RGB LED: An RGB LED actually contains three tiny single-color LEDs inside: one red, one green, and one blue. There are four pins on an RGB LED, one for each color and a fourth one that connects to low voltage (i.e., ground) on cathode LEDs, or high voltage (e.g., 5V) for anode LEDs. We bought anodes, so our LEDs will connect to 5V.

Jumper wires: Jumper wires are normal wires that have had attachments added to the ends to make them easier to connect to pins and breadboards and other circuit components. Stripped wires do not have those nice plastic attachments, so sometimes the ends can get bent and annoying to work with if you are connecting and disconnecting them often, but electrically they will work just as well.

Breadboard: Breadboards are a convenient way to connect a bunch of electrical components together into circuits. This picture shows how the holes are connected together in strips in most breadboards. There are two main parts: The power rails (the vertical strips in the picture) and the component rails (the horizontal strips). The power rails run down the side of the breadboard. Technically you can connect any wire to those rails, but by convention we connect high voltage (for example, 5V) to the red rail labeled with + and we connect ground to the blue rail labeled with -. The component rails are connected in horizontal rows. These rows do not connect across the gap in the middle. Generally you plug various components like (resistors or LEDs) into these rows, and then connect them to the power or ground rails, or to the Arduino.

USB A to B cable: The USB cable is how you will program and power your Arduino Uno and the electrical circuit.

How to connect the parts

Now that you have your parts, it's time to put them together! Here is the hookup diagram for our circuit. Your finished circuit will look like something like this, except that the black wire will connect to 5V instead of ground. Note that our hookup diagram is for an anode LED. If you have a cathode LED, your black wire will connect to ground instead of 5V. All of the ground pins (marked GND) are connected on the Arduino, so you can use any GND pin you want.

The one thing the hookup diagram does not show is which of the LED pins are connected to which wires.

The LED's pins are numbered in order from Pin 1 to Pin 4. The longest pin of the LED is Pin 2. Here is an illustration of the LED pin labels on a cathode LED. Since we are using anodes instead of cathodes, our LED's Pin 2 will connect to 5V instead of ground.

  • LED Pin 1 controls red and is connected to the Arduino's Pin 9.
  • LED Pin 2 is a power thing. In anodes like the ones we are using, it is connected to the Arduino's 5V pin. In cathodes, it is connected to the GND pin.
  • LED Pin 3 controls green and is connected to the Arduino's Pin 10.
  • LED Pin 4 controls blue and is connected to the Arduino's Pin 11.

Start connecting your circuit! Because the LED's pins are uneven lengths, you will almost certainly have to bend the pins to insert them all the way into the breadboard. Also, so long as no power is flowing through the circuit, you can connect things together in any order.

Programming the Arduino

Now that you have assembled your circuit, it's time to program the Arduino!

Install the Arduino IDE application on your computer if you haven't already. IDE stands for Integrated Development Environment. The Arduino IDE is a code editor that will allow you to write and upload code to the Arduino and do other useful things.

Once the Arduino IDE is installed, open the Arduino application. Once open, go to Tools > Board and select Arduino Uno if it isn't already selected.

Next, plug your Arduino Uno into your computer using the USB A to B cable. Go to Tools > Port and select the Arduino device. Now you can start uploading programs!

Go to File > New then delete the template code in the new window and paste this code into the window. Then click the round Upload button (the right-pointing arrow icon located second from the top left). You should see things happening on the bottom and then the message "Done uploading." Your Arduino should start going through some colors!

The example code is a good place to start playing around with controlling the LED! If you are using a cathode LED, setting the pin high for a particular color LED will turn that LED on, as you would expect. For an anode LED, setting it high will turn it off, and setting it low will turn it on. The code we provide was written for a cathode LED. However, the code can be run using an anode LED without making any changes! The anode LED will just display a different pattern of colors than a cathode LED (in some sense, the "opposite" colors).

Another program to try is this one. It will flash through the colors and then fade smoothly though the colors. There are a lot of comments in the code, but the code itself is simpler than the length of the file would suggest.

More Resources (Optional)

Here are some more links to tutorials about controlling RGB LEDs with Arduino: