An RGB LED with a common cathode configuration 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 decorative lighting, signal indicators, displays, and any application requiring visual feedback or aesthetic enhancement.
Pin Number | Description |
---|---|
1 | Anode for Red LED |
2 | Anode for Green LED |
3 | Anode for Blue LED |
4 | Common Cathode (GND) |
// Define the RGB LED pins
const int RED_PIN = 9; // Red LED anode connected to digital pin 9
const int GREEN_PIN = 10; // Green LED anode connected to digital pin 10
const int BLUE_PIN = 11; // Blue LED anode connected to digital pin 11
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); // Full intensity for Red
analogWrite(GREEN_PIN, 0); // No Green
analogWrite(BLUE_PIN, 255); // Full intensity for Blue
// Keep the color for 2 seconds
delay(2000);
// Turn off the LED
analogWrite(RED_PIN, 0);
analogWrite(GREEN_PIN, 0);
analogWrite(BLUE_PIN, 0);
// Wait for 1 second
delay(1000);
}
Q: Can I power the RGB LED directly from an Arduino pin? A: Yes, but you must use current-limiting resistors to protect the LEDs and the Arduino pins.
Q: How do I calculate the value of the current-limiting resistors? A: Use Ohm's Law: R = (V_supply - V_forward) / I_forward, where V_supply is the supply voltage, V_forward is the forward voltage of the LED, and I_forward is the desired forward current.
Q: What is PWM and how does it control the LED colors? A: PWM stands for Pulse Width Modulation. It controls the brightness of each LED by rapidly turning it on and off at a frequency high enough that the human eye perceives it as a continuous light with varying intensity.