RGB LED (common annode) controlled by potentiometers and Arduino


In this post I’m going to play with an RGB LED. This device combines three different LEDs (red, green and blue) and due to the additive properties of the light, changing the intensity of each primary color we are able to get a broad array of colors. For controlling the brightness of each LED, in this small project I’m going to use three potentiometers, the signal of each one will be read by the Arduino as an input.

1. An Arduino board with an USB connection and the Arduino IDE installed.
2. 3 potentiometers to control each color of the RGB LED, in this case they are 2k Ohm linear potentiometers.
3. An RGB LED, I’m using a cheap RGB LED with common anode from eBay. Be aware that this circuit will not work with a RGB LED with common cathode.
4. A breadboard and wires to connect all components.

Circuit diagram:

RGB LED (common anode) controlled with Arduino and 3 potentiometers
RGB LED (common anode) controlled with Arduino and 3 potentiometers

Connections between all components are shown in the previous image, potentiometers don’t have polarity so there is no problem how you connect the power and the ground. The RGB LED does have polarity so be careful to know to which pin you have to connect the power. Finally, the most important thing in this circuit is to be careful and be sure to which pin you connect each potentiometer and LED leg.

Arduino code:
For the RGB LED with common anode, this is the code that Arduino should run:

The first 3 lines are the pins for the RGB LED and between lines 5 to 7 we do the same for each potentiometer. We need to declare variables for what we read from each potentiometer (lines 9 to 11)  and for what we are going to write to the LEDs (lines 13 to 15). Then, in the void setup LED pins are set as outputs and potentiometer pins are set as inputs.

Now in the void loop the first thing is to read the value from the potentiometer, and the next step is to convert the value from the potentiometer to a brightness value for each LED. In this case we have to do something similar that what we did when we converted the reading from the potentiometer to a position from 0 to 180 degrees for the micro servo. In this case we need to write on each LED a number between 0 and 255, we know that:
– The brightness of the LED is set sending a number between 0 to 255.
– The potentiometer reading (that is, what the analogRead() command gets) is a number from 0 to 1023.

We have two known situations: when the potentiometer reading is 0 which will set a brightness of 0, that is, no light. In the same way, when the potentiometer reading is 1023 the brightness should me maximum, that is 255. This gives us two points as in the following plot:

Potentiometer read vs LED brightness
Potentiometer read vs LED brightness

We want a linear change when we use the potentiometers so we use now the equation of a line to figure out how to calculate the brightness of the LED:
– The slope of the line is calculated as m=(y_2-y_1)/(x_2-x_1) where our y values are the LED brightness and our x values are the potentiometer readings. This translates to m=(255-0)/(1023-0)=255/1023.
– The equation of a line is y-y_1=m(x-x_1) which in our case, using the first point (0,0) very easily leads us to y=255/1023(x-0).

Using the defined variables x = potRead_LED and y = writeValue_LED, the command to use will be: writeValue_LED=(255/1023)*potRead_LED. We need to do this for each color and that’s written between lines 32 to 34. Remember that you need to write the point after the values inside the division to tell the Arduino to do a float division even if later the value is truncated to an integer.

Final set of instructions is just to send the value for each LED and set the correct brightness for each one of them.

Here is a video which shows how the RGB LED change between different colors:

One comment

Leave a Reply