Fading an LED on the ESP32-S3 is simple using PWM (Pulse Width Modulation). The ESP32-S3 has built-in LEDC (LED Controller) hardware, which makes it efficient for dimming LEDs smoothly.
Steps to Fade an LED on ESP32-S3
- Connect the LED – Attach the positive leg of the LED to a PWM-capable GPIO pin and the negative leg to the ground via a resistor (220Ω recommended).
- Set Up PWM in Code – Use the LEDC library to configure the PWM signal.
- Write a Simple Code – Use the following example in Arduino IDE:
const int ledPin = 5; // Use any PWM-capable GPIO
int brightness = 0;
int fadeAmount = 5;
void setup() {
ledcSetup(0, 5000, 8);
ledcAttachPin(ledPin, 0);
}
void loop() {
ledcWrite(0, brightness);
brightness += fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
- Upload and Test – Upload the code and observe the LED fading in and out smoothly.
This method ensures smooth LED dimming with minimal CPU usage.