28/10/2024
Hello, my friends.
Are you ready for learning?
Today we are going to learn about the RGB LED.
If you want to add red, blue, green, or any color to your project, you can use just one LED. This LED has four legs (RED, GND, GREEN, BLUE), and you can control the color concentration to create new colors.
To use this item, we need to learn about PWM pins. PWM, or "Pulse Width Modulation," is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. Why do we need the analog output? Because, for example, to make the RGB LED glow red, we should output three values (red, green, blue) (255, 0, 0). This way, we can control the light in different colors.
For example, we will design a traffic light using one LED. We need:
220Ω resistor x3
RGB LED x1
Test board x1
Arduino UNO x1
Connected wires x4
First Step: Connect the Pins with Arduino
Red leg --> Pin 3 (any pin with PWM)
Green leg --> Pin 5 (any pin with PWM)
Blue leg --> Pin 6 (any pin with PWM)
Don't forget to connect the resistor between Arduino and the LED legs.
Second Step: The Code for Programming
Open the Arduino IDE and write this code:
red 3
green 5
blue 6
void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop() {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 0);
delay(5000);
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
delay(500);
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 0);
delay(3000);
}
.
.
By Eng.Wajed