In the previous tutorial , we discussed analog input when using Arduino. So far, we've covered four of the five ways controllers can interface and interact with other electronic components and devices. This includes digital output, digital input, analog output and analog input.
In this tutorial, we discuss how to interface RGB LEDs using Arduino . RGB LEDs are a combination of three LEDs – red, green and blue – all packaged as one LED. These LEDs are used in a variety of industrial applications such as status/power indicators, plumbing fixtures, digital signage boards, LED rings, home decor lights, and large outdoor displays.
By controlling the intensity of each individual LED, almost 16 million colors can be produced. RGB LEDs are used in the construction of direct-view light-emitting diode (DV LED) displays that are widely used in the digital signage industry.
RGB LED
As mentioned in the introduction, an RGB LED is a combination of three LEDs (red, green and blue) in a single package. Electronic display devices use additive mixing of colors to produce different colors.
In additive mixing, red, green, and blue are the primary colors, which are colors that cannot be created by mixing others. But by mixing primary colors, all other colors can be generated.
The human eye's color receptors are the most sensitive to these. Thus, by additively mixing these colors, the largest matrix visible to the human eye can be generated.
The color space generated by mixing primary colors is called the RGB color space. Here are some examples:
- When mixing red and green, shades of yellow and orange are created.
- When mixing green and blue, cyan tones are created.
- When mixing red and blue, shades of purple and magenta are created.
- By mixing the three colors, shades of gray are created.
- When all three colors are fully saturated, white light is created.
- The absence of all three colors creates black light in the appropriate setting.
In a large external DV LED display, each RGB LED functions as a pixel.
By RGB LED, almost any color can be produced. So, let's assume that the intensity of each color is changed on an 8-bit scale, which would equate to 16,777,216 colors (256 x 256 x 256) or approximately 16 million colors that could be produced.
In an RGB LED, three LEDs of red, green and blue color are closely packed. This is why when these LEDs emit light, the light is not seen individually, but as an additive mixture of three lights.
The intensity of each colored LED can be controlled by applying PWM signal to them. If the pulse width of this signal at each LED is changed by 8-bit values, the light intensity of each LED also changes on the same scale.
Approximately 16 million different colors can be produced on an RBG LED.
Types of RGB LEDs
There are two types of RGB LEDs: common anode LED and common cathode RGB LED.
1. Common Anode LED
In common anode LED, all three LEDs have a common anode but have different cathodes. These LEDs interface with a controller so that the controller absorbs current through them.
In this configuration, the PWM signal must be applied to the cathode terminals to vary the intensity of each LED. When the pulse width of the PWM signal increases, the LED intensity decreases and vice versa.
2. Common cathode RGB LED
With common cathode RGB LED, all three LEDs have a common cathode but their anodes are different. These LEDs interface with a controller so that the controller supplies current to them.
In this configuration, the PWM signal must be applied to the anode terminals to vary the intensity of each LED. When the pulse width of the PWM signal increases, the LED intensity also increases and vice versa.
Both types of RGB LEDs are four-terminal devices with this pin configuration:
Common anode and common cathode RGB LEDs can be easily distinguished by a multimeter. When using a multimeter, do so in continuity mode.
- Place the positive lead of the multimeter on the longest wire and the negative lead on any other terminal. If the respective red, green or blue LED lights up, then it is a common anode LED.
- Place the negative lead of the multimeter on the longest wire and the positive lead on any other terminal. If the RGB LED lights up this time, it is a common cathode RGB LED.
- If none of the connections work, one or more of the RGB LED's LEDs may be damaged.
Nonlinearity
It is important to note that all three LEDs of the RGB LED have different VI characteristics. For example, they have different direct voltages and slightly different VI curves. This is due to the differences between the types of LEDs, according to the color they emit.
The electrical characteristics of the red, green and blue lights on the RGB LED are listed here:
The VI curve of LEDs is exponential and not linear. Furthermore, the current through each LEAD peaks at different voltage levels and increases at different forward voltages. Therefore, in a commercial application, all these factors must be taken into account because the intensity of each LED has to be varied to produce different colors.
In commercial displays, the intensity of each LED varies depending on the true analog signals provided through digital-to-analog converters. Additionally, the nonlinearity of the light intensity (or lux units) of the red, green, and blue LEDs is corrected by gamma correction and other color calibration techniques.
For simplicity, we will not take into account this non-linearity of RGB LEDs. Instead, we will assume that the light intensity of each colored LED varies equally with respect to the applied forward voltage, increasing directly from the 0V level. Furthermore, the direct region curve is linear for all LEDs by the proportionality of one. This is just to simplify our controller program. But remember, this is just an assumption.
RGB LED Applications
RGB LEDs are used in various applications, such as the following:
1. Indicators
2. LED Rings
3. Decorative lights
4. Hydraulic installations
5. Digital signage boards and billboards
6. Outdoor lights and light shows
Controlling the RGB LED with Arduino
Most Arduino boards do not have a true analog output or, if they do, they only offer one or two channels. However, most Arduino boards can output PWM signals. For example, the Arduino UNO can output a PWM signal using the analogWrite function on pins 3, 5, 6, 9, 10 and 11.
Arduino boards can also output PWM signals on any pin using timers/counters or hitting bits.
PWM signals are an approximation of analog voltages. By applying a PWM signal, the same effect can be achieved on an RGB LED as if applying analog voltage. Since the PWM signal generated using the analogWrite function can vary the duty cycle, on an 8-bit scale, the RGB24 color profile can be imitated by applying the PWM signals to the red, green and blue LEDs of the RGB LED.
This way, colors can be produced from an RGB LED that directly compares to colors in the RGB24 color space — where each color is represented by 24-bit hexadecimal values.
The Arduino-based RGB LED driver revenue
In this recipe, we will design an RGB LED driver using Arduino. This RGB LED driver will not involve any correction for the non-linearity of the red, green and blue LEDs of an RGB LED.
Required components
1. Arduino UNO x1
2. RGB LED x1
3. 330 Ohm Resistor x1
4. Test board x1
5. Male-to-male bonding wires or connecting wires
Circuit Connections
First, test whether your RGB LED is common cathode or common anode. The RGB LED used in this project is the common cathode. Therefore, the common cathode terminal (the second terminal identified by the longer wire) must be connected to ground through a 330 Ohm series resistor.
The anode terminals of the red, green and blue LEDs of the RGB LED are connected to PWM pins 3, 9 and 10 of the Arduino UNO. These pins can generate a PWM signal using the analogWrite function. The breadboard is powered by the common ground and 5V power rail of one of the ground pins and 5V pin of the Arduino UNO respectively.
Circuit Diagram
Arduino Sketch
`
How the project works
The common cathode RGB LED interfaces with the Arduino UNO, so the Arduino's PWM pins supply current to the RGB LED's color LEDs. From the PWM pins, a PWM signal is applied to the red, green and blue LEDs using the analogWrite function.
The function generates a PWM signal with a duty cycle that can be changed on an 8-bit scale. This means that the intensity of each LED can be varied in 256 steps or on an 8-bit scale. Thus, colors from the RGB24 color space can be produced on an RGB LED.
These colors can be directly compared to colors in an RGB24 color space, where each color is represented by a 24-bit hexadecimal value. For example, to produce pure red color in an RGB LED (where the color is represented by #FF0000), the intensity of the RGB lights must be 255 — so 0 and 0 respectively.
As the Arduino pins supply current to the colored LEDs, the light intensity of each LED increases by increasing the pulse width applied to the PWM signal.
If the light intensity of each LED is directly proportional to the pulse width (here we ignore the non-linearity of the RGB LED, such as differences in forward voltages and VI curves), then applying PWM signals with duty cycles of 100%, 0% and 0% on the red, green and blue LEDs will produce pure red light.
For this to occur, however, the duty cycle of the PWM signals on the red, green, and blue LEDs must be set to 255, 0, and 0, respectively.
Similarly, to obtain pure green light, PWM signals with duty cycles defined by 0, 255 and 0 must be applied to the red, green and blue LEDs. respectively.
To obtain pure blue light, PWM signals with duty cycles defined by 0, 0, and 255 must be applied to the red, green, and blue LEDs, respectively.
To obtain pure white light, PWM signals with duty cycles defined by 255, 255 and 255 must be applied to the red, green and blue LEDs respectively.
When all LEDs are off, it represents black color. The other colors of the RGB24 color space can be produced by defining the duty cycle of PWM signals for red, green, and blue LEDs and directly comparing their 24-bit hexadecimal values.
In this project, 25 different colors are generated from the RGB LED at intervals of one second each. These colors are listed below by their hexadecimal RGB values and the duty cycle values used to produce them.
In this project, the following colors are produced in the RGB LED:
Programming guide
Pins 3, 9 and 10 which are connected to the R, G and B terminals of the RBG LED are defined in the sketch as global variables R, G and B. As we are using the analogWrite function to generate the PWM signals at the LED terminals RGB, there is no need to configure the pins using the pinMode function. Therefore, the setup function is empty.
int R = 3;
internal G = 9;
internal B = 10;
empty configuration {
}
In the loop function, different colors are produced on the RGB LED by applying the PWM signals of the different duty cycles to the R, G and B terminals of the RGB LED.
The duty cycle of the PWM signals to the RGB LED terminals is directly proportional to the RGB values of those colors. These PWM signals are generated by calling the analogWrite function. The delay function is called to provide a one-second interval between each color flashing.
For example, to produce a red color, this code is used:
analogWrite(R, 255);
analogWrite(G, 0);
analogWrite(B, 0);
delay(1000);
Similarly, to produce green color light, this code is used:
analogWrite(R, 0);
analogWrite(G, 255);
analogWrite(B, 0);
delay(1000);
And to produce blue light, this code is used:
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 255);
delay(1000);
To produce different shades in the RGB24 color space, PWM signals are generated with a duty cycle directly proportional to their RGB values.
For example, to produce a shade with hexadecimal value #2ecc71, with RGB value 46, 204, 113, this code is used:
analogWrite(R, 46);
analogWrite(G, 204);
analogWrite(B, 113);
delay(1000);
To produce a white color light, this code is used:
analogWrite(R, 255);
analogWrite(G, 255);
analogWrite(B, 255);
delay(1000);
To display black, all LEDs must be turned off by setting a duty cycle for all PWM signals to 0%. Since all LEDs are off, the color black will be displayed at appropriate settings – such as in the absence of any backlight on a display board.
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
delay(1000);
Note: The loop function code keeps repeating itself for an infinite number of times, flashing different colors on the RGB LED in the defined sequence until the Arduino UNO is turned on.
Try it yourself
In this RGB LED driver, analogWrite function is used to generate PWM signals to produce different colors in RGB LED. Since the duty cycle of the PWM signal can be varied using the analogWrite function (between 0 to 100% on an 8-bit scale), it is impossible to correct the nonlinearity of the RGB LED.
However, if PWM signals are generated by timer/counter or bit-banging (where the ON and OFF period of PWM pulses are controlled with microsecond precision), PWM signals can be generated by matching the duty cycle with the advance. . voltages of the red, green and blue LEDs.
The duty cycle of each PWM signal can be varied on an 8-bit scale for each LED by varying the output voltage between the forward bias voltage and the voltage at which their currents increase exponentially on their respective VI curves.
With a little more effort, the PWM signal can be generated to mimic the real exponential curve of the VI characteristics of each LED using the mathematical library and an exponential function from it. You may need to check the manufacturer's RGB LED datasheet for the exact VI characteristic curves and each specific RGB LED light below.
In the next tutorial, we will discuss interfacing a seven-segment display (SSD) with Arduino.
Demo Videos
(tagsToTranslate)Arduino