Build and code a circuit to control an LED colors and brightness. An RGB LED is an LED that can emit light of any color depending on the combination of the colors Red, Green and Blue (RGB).
You will use 3 potentiometers, one for each color of the LED. According to each color brightness you can make any color.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 30 min.
- 3 Resistances of 220
- 3 Potentiometers 10k
- 1 Build&Code UNO
- 1 Protoboard
- 1 RGB LED
What is an RGB LED?
An RGB LED is an all-in-one LED with the 3 basic colors (Red, Green and Blue). In other words, it’s the union of three LEDs of the basic colors (red, green and blue) in one encapsulation. Depending on the voltage you provide to each of the LED pins you can display any color you want.
An RGB LED has 4 pins, 1 for each color (3 pins), plus 1 pin where the electricity goes in and goes out, depending if it is common anode (the largest pin is where the electricity goes in) or common cathode (the largest pin is where the electricity goes out).
Before each of the RGB LED pins you must set a resistance with the correct value so the LED doesn’t break. In this case, you will put a 220 ohms resistance.
What is a potentiometer?
A potentiometer is a device that allows you to manually modify its resistance. You can modify it between a minimum value known as Rmin (normally 0 ohms) and a maximum value, known as Rmax (normally: 5k, 10k or 20k ohms).
When you turn around a potentiometer between the minimum and the maximum values you are proportionally modifying the inner resistance value.
Usually, a potentiometer has 3 pins. 1 pin at one end is related to the value of the Rmin minimum resistance, and the other pin at the other end is related to the Rmax maximum resistance. The 3rd pin is related to the variable resistance value, and this value is the result of the potentiometer position, if it’s closer to the Rmin or to the Rmax.
Both pins at the end of the LED must be connected to the Build&Code UNO 5V and GND, and the 3rd pin (which value depends on the potentiometer movement) should be connected to the Build&Code UNO analog pins.
Like all of the analog sensors, the potentiometer will send information to the analog input. The information can be from 0 to 1023. When the potentiometer is at its minimum value it will send a 0, when it is at its maximum value it will send a 1023, and if it is at its middle value, it will send a 516.
- The protoboard receives the electricity from the 5V pin of the Build&Code UNO and then goes back to the protoboard from the GND pin of the Build&Code UNO. All grounds of the circuit must be connected to each other so they have the same GND value. In the image, the GND is represented with a black cable, where all the components are connected to each other and to the GND board. The yellow cable represents the 5V, that supply each of the potentiometers.
- Connect each of the potentiometers central pins to a different analog pin, because the potentiometer is an analog sensor. In the image, you will see 3 gray cables connecting the potentiometer central pins with the 3 analog inputs.
- Each RGB LED pin must be connected to a PWM digital pin, which will make each LED color shine, more or less in order to produce any color, according to the readings of the sensor. The image shows 3 blue cables connecting the RGB LEDs to 3 digital outputs, that work as PWM (9, 10 and 11).
You can do this project using the Arduino program or a visual programming software by blocks compatible. Below you will find the necessary code.
You will write a code that is continuously reading the potentiometers information. According to the readings of each sensor you will make each LED color shine more or less.
Each potentiometer will make readings from 0 to 1023, depending on how much you have turned it around.
The digital PWM pin will work as an output pin, but instead of giving a constant high or low value, it can measure and provide middle values, this way the LED can have different levels of brightness.
For example, if an LED turns on and off for 100 times in 1 second, and 50% of the times is on and the other 50% is off it means that the human eye will perceive the brightness at 50%. But if the LED is off most of the time (for example, 75% off and 25% on) then the human eye will detect less brightness.
The PWM has a 256 states resolution. It goes from 0 to 255. If you write 15, it will shine very low; if you write 127, its brightness will be at 50%; if you write 255, the brightness will be at 100%.
To relate the analog input of the potentiometer with the PWM digital pin, you have to divide the two maximum values:
This means that if you divide the sensor value by 4 then you will be making a lineal relation between the potentiometer turn and the brightness of the LED.
This chart is very useful to know how to produce any color by combining the 3 RGB colors (Red, Green and Blue).
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- Open the Arduino program and copy the following program in it:
Code for the visual programming software by blocks compatible
- Download and install the program.
1.1 Open the software.
1.2 Configure the program to save code into the Build&Code UNO board. You will find the instructions in the Arm Robot First Steps guide.
- Open the program and copy the following code. Use the following image as a guide:
RESULT OF THE EXERCISE
The RGB LED connected to the Build&Code UNO board through the protoboard’s circuit, will change its color according to the movement of the three potentiometers.