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 primary color, users can mix colors to produce a wide spectrum of hues, including white. This Wokwi compatible RGB LED is designed to interface easily with microcontrollers such as the Arduino UNO, making it ideal for a variety of applications including mood lighting, color displays, and indicators.
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 an anode or cathode, depending on the model of the RGB LED. Ensure to check the datasheet of your specific component.
// 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 output
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
// Set the color to purple
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 a soft teal
analogWrite(RED_PIN, 0); // Red off
analogWrite(GREEN_PIN, 128); // Green at half intensity
analogWrite(BLUE_PIN, 128); // Blue at half intensity
delay(1000); // Wait for 1 second
}
Q: Can I power the RGB LED directly from an Arduino pin? A: While you can power the LED from an Arduino pin, it is recommended to use a current-limiting resistor to prevent damage to both the LED and the microcontroller.
Q: How do I create white light with an RGB LED? A: To create white light, you need to turn on all three colors (Red, Green, Blue) at full intensity. Adjust the intensity if the white light appears to have a color tint.
Q: What is PWM and how does it control the LED color? 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. By adjusting the duty cycle (the percentage of time the signal is high), you can control the intensity of each color, thus mixing them to create different colors.