How to Get a LED to Fade on ESP32-S3

2025-04-24 10:05:32

Controlling the brightness of an LED on an ESP32-S3 is a great way to learn about Pulse Width Modulation (PWM). This guide will show you how to fade an LED in and out using the ESP32-S3’s LEDC (LED Controller) feature.

ESP-IDF Official Document


1. Components Needed

  • ESP32-S3 development board

  • LED (any color)

  • 220Ω resistor

  • Breadboard and jumper wires

2. Wiring the LED to ESP32-S3

Connection Diagram:

  • Connect the long leg (anode) of the LED to GPIO 5 through a 220Ω resistor.

  • Connect the short leg (cathode) to GND.

(Replace with actual image)

3. Understanding PWM on ESP32-S3

PWM allows us to simulate analog output by rapidly switching the GPIO pin between HIGH and LOW states. The ESP32-S3 has LEDC (LED Controller), which supports PWM with different frequency and resolution settings.

How PWM Works

(Replace with actual image)

4. LED Fading Code for ESP32-S3

The following Arduino sketch gradually increases and decreases the brightness of the LED.

LED Fading Code for ESP32-S3

#define LED_PIN 5  // GPIO where the LED is connected
#define LED_CHANNEL 0
#define LED_FREQ 5000 // 5 kHz frequency
#define LED_RESOLUTION 8 // 8-bit resolution (0-255)

void setup() {
  ledcSetup(LED_CHANNEL, LED_FREQ, LED_RESOLUTION);
  ledcAttachPin(LED_PIN, LED_CHANNEL);
}

void loop() {
  for (int brightness = 0; brightness <= 255; brightness++) {
    ledcWrite(LED_CHANNEL, brightness);
    delay(10); // Adjust fade speed
  }
  for (int brightness = 255; brightness >= 0; brightness--) {
    ledcWrite(LED_CHANNEL, brightness);
    delay(10);
  }
}


More Detailed Tutorial View

5. Enhancing the Project

1️⃣ Add a Button to Control LED Fading

Modify the code to start and stop the fading effect using a button:

Add a Button to Control LED Fading

#define BUTTON_PIN 4  // GPIO where the button is connected
bool fadeEnabled = false;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    fadeEnabled = !fadeEnabled;
    delay(200); // Debounce delay
  }

  if (fadeEnabled) {
    // Run LED fading code here
  }
}

2️⃣ Use a Potentiometer to Control Brightness

Connect a potentiometer to an analog pin to dynamically adjust brightness.

Use a Potentiometer to Control Brightness

#define POT_PIN 34  // Analog input pin for the potentiometer

void loop() {
  int potValue = analogRead(POT_PIN);
  int brightness = map(potValue, 0, 4095, 0, 255);
  ledcWrite(LED_CHANNEL, brightness);
}

3️⃣ Control LED via WiFi or Bluetooth

Use ESP32’s built-in WiFi or Bluetooth to control LED brightness remotely via a mobile app or web interface.

6. Troubleshooting

Common Issues & Fixes

LED Not Fading?

  • Ensure the LED is connected to the correct GPIO pin.

  • Double-check the resistor value (220Ω recommended).

  • Verify that your ESP32-S3 board is flashed correctly with the code.

  • Use Serial.println(brightness); to debug brightness values.

Power Issues?

  • Use a stable 5V power source for the ESP32-S3.

  • If using multiple LEDs, consider external power supply.

PWM Not Working on Some GPIOs?

  • Avoid using reserved GPIOs (e.g., GPIO6-11 are used for flash memory).

Serial Debugging Tip Use Serial.print() statements to check brightness values in real-time.

 Serial Debugging Tip Use

Serial.print("Brightness: ");
Serial.println(brightness);

7. Advanced Features

1️⃣ Multi-LED Fading

Control multiple LEDs fading asynchronously using different PWM channels.

Multi-LED Fading

#define LED1_PIN 5
#define LED2_PIN 18

void setup() {
  ledcSetup(0, 5000, 8);
  ledcSetup(1, 5000, 8);
  ledcAttachPin(LED1_PIN, 0);
  ledcAttachPin(LED2_PIN, 1);
}

2️⃣ IoT Integration

Send LED brightness data to the cloud using MQTT or HTTP requests.

3️⃣ Home Automation Integration

Use ESPHome or Home Assistant to integrate with smart home systems.

Structured FAQ

Q: Can I use multiple LEDs with ESP32-S3?

Yes, you can use multiple PWM channels to control multiple LEDs.

Q: How can I control LED brightness wirelessly?

You can use Bluetooth or WiFi to control LED brightness remotely.

Q: Why is my LED not dimming smoothly?

Try adjusting the PWM frequency or resolution for smoother dimming.

9. Video Demonstration

🎥 Watch this video tutorial for a step-by-step guide: YouTube Video (Replace with actual video link)

 

Related Experience Learning: Ultimate ESP32 Troubleshooting Guide [Fix Flashing, WiFi, Bluetooth & More]

Raj Bhatt
Raj Bhatt
Raj Bhatt launched Eletronic Lab in 1999 as a passion project to share his journey in electronics and to contribute to the DIY and maker communities. Over the years, his work has evolved into a valuable resource for hobbyists and enthusiasts, offering detailed tutorials and practical projects that inspire creativity and learning. Raj's expertise and enthusiasm for electronics shine through in every project he shares. His mission is to make electronics accessible and engaging for everyone, especially newcomers to the field. He welcomes feedback, suggestions, and error reports from readers, believing that collaboration and shared knowledge drive continuous improvement. Raj hopes you enjoy exploring his work as much as he enjoys creating it.