Controlling RGB LEDs - Analog
Try reloading this page, or reviewing your browser settings
This segment introduces how to use a common cathode RGB LED using digital controls. Examples for controlling the red, green and blue leads with blink and push buttons. The coding concept of arrays are also introduced.
- rgb led
- push buttons
About this video
- Liz Clark
- First online
- 09 April 2019
- Online ISBN
- Copyright information
- © Liz Clark 2019
In this section, we’re going to be looking at our RGB LEDs, a special type of LED that has three chips inside rather than the normal single chip inside a regular LED. The three chips are three different colors– red, green, and blue, which allows you to control the color of the LED by mixing different amounts of red, green, and blue. To follow along you’ll need the Arduino, breadboard, jumper wires, and 220 ohm resistors that we’ve been using so far, as well as common cathode RGB LEDs, push buttons, and 10k resistors. Let’s get started.
First, let’s get better acquainted with RGB LEDs. They have four leads with three being for each chip in the LED. And then one, the longest one specifically, is either an anode that is connection to 5 volts, referred to as a common anode type, or a cathode that is connected to ground, referred to as a common cathode type. For the purposes of this tutorial, we’ll be utilizing a common cathode type RGB LED. RGB LEDs are oriented so that the common is considered lead 2. And then going left to right are the red, lead 1, green, lead 3, and blue, lead 4.
Speaking of circuits, let’s begin by building a simple RGB LED circuit by simply connecting it into the breadboard. First, insert your RGB LED into the breadboard and connect the common cathode, leg 2 to the ground rail on the breadboard. Then attach a 220 ohm resistor to each color lead– leads 1, 3, and 4. Next, we’re going to connect each color lead to a different digital pin on the Arduino, specifically pins 2 through 4.
Now we can move onto the code. Basically, we’re going to blink each individual color in the RGB LED to make sure that each chip is working. We’re also going to write this data to the serial monitor. First, we’ll set up our digital pins as integers. Put in int red = 2, int green = 3, and int blue = 4. In the setup, we’re going to set up each pin mode to be an output with pin mode red output, pin mode green output, and pin mode blue output. We’re also going to open up serial communication with serial.being 9600.
Now for the loop. We’re essentially repeating our original blink code format along with the serial data but for each digital pin so that it cycles through and we can see each individual color turning on and off by itself. We’re going to do this with digital write red hi serial.write LN red delay 1,000 digital write red low digital write LN space delay 1,000.
We’re putting in the blank space so that when nothing is actively on then no data will be written to a serial monitor. And as a result, our data will be more clear. We’re going to repeat these six lines of code for both the green and blue pins. In the end, your code should look like this. Now we can compile and upload our code as well as open the serial monitor.
You should see each individual color turn on and off one after the other on the LED. And the serial monitor should reflect this as well. We can also control the individual colors on the RGB LED with push buttons. Let’s build a circuit with three buttons that will turn each color on and off similar to our original push button example from the earlier segment. Keeping the RGB LED hooked up as before insert three push buttons into the breadboard. Connect one pin on the button to 5V and then the pin directly next to it to ground with a 10k resistor.
Then, connect the pin across from ground to a digital pin on the Arduino. We’re going to connect our buttons to digital pins 5, 6, and 7 right next to the RGB LED pins. Next, we’ll write our code so that each button will control one of the three colors on the LED. Just like we did with our original button example, we’ll be using state changes to keep track of whether or not a button has been pressed. First, we’ll begin by declaring each pin for the LED and buttons with their pin numbers and as constant integers.
We can denote the corresponding color for each with the first letter of each color as shown. Then we’ll set up the states for each button as integers and equaling zero also denoting them with the first letter of their respective color. We’re also going to take steps to set up our serial monitor data as well. Much like we did in the first button example, we’re going to use states to hold this data. Create integers called R last button state, G last button state, and B last button state.
After this though, we’re going to approach the code in a different way than we have before. Previously, we’ve written out each piece of code for each peripheral individually since we haven’t been trying to control more than two objects. However, if we were to do this with three buttons, three LEDs, and state changes, our code would be long, messy, and inefficient. Instead, we’re going to use arrays. Arrays allow you to index variables.
What this means in this case is that we can create an array called LEDs and have it contain the three LED pins that we’ve set up. We can do the same thing for the buttons and button states. Since there are three variables in each of these arrays, their indexes will match and be able to correspond to each other later in the code. For example, the red LED will be indexed first in the LED’s array. And the red button and red button state will also be indexed first in their respective arrays.
This will begin to make more sense once we write our loop portion. For now, let’s create our arrays. Our first array will be for the LEDs. Begin with constant integer LEDs brackets = R LED G LED B LED. Making note of the syntax. You’ve just written your first array. Continuing with the button array, we’ll set it up as constant integer buttons = R button G button B button. And finally, the states array as int states = R button state G button state and B button state. And then create an array called “last button states” that include R last button state, G last button state, and B last button state.
The LED and button arrays are set up as constant integers since they will not be changing value, and the states are conversely set up as regular integers since they will be changing value depending on the button status. We’re going to create one last integer called count and have it equal 3. This will be a placeholder integer during the setup and loop portions of the code to refer to the fact that we’re utilizing arrays that have three objects. Now we can move on to the setup.
Arrays generally require for statements. Essentially, you setup an integer to be equal to the arrays index count that is then called in the code. In the setup we’ll create a for statement with the integer I equaling 0. I less than count, which is the integer we just set up to equal 3, and I ++, meaning that it will count three places in the index. Then inside the for statement we can do our normal setup for the LEDs and buttons, enter pin mode, and insert the LEDs array with I as a placeholder and set them up as an output. Then, enter pin mode and insert the buttons array with I as a place holder in the brackets and set them up as an input.
This allows for all of the buttons and the LEDs to be defined as inputs and outputs respectively all at once, rather than having to enter individual pin mode declarations for each one. After our pin modes, we’re going to open up serial communication with serial.begin 9600. Now it’s time for our loop. As a reminder, our goal is for each of our buttons to turn on one of the three colors with the RGB LED. We also want to print the status of the buttons to the serial monitor.
To begin we’re going to write a for loop that is identical to the one we used in the setup using the integer I, where I will equal 0, be less than count, and end with I ++. We’re going to nest our entire loop inside this for statement, since we’ll be working with the arrays that we declared earlier. If you remember the example button code, then this code structure will feel very familiar. First, we’re going to have the button states array set to equal the digital read of the buttons array with I acting as a placeholder for both arrays indexes. Then, we’ll write our if else statement for the button states to control the LED’s on or off status.
We’ll do this with if button states = = hi. Digital write LEDs hi with I as a placeholder for the indexes. Else digital write LEDs low. And that’s everything for the button and LED’s portion of the code. As you can see, using arrays keeps things nice and neat. Now we can move on to the serial portion of the code. We’ll begin with an if statement. If button states is not equal to last button states with I acting as the index placeholder for both, then serial.print red equals expressed as a string followed by serial.print LN button states index 0. Let’s take a moment to see what’s going on here.
We’re printing a string as we’ve done previously. But then for the button state data we’re printing a specific index from the array. In this case, the button state that corresponds with the button that controls the color red for the LED. Why 0 and not 1 then since that state is the first index in the array? Arrays count in binary, which means that the first index is 0 followed by 1, 2, 3, et cetera. This counting scheme will continue for our next two pieces of data that we print.
To print the green and blue status we’ll write serial.print green equals expressed as a string followed by a serial.print LN button states index 1. And then serial.print blue equals expressed as a string followed by serial.print LN button states index 2. And there we have our serial data portion. Right after this if statement, we’ll enter the line last button states equals button states with I acting as the index placeholder. This sets both arrays to hold the state chain’s data that we need to write our serial data. And this will end our loop.
We can then compile and upload our code and then open the serial monitor. As you press the buttons, you should see the different colors in the RGB LED turn on and the button states print to the serial monitor. You can also hold multiple buttons down to mix the colors together. This code was the most complicated that we’ve looked at so far, and it involved a lot of work at the beginning to set up our arrays. As you can see though, arrays really simplify the setup and loop. Arrays are also quite versatile.
As you saw, we can call on the entire array or individual indexes depending on your code’s needs. Often with LED projects, you’ll be dealing with many LEDs. And by using arrays you can keep your code organized and your loop shorter than if you were to write statements for each individual component attached to a pin on the Arduino.