How to use RGB LED with Arduino
Introduction:
Getting Started RGB LED with Arduino – LED operation is the most introductory exercise for Arduino, but before, everyone may have used single-color LEDs, such as red, green, blue, white, etc. In fact, there is also a kind of LED light that can express various colors. We call it RGB LED. You can find a single 4-pin RGB LED, or you can choose a more convenient RGB module. In this article, we will use an RGB LED Full-Color module with arduino.
Amazon links:
Arduino Nano USB-C Type (Recommended)
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
RGB Colors:
RGB stands for the red, green and blue color channels and is the industry color standard. RGB displays various new colors by changing three channels and superimposing them. According to statistics, 16,777,216 different colors can be created. If the colors shown do not exactly match natural colors, they almost certainly cannot be discerned with the naked eye.
Each of the three color channels of red, green and blue has 255 brightness levels. When all three primary colors are 0, the “LED light” is the dimmest, that is, turned off. When all three primary colors are 255, the “LED light” is the brightest. When the light emitted by the three primary colors is superimposed, the colors will mix. However, brightness is equal to the sum of all brightnesses, and the more mixed, the brighter the LED. This process is called additive mixing.
RGB LED Full-Color Module:
Generally common 5mm RGB LED modules are common cathode, which means there is only one GND pin on the board and no VCC pin; if it is common anode, it is replaced with VCC pin. For this example, we are using the version with GND pin.
The module has 4 legs in total, and the other 3 legs are R, G, and B, which correspond to the three primary colors of red, green, and blue. Each primary color can have a change of 0-255, so when combined, there are 16.77 million colors. It’s full color.
RGB LED Full-Color Module with Arduino Circuit:
As you can the circuit diagram is very simple, I connect the “-“ pin of the RGB LED Full-Color Module to Arduino nano GND, B pin of the RGB led module is connected with Arduino nano pin D11, G pin of the RGB LED Full-Color Module is connected with Arduino nano pin D10, and finally the R pin of the RGB LED Full-Color Module is connected with Aruduino nano D9.
The program is very simple. As long as we give different PWM values ​​to the three pins R, G, and B respectively, we can make the LED display the color we want.
Arduino RGB LED Full-Color Module Color Changing Code:
The first example is very simple. We let the LEDs take turns to display three colors: true red, true green, and true blue, with a time interval of 1 second.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
const int Red = 9; const int Green = 10; const int Blue = 11; void setup() { pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(Blue, OUTPUT); } void loop() { analogWrite(Red,255); analogWrite(Green,0); analogWrite(Blue,0); delay(1000); analogWrite(Red,0); analogWrite(Green,255); analogWrite(Blue,0); delay(1000); analogWrite(Red,0); analogWrite(Green,0); analogWrite(Blue,255); delay(1000); } |
Code Explanation:
This program controls a full-color RGB LED module, which can shine in different colors like red, green, and blue.
1 2 3 4 5 |
const int Red = 9; const int Green = 10; const int Blue = 11; |
First, it sets up the Arduino by telling it which pins are connected to the red, green, and blue parts of the LED. These pins are like the wires that connect the Arduino to the LED.
1 2 3 4 5 |
pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(Blue, OUTPUT); |
These lines of code tell the Arduino how to use the pins connected to the LED. Specifically, they set the pins named “Red,” “Green,” and “Blue” to be used as outputs. This means the Arduino will send signals through these pins to control the LED. By setting them as outputs, the Arduino can send electrical signals to the LED to change its colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
void loop() { Â analogWrite(Red,255); Â analogWrite(Green,0); Â analogWrite(Blue,0); Â delay(1000); Â analogWrite(Red,0); Â analogWrite(Green,255); Â analogWrite(Blue,0); Â delay(1000); Â analogWrite(Red,0); Â analogWrite(Green,0); Â analogWrite(Blue,255); Â delay(1000); } |
Then, it starts a continuous loop where it changes the colors of the LED. It turns on the red part to its brightest (255), and the green and blue parts off (0), making the LED red. After one second (1000 milliseconds), it turns off the red and blue parts, and turns on the green part, making the LED green. Then, it turns off the green and red parts, and turns on the blue part, making the LED blue. It waits for another second before starting the loop again, so it keeps changing colors in a loop.
Arduino RGB LED Full-Color Module Fade Effect Code:
This program is for an Arduino controlling an RGB LED full-color module, which can display many colors. The code creates a fading effect where the LED smoothly changes colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
const int Red = 9; const int Green = 10; const int Blue = 11; #define FADESPEED 5 void setup() { pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(Blue, OUTPUT); } void loop() { int r, g, b; for (r = 0; r < 256; r++) { analogWrite(Red, r); delay(FADESPEED); } for (b = 255; b > 0; b--) { analogWrite(Blue, b); delay(FADESPEED); } for (g = 0; g < 256; g++) { analogWrite(Green, g); delay(FADESPEED); } for (r = 255; r > 0; r--) { analogWrite(Red, r); delay(FADESPEED); } for (b = 0; b < 256; b++) { analogWrite(Blue, b); delay(FADESPEED); } for (g = 255; g > 0; g--) { analogWrite(Green, g); delay(FADESPEED); } } |
Code Explanation:
1 2 3 4 5 |
const int Red = 9; const int Green = 10; const int Blue = 11; |
First, it sets up the Arduino by telling it which pins are connected to the red, green, and blue parts of the LED. These pins are like the wires that connect the Arduino to the LED.
1 2 3 4 5 |
pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(Blue, OUTPUT); |
These lines of code tell the Arduino how to use the pins connected to the LED. Specifically, they set the pins named “Red,” “Green,” and “Blue” to be used as outputs. This means the Arduino will send signals through these pins to control the LED. By setting them as outputs, the Arduino can send electrical signals to the LED to change its colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
void loop() { Â int r, g, b; Â for (r = 0; r < 256; r++) { Â Â Â analogWrite(Red, r); Â Â Â delay(FADESPEED); Â } Â for (b = 255; b > 0; b--) { Â Â Â analogWrite(Blue, b); Â Â Â delay(FADESPEED); Â } Â for (g = 0; g < 256; g++) { Â Â Â analogWrite(Green, g); Â Â Â delay(FADESPEED); Â } Â for (r = 255; r > 0; r--) { Â Â Â analogWrite(Red, r); Â Â Â delay(FADESPEED); Â } Â for (b = 0; b < 256; b++) { Â Â Â analogWrite(Blue, b); Â Â Â delay(FADESPEED); Â } Â for (g = 255; g > 0; g--) { Â Â Â analogWrite(Green, g); Â Â Â delay(FADESPEED); Â } } |
This code creates a continuous color-fading effect for an RGB LED. Inside the loop, variables r, g, and b represent the intensity of the red, green, and blue colors respectively. This loop consists of several for loops, each of them controlling the intensity of a specific color. First, it increases the red color from minimum to maximum intensity, causing the LED to brighten in red. Then, it decreases the blue color from maximum to minimum intensity, fading out the blue light. Subsequently, it increases the green color from minimum to maximum intensity, introducing a green glow. Following this, it decreases the red intensity, dimming the red light. Next, it increases the blue intensity, bringing back the blue light. Finally, it decreases the green intensity, dimming the green light. Throughout this process, the delay(FADESPEED) function regulates the speed of color changes, ensuring a smooth transition between colors. This loop repeats continuously, creating an eye-catching and dynamic color fading effect for the LED.