An RGB LED is a versatile electronic component that combines red, green, and blue light-emitting diodes in a single package. By adjusting the intensity of each color, it can produce a wide spectrum of colors, making it ideal for a variety of applications such as mood lighting, display panels, and decorative purposes. RGB LEDs are commonly used in electronics projects, including those involving microcontrollers like the Arduino UNO.
Pin Number | Color | Description |
---|---|---|
1 | Red | Anode or cathode for the red LED segment |
2 | Common | Common anode or cathode for all segments |
3 | Green | Anode or cathode for the green LED segment |
4 | Blue | Anode or cathode for the blue LED segment |
Note: The common pin may be either an anode or cathode, depending on the type of RGB LED (common anode or common cathode).
// Define the RGB LED pins
const int RED_PIN = 9; // Red LED pin
const int GREEN_PIN = 10; // Green LED pin
const int BLUE_PIN = 11; // Blue LED pin
void setup() {
// Set the LED pins as outputs
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, invert the PWM values (use 255 - value
).
Q: Can I connect an RGB LED directly to an Arduino without resistors? A: No, resistors are necessary to limit the current and prevent damage to both the LED and the Arduino.
Q: How do I create custom colors?
A: Mix different intensities of red, green, and blue using the analogWrite()
function to create custom colors.
Q: What is the difference between common anode and common cathode RGB LEDs? A: In common anode LEDs, all anodes are connected together to VCC. In common cathode LEDs, all cathodes are connected to GND. The type affects how you control the LED with your microcontroller.