An RGB LED is a special LED that can emit Red, Green, and Blue light. By combining these three colors in different intensities, we can create almost any color.
There are two types of RGB LEDs:
In this tutorial, we’ll control a Common Anode RGB LED using the ESP32 DOIT DevKit v1.
⚠️ Important: Since this is a Common Anode RGB LED, writing a LOW signal turns the LED ON. Writing HIGH turns it OFF.
// RGB LED with ESP32 DOIT DevKit v1 (Common Anode)
int redPin = 13;
int greenPin = 14;
int bluePin = 27;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Red
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
delay(1000);
// Green
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
delay(1000);
// Blue
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
delay(1000);
// Yellow (Red + Green)
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
delay(1000);
// Cyan (Green + Blue)
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
delay(1000);
// Magenta (Red + Blue)
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
delay(1000);
// White (All ON)
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
delay(1000);
}
int redPin = 13, greenPin = 14, bluePin = 27;
Assign ESP32 GPIOs for R, G, and B.
Setup Function
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Configure all three pins as outputs.
Loop Function
⚠️ Remember: LOW = ON, HIGH = OFF (because it’s a common anode LED).
Problem | Cause | Solution |
---|---|---|
LED doesn’t light | Wrong pin connections | Check Red=13, Green=14, Blue=27 |
Wrong colors | Common Anode/Cathode mismatch | Ensure you’re using Common Anode |
Colors too dim | High resistor value | Use 220Ω – 330Ω resistors |
ESP32 resets | Short circuit | Double-check wiring |
Yes, but the logic will be reversed (HIGH = ON, LOW = OFF).
Yes, by using PWM (analogWrite) on each pin. We’ll cover this in the PWM tutorial.
Resistors limit current and prevent LED damage.
Yes, by combining PWM signals, you can fade colors smoothly.
RGB LEDs typically work at 2V–3.3V per color, which is safe for ESP32.