This sample program demonstrates how to control an RGB tri-color LED using the ESP32-WROOM-32E. It implements basic on/off control and blinking effects through digital I/O pins, and also demonstrates LED dimming functionality using the ESP32's LEDC (PWM) peripheral.
Note: Two versions (V2.0 and V3.0) are available, which correspond to different ESP32 Arduino core versions.
If your installed ESP32 Arduino core is V3.0, compiling and uploading code of V2.0 will trigger errors. Just switch to the V3.0 version code to solve this problem.
This sample code is based on the ESP32-WROOM-32E microcontroller. The program controls an RGB tri-color LED (common anode, lit at low level), sequentially displaying steady-on and blinking effects for red, green, and blue colors, and finally demonstrates gradual brightness dimming via PWM.
The following is the basic configuration part of the code, including library inclusion, pin definitions, and global variable settings.
#include <Arduino.h>
#define RED_PIN 22
#define GREEN_PIN 16
#define BLUE_PIN 17
#define LED_ON 0
#define LED_OFF 1
int freq = 2000;
int channel = 0;
int resolution = 8;
const int led = RED_PIN;
Arduino.h, providing hardware abstraction for GPIO, PWM, and other functionalities.LED_ON = 0 (low level turns on) and LED_OFF = 1 (high level turns off) are defined.freq = 2000: Sets PWM frequency to 2000Hzchannel = 0: Uses LEDC channel 0resolution = 8: Sets PWM resolution to 8 bits (0-255 brightness levels)led = RED_PIN, selecting the red LED pin as the PWM demonstration object.| RGB LED Pin | ESP32-WROOM-32E Pin | Description |
|---|---|---|
| RED | GPIO22 | Red LED control |
| GREEN | GPIO16 | Green LED control |
| BLUE | GPIO17 | Blue LED control |
| VCC | 5V | Power supply positive |
| GND | GND | Power supply ground |
Note: The RGB LED in this example is a common anode type, lit at low level. If using a common cathode LED (lit at high level), swap the definitions of
LED_ONandLED_OFF.
The setup() function completes the initialization of GPIO pins and performs the initial LED state setting once after power-on.
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
delay(1000);
digitalWrite(RED_PIN, LED_ON);
digitalWrite(GREEN_PIN, LED_ON);
digitalWrite(BLUE_PIN, LED_ON);
delay(2000);
}
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Configures the red, green, and blue pins as output mode to drive the LEDs.
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
Turns off all LEDs after power-on to ensure a safe initial state.
delay(1000);
digitalWrite(RED_PIN, LED_ON);
digitalWrite(GREEN_PIN, LED_ON);
digitalWrite(BLUE_PIN, LED_ON);
delay(2000);
After waiting 1 second, simultaneously turns on the red, green, and blue LEDs for 2 seconds to verify hardware connections.
The loop() function is the main program loop, sequentially demonstrating steady-on and blinking effects for the RGB tri-color LEDs, and finally presenting gradual brightness dimming via PWM.
void loop()
{
int i = 0;
// Red phase
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
digitalWrite(RED_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(RED_PIN, LED_OFF);
delay(500);
digitalWrite(RED_PIN, LED_ON);
delay(500);
}
// Green phase
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(GREEN_PIN, LED_OFF);
delay(500);
digitalWrite(GREEN_PIN, LED_ON);
delay(500);
}
// Blue phase
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(BLUE_PIN, LED_OFF);
delay(500);
digitalWrite(BLUE_PIN, LED_ON);
delay(500);
}
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
// LEDC PWM dimming
ledcAttachChannel(led, freq, resolution, channel);
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
{
ledcWrite(led, dutyCycle);
delay(100);
}
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
{
ledcWrite(led, dutyCycle);
delay(100);
}
delay(1000);
ledcDetach(led);
}
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
digitalWrite(RED_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(RED_PIN, LED_OFF);
delay(500);
digitalWrite(RED_PIN, LED_ON);
delay(500);
}
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(GREEN_PIN, LED_OFF);
delay(500);
digitalWrite(GREEN_PIN, LED_ON);
delay(500);
}
Same logic as the red LED demonstration, switching to green LED for steady-on and blinking effects.
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_ON);
delay(1500);
for(i=0; i<6; i++)
{
digitalWrite(BLUE_PIN, LED_OFF);
delay(500);
digitalWrite(BLUE_PIN, LED_ON);
delay(500);
}
Same logic as the red LED demonstration, switching to blue LED for steady-on and blinking effects.
digitalWrite(RED_PIN, LED_OFF);
digitalWrite(GREEN_PIN, LED_OFF);
digitalWrite(BLUE_PIN, LED_OFF);
ledcAttachChannel(led, freq, resolution, channel);
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle = dutyCycle - 5)
{
ledcWrite(led, dutyCycle);
delay(100);
}
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle = dutyCycle + 5)
{
ledcWrite(led, dutyCycle);
delay(100);
}
delay(1000);
ledcDetach(led);
digitalWriteledcAttachChannel(led, freq, resolution, channel) connects RED_PIN to LEDC channel 0, setting frequency to 2000Hz and resolution to 8 bitsledcDetach(led) disconnects the pin from the PWM channel, restoring it to regular GPIO controlNote: The ESP32's LEDC (LED Controller) is a dedicated peripheral designed for PWM output. Compared to the traditional
analogWrite(), it offers more powerful features including independent channel management and flexible frequency/resolution configuration.
This sample program demonstrates the ESP32's control capabilities over an RGB tri-color LED through the following steps:
The key functions used in the program include:
pinMode(): Configures GPIO pin input/output modedigitalWrite(): Sets GPIO pin high/low levelledcAttachChannel(): Connects a pin to an LEDC PWM channelledcWrite(): Sets the PWM channel duty cycleledcDetach(): Disconnects a pin from the LEDC channeldelay(): Pauses execution for the specified number of millisecondsIf you need to modify the code to change the display effects, you can refer to the following aspects for adjustments:
Modify Pin Definitions:
RED_PIN, GREEN_PIN, and BLUE_PIN to suit different hardware wiringModify LED State Polarity:
LED_ON to 1 and LED_OFF to 0Modify Steady-On Duration:
delay(1500) value to change the steady-on duration for each colorModify Blink Count and Speed:
for(i=0; i<6; i++) to alter the number of blinksdelay(500) value inside the loop to alter the blink speedModify PWM Parameters:
freq (frequency) and resolution to change PWM output characteristicsAdd Color Mixing Effects:
Change LED Color for PWM Demonstration:
led variable to GREEN_PIN or BLUE_PIN to demonstrate PWM dimming for other colors