An RGB LED is a Light-Emitting Diode that combines Red, Green, and Blue light sources in a single package. By adjusting the intensity of each color, it can produce a wide spectrum of colors, making it a versatile component for color mixing and decorative lighting. Common applications include mood lighting, display panels, and DIY electronics projects with microcontrollers like the Arduino UNO.
Pin Number | Description | Color |
---|---|---|
1 | Common Anode/Cathode | - |
2 | Red Anode/Cathode | Red |
3 | Green Anode/Cathode | Green |
4 | Blue Anode/Cathode | Blue |
Note: The common pin may be either anode or cathode, depending on the type of RGB LED (common anode or common cathode).
// Define the RGB LED pins
const int RED_PIN = 11;
const int GREEN_PIN = 10;
const int BLUE_PIN = 9;
void setup() {
// Set the 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 example assumes a common anode RGB LED. For a common cathode, invert the PWM values (use 255 - value
).
Q: Can I connect an RGB LED directly to an Arduino without resistors? A: No, you should always use current-limiting resistors to prevent damage to both the LED and the Arduino.
Q: How do I make white light with an RGB LED? A: To create white light, you need to mix red, green, and blue at full brightness. However, the resulting white may not be perfectly balanced due to differences in LED color intensity.
Q: Can I use a single PWM pin to control all three colors? A: No, you need individual PWM control for each color to mix colors effectively.