An RGB LED is a versatile electronic component that combines red, green, and blue light-emitting diodes to produce a wide spectrum of colors. By adjusting the intensity of each individual color, it can create various hues and shades, making it popular in decorative lighting, signal indicators, and display panels. RGB LEDs are commonly used in electronics projects, including those involving microcontrollers like the Arduino UNO.
Pin Number | Description | Common Type |
---|---|---|
1 | Red Anode/Cathode | Anode/Cathode |
2 | Green Anode/Cathode | Anode/Cathode |
3 | Blue Anode/Cathode | Anode/Cathode |
4 | Common Cathode/Anode | Cathode/Anode |
Note: The common pin is either the common anode or common cathode, depending on the type of RGB LED.
R = (V_supply - V_LED) / I_LED
.// Define the RGB LED pins
const int RED_PIN = 9; // Red pin connected to digital pin 9
const int GREEN_PIN = 10; // Green pin connected to digital pin 10
const int BLUE_PIN = 11; // Blue pin connected to digital pin 11
void setup() {
// Set the RGB LED pins as output
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
// Set the color to purple (Red + Blue)
analogWrite(RED_PIN, 255); // Red at full intensity
analogWrite(GREEN_PIN, 0); // Green off
analogWrite(BLUE_PIN, 255); // Blue at full intensity
delay(1000); // Wait for 1 second
// Set the color to aqua (Green + Blue)
analogWrite(RED_PIN, 0); // Red off
analogWrite(GREEN_PIN, 255); // Green at full intensity
analogWrite(BLUE_PIN, 255); // Blue at full intensity
delay(1000); // Wait for 1 second
}
Note: The above code assumes a common anode RGB LED. For a common cathode, set the pins to LOW to turn on the LED.
Q: Can I connect an RGB LED directly to an Arduino without resistors? A: No, you should always use current-limiting resistors to prevent damaging the LED and the Arduino.
Q: How do I produce white light with an RGB LED? A: Set the PWM values for red, green, and blue to full brightness to mix the colors into white.
Q: What is PWM and how does it control the LED brightness? A: PWM stands for Pulse-Width Modulation. It controls the brightness by varying the duty cycle of the power supplied to the LED.