Mastering the Spectrum: The ESP32 3-Color LED Manual
The 3-Color LED Module, or RGB LED, is the building block of modern display technology. By combining Red, Green, and Blue light sources into a single package, the ESP32 can produce over 16 million colors through Additive Color Mixing. This manual explores the hardware requirements and the software logic needed to transform digital signals into a vibrant light spectrum.
How it Works: The RGB Color Model
Unlike pigments (which subtract light), LEDs add light together. When all three internal diodes are at 100% brightness, they create White light. By varying the intensity of each diode using PWM (Pulse Width Modulation), you can create any color. For example, Red + Green at full power creates Yellow, while Red + Blue creates Magenta.
Wiring the RGB Module
Most modules (like the KY-016) are Common Cathode, meaning they share a single Ground pin. Each color channel requires its own current-limiting resistor to account for different forward voltage drops (Red usually requires a higher resistance than Blue/Green to appear balanced).
| Module Pin | Color Channel | ESP32 GPIO Pin |
|---|---|---|
| R | Red Anode | GPIO 18 |
| G | Green Anode | GPIO 19 |
| B | Blue Anode | GPIO 21 |
| - | Common Ground | GND |
Programming: 24-Bit Color Control
To control an RGB LED effectively, we assign each color to a unique LEDC Channel. With an 8-bit resolution (0-255) for each of the three channels, the ESP32 achieves 24-bit color depth.
// Define Pins
const int rPin = 18;
const int gPin = 19;
const int bPin = 21;
void setup() {
// Setup 3 PWM Channels
ledcSetup(0, 5000, 8); // Red
ledcSetup(1, 5000, 8); // Green
ledcSetup(2, 5000, 8); // Blue
ledcAttachPin(rPin, 0);
ledcAttachPin(gPin, 1);
ledcAttachPin(bPin, 2);
}
void setColor(int r, int g, int b) {
ledcWrite(0, r);
ledcWrite(1, g);
ledcWrite(2, b);
}
void loop() {
setColor(255, 0, 0); // Red
delay(1000);
setColor(0, 255, 0); // Green
delay(1000);
setColor(0, 0, 255); // Blue
delay(1000);
setColor(255, 255, 255); // White
delay(1000);
}
IoT and Environmental Lighting
Integrating RGB LEDs with the ESP32's connectivity features allows for dynamic environments:
- Ambient Weather Indicators: Changing the LED color based on a weather API (Blue for rain, Yellow for sun).
- Mood Lighting via Web Server: A mobile UI with sliders for R, G, and B to set room atmosphere wirelessly.
- Notification Hub: Blinking specific colors for different app notifications (Green for WhatsApp, Blue for Email).
- CPU Load Monitor: Fading from Green to Red based on the ESP32's internal processing temperature or task load.
Common Pitfalls
- Inverted Colors (Common Anode): If your LED stays ON when the code says 0, you likely have a Common Anode module. Connect the common pin to 3.3V and use
255 - brightnessin your code. - Unbalanced White: If 'White' looks too pink or blue, you need to calibrate your resistors or PWM limits. Red is typically much brighter than Blue at the same current.
- Flickering during WiFi: Ensure you use the hardware-based
ledcWriterather than softwareanalogWriteto prevent the WiFi stack from interrupting the PWM timing.
Final Summary
The 3-Color LED Module is the ultimate visual output for an ESP32. By mastering the coordination of three separate PWM timers, you can communicate massive amounts of data through color alone, creating intuitive and beautiful human-machine interfaces.