The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green and blue. In this lesson, we will show how to use a RGB (Red Green Blue) LED with an Arduino.
RGB Module Specification
Color: red, green and blue
Input: digital level
How do RGB LEDs work?
At first glance, RGB (Red, Green, Blue) LEDs look just like regular LEDs, however, inside the usual LED package, there are actually three LEDs, one red, one green and yes, one blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want.
How to create different colors?
The reason that you can mix any color you like by varying the quantities of red, green and blue light is that your eye has three types of light receptor in it (red, green and blue). Your eye and brain process the amounts of red, green and blue and convert it into a color of the spectrum.
In a way, by using the three LEDs we are playing a trick on the eye. This same idea is used in TVs, where the LCD has red, green and blue color dots next to each other making up each pixel.
You can create one of those three colors – red, green or blue – by activating just one LED.
For example, if you want to produce blue, you activate the blue LED and turn off the other two.
If we set the brightness of all three LEDs to be the same, then the overall color of the light will be white. If we turn off the blue LED, so that just the red and green LEDs are the same brightness, then the light will appear yellow.
We can control the brightness of each of the red, green and blue parts of the LED separately, making it possible to mix any color we like.
Black is not so much a color as an absense of light. So the closest we can come to black with our LED is to turn off all three colors.
The brightness of an LED is proportional to the current going through it, but it would be rather difficult to use a microcontroller to accurately control the current flowing through an LED. Fortunately, human vision has a nice phenomenon called persistence of vision. Persistence of vision is the phenomenon where an image that is seen for only a fraction of a second will continue to be “seen” by your brain even after the original image has vanished or moved. This this the same principle behind film and television, where a rapidly changing image tricks your brain into seeing continuous motion. By turning our LED on and off rapidly, we can trick the brain into seeing an “average” value of brightness based on the duty cycle of the driving PWM signal.
Pulse-width modulation (PWM) is the practice of modulating the duty cycle of a signal, used in this application to control the average power sent to each LED. In the following figure, we show three different duty cycles, first with 50% duty cycle, then 10% and 90% duty cycle. During the 10% duty cycle, the signal is at the logic high level for only a brief time each cycle, but with 90% duty cycle, most of the signal’s period is spent at logic high level. If the frequency of the signal is fast enough, then there will be no visible flicker, and the LED’s brightness will be proportional to the signal’s duty cycle.
RGB LED Color Control
RGB stands for the red, green, and blue color channels and is an industry color standard. RGB displays various new colors by changing the three channels and superimposing them, which, according to statistics, can create 16,777,216 different colors. If you say the color displayed doesn’t completely match a natural color, then it almost certainly cannot be differentiated with the naked eyes.
Each of the three color channels of red, green, and blue has 255 stages of brightness. When the three primary colors are all 0, “LED light” is the darkest, that is, it turns off. When the three primary colors are all 255, “LED light” is the brightest. When superimposing the light emitted by the three primary colors, the colors will be mixed. However, the brightness is equal to the sum of all brightness, and the more you mix, the brighter the LED is. This process is known as additive mixing.
Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1
OSOYOO Magic I/O Shield for Arduino x1
RGB Module x 1
OSOYOO 4-Pin PNP Cable x 1
USB Cable x 1
PC x 1
First, please plug Osoyoo Magic I/O shield into UNO board:
Then connect the RGB module to the RGB port of the Magic I/O shield with a 4-pin PNP cable as below:
Notice: Shut off your battery or Unplug your power adapter when upload sketch code to Arduino.
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.
Open the Graphical Programming softwareMixly, if Mixly is not English, you should change the language first:
You can download the code directly, then click “Open” in Mixly to choose the code you download:
Drag the first block “procedure do” to the blank space, and edit “procedure” to “RED“;
Click “In/Out” block;
Drag “DigitalWrite PIN#” to fit “do RED” block, and edit the parameter to “DigitalWrite PIN# 11 Stat HIGH“;
Repeat step 3 & step 4, fit the three “DigitalWrite PIN#” blocks, and edit the other two blocks parameter to “DigitalWrite PIN# 12 Stat LOW“ and “DigitalWrite PIN# 13 Stat LOW“.
Right click the “do RED” block;
Duplicate the existing blocks;
Edit the parameter as the picture shows;
Refer to the above step 1-3 to finish the blocks, remember to edit the parameter as the picture shows.
Click “Functions” block;
Drag “do RED” block to the blank space;
Click “Control” block;
Drag “Delay” to fit “doRED” block(use default delay time 1000 ms);
Refer to the above step 1-4 to finish the blocks.
The whole program blocks are as following:
After above operations are completed, do as following:
Click Save after programming is done.
Select the board type and serial port before uploading. In our case, we use UNO board, so just select Arduino/Genuino Uno. If you use a Mega2560, select Arduino/Genuino Mega or Mega2560.
Select the serial device of the Arduino board from the COM menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.
Next, click upload to send the program to Arduino board. If uploading fails, check connection according to the prompts.
Finally, the status will change to ‘Upload success!’.
A few seconds after the upload finishes, you should see the RGB LED flash circularly in red, green, and blue colors.