Try reloading this page, or reviewing your browser settings
This segment builds on the digital RGB LED segment on how to use a common cathode RGB LED with analog controls; specifically potentiometers that are assigned to each color. The code allows the user to mix all three colors together and write the data for each potentiometer to the serial plotter.
- rgb led
- color mixing
- serial plotter
About this video
- Liz Clark
- First online
- 09 April 2019
- Online ISBN
- Copyright information
- © Liz Clark 2019
In this section, we’re going to continue working on RGB LEDs, this time using potentiometers and analog control. 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 some common cathode RGB LEDs from our last segment, and some potentiometers. Let’s get started.
We just use some push buttons to control our RGB LED by having the three individual colors be controlled by a button. We can also control our three colors with potentiometers, and as a result, utilize analog control, meaning that we can adjust the amount of each color that is showing up in our RGB LED. By doing this, we can essentially create a color mixer, where we can dial in unique colors that are achieved by using different amounts of red, green, and blue.
Let’s begin with setting up our circuit. First, we’ll have our common cathode RGB LED on our breadboard with the second lead attached to ground, and a 220 ohm resistor attached to leads 1, 3, and 4. We’ll then connect leads 1, 3, and 4 via the resistors to digital pins 3, 5, and 6 on the Arduino, since they are PWM enabled pins, and as a result, will be able to properly interpret the analog data.
Now we can connect our potentiometer. We’ll be using three of them. And if you remember from our analog segment, potentiometers have three pins that are connected to ground, data, and power. Connect the ground and power legs on the three potentiometers to the ground and 5 volt rails on the breadboard. And then connect the first potentiometer’s data pin to analog pin 0, the second potentiometer’s data pin to analog pin 1, and the third potentiometer to analog pin 2. This will complete our circuit setup.
We can now move to the Arduino ID to write our code. It’s going to have a very similar setup to our RGB LED button code, since we’ll be using arrays again.
First, we’ll begin by declaring each pin for the LED potentiometers 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. We can then create our arrays for the LEDs and potentiometers set up as constant integers.
For LEDs, we’ll create an array called LEDs and have a contain RLED, GLED, and BLED. And then for our potentiometers, we’ll create an array called pots, and have it contain R pot, G pot, and B pot.
Instead of states, we’re going to create value place holders to populate an array. If you remember from our other analog examples, we use an integer to hold the analog value of the potentiometer, and that integer is then converted to the PWM value range to be able to be sent to the output– in our case, an LED as data.
Since we’ll be gathering and sending analog data for three separate inputs and outputs, we’ll need three integers to hold this data, and these integers we put into an array. Create integers called Rval, Gval, and Bval– keeping our naming scheme for the different colors consistent– and have them all equal 0. Then create an array called values, and have it contain Rval, Gval, and Bval.
Finally, we’ll create one more integer called count, and have it equal 3, which will serve as our index placeholder for our arrays in the next portion of our code, just like we did with the button’s code. We can then move to the setup portion where we’ll declare our pin modes for the LED and potentiometer arrays.
As before, this will be nested inside a for statement. We’ll declare an integer called I to equal 0, have it be less than count, and then enter I plus plus. We’ll then put pin mode LED as output, with I being the place holder for the index, and pin mode, pots, input– again, with I being placeholder for the index. We’re going to be utilizing serial data again, so we’ll also enter serial dot begin 9,600 underneath the for statement to complete the set up portion.
Now for the loop. This will be very similar to our previous analog examples. First though, we’ll create a for statement to nest our loop that will be identical to the one we wrote for the setup. Again, using integer i, which would be our array index placeholder.
Next, we’ll utilize our values array to map the analog values to PWM digital values. We’ll begin with values equals analog read pots, both utilizing I as index place holder. Then we’ll map the analog values PWM values as we have in previous analog examples, but this time, utilizing the values array to hold those values.
Then we’ll send the potentiometer’s values to the LEDs by using analog right LEDs values– again, using I as the index placeholder for both arrays. This takes care of the code for writing the data to the LED.
Now we can take care of our serial communication portion of the code. Our goal is to see the value of each potentiometer. Instead of using the serial monitor, we’re going to use a serial plotter that we used for the photoresistor segment. The serial plotter allows for multiple lines to be charted out in real time. So we’ll be able to have a line of data for each potentiometer value.
To do this, we’ll enter serial dot print values index 0, just like how we used individual indexes in the button code. This will allow us to isolate the first index entry in values array, which corresponds with the potentiometer controlling the red color.
We’ll then enter a new line, serial dot print comma as a string. By putting this in for use with the serial plotter, it’ll separate our data lines. Now, we’ll enter a serial dot print commands for values index 1 and values index 2 with the comma separators between the index and two lines. You’ll end with serial dot print LN comma.
This wraps up our code. So we can go ahead and compile, upload, and open the serial plotter. Experiment with turning the three potentiometers to see all of the different colors you can mix together and how the different amounts of each color affect the LED. You will also see the plotter correspond with each potentiometer.