◆Smart Home Ideas Journal

ESP32 LED PWM Example: Brighten Up Your Projects

By

The ESP32, developed by Espressif Systems, is a low-cost, low-power system on a chip microcontroller that offers advanced IoT features like Wi-Fi and Bluetooth, along with dual-mode digital logic and sophisticated analog features. A popular application of the ESP32 is controlling LED lights, an action often performed using Pulse Width Modulation (PWM). Today, we're going to explore the ESP32's PWM capabilities and explore an LED example in detail.

Before delving into the example, let's briefly understand PWM and the ESP32's PWM functionality. PWM is a technique that allows you to control the brightness of an LED by varying the duty cycle of a square wave. The ESP32 has built-in PWM functionality which you can use to control the intensity of LEDs connected to its GPIO pins.

The LED and ESP32 Connection

The first step is to connect the LED to your ESP32. Typically, a common anode LED is used for reverse polarity protection. You can connect the longer leg (anode) to one of the ESP32's GPIO pins and the shorter leg (cathode) to the ground (GND) pin.

ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
MicroPython PWM ESP32 & ESP8266 - LED fading & Brightness Control
MicroPython PWM ESP32 & ESP8266 - LED fading & Brightness Control
the electronic device is connected to wires and other electrical equipment on a purple clothed surface
the electronic device is connected to wires and other electrical equipment on a purple clothed surface

For safety purposes, it's recommended to use a resistor in series with your LED. This protects the LED from excess current and prolongs its lifespan. A common value for this resistor is around 220 ohms to 330 ohms.

Setting up the LED Pin

In your ESP32 programming, first, include the necessary header files and define your LED pin:

ESP32
ESP32
ESP32 PWM with Arduino IDE ( LED Fading Example )
ESP32 PWM with Arduino IDE ( LED Fading Example )
ESP32 PWM Signals with Arduino – Analog Output
ESP32 PWM Signals with Arduino – Analog Output
```c #include #define LED_PIN 2 // ESP32's GPIO2 connected to LED's anode ```

Next, initialize the GPIO as an output pin:

```c void setup() { gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT); } ```

Generating PWM

To generate PWM, you can use the `ledc_set_fade_time` and `ledc_set_duty` functions. First, initialize the LEDC timer and channel:

```c void setup() { ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_LOW_SPEED_MODE, .duty_resolution = LEDC_TIMER_13_BIT, .timer_num = LEDC_TIMER_0 }; ledc_timer_config(&ledc_timer); ledc_channel_config_t ledc_channel = { .speed_mode = LEDC_LOW_SPEED_MODE, .channel = LEDC_CHANNEL_0, .timer_sel = LEDC_TIMER_0, .intr_type = LEDC_INTR_DISABLE, .gpio_num = LED_PIN, .duty = 0, .hpoint = 0 }; ledc_channel_config(&ledc_channel); } ```

The `speed_mode` set to `LEDC_LOW_SPEED_MODE` ensures the correct timer is used for PWM. The `duty_resolution` of 13 bits sets the PWM resolution, and `timer_sel` matches the timer to the channel.

Then, you can start the LEDC timer with a fade time, set the duty cycle, and finally enable the PWM channel:

```c void loop() { ledc_set_fade_time(LEDC_CHANNEL_0, 128); // Fade time in ms ledc_update_duty(LEDC_CHANNEL_0, 128); // Duty cycle (0-8191) ledc_fade_func_enable(LEDC_FADE.func, LEDC_CHANNEL_0); delay(1000); // Wait for 1 second ledc_fade_func_disable(LEDC_FADE.func, LEDC_CHANNEL_0); } ```

In this loop, the LED's brightness increases to full intensity over 128 ms, stays at full intensity for 1 second, decreases to off over 128 ms, and repeats this cycle.

Additional Advanced Features

Beyond simple PWM control, the ESP32 also offers advanced features like fade and brightness control. Fading allows the LED to smoothly increase or decrease in brightness, creating visually appealing transitions. Brightness control lets you set the maximum brightness of the LED, protecting it from excess power.

Fade and Brightness Control

To enable fade and brightness control, you can use `ledc_set_fade_time`, `ledc_set_duty`, and `ledc_set_brightness`. Here's an example:

```c void loop() { ledc_set_fade_time(LEDC_CHANNEL_0, 1000); // Fade time in ms ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 8191); // Duty cycle (0-8191) ledc_update_duty(LEDC_CHANNEL_0); ledc_set_brightness(LEDC_CHANNEL_0, 2); // Brightness value (0-1) delay(1000); // Wait for 1 second ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 0); // Set duty cycle to 0 ledc_update_duty(LEDC_CHANNEL_0); } ```

In this code, the LED fades to full brightness (8191 duty cycle) over 1 second, maintains this brightness with a maximum of 2 out of 16 brightness levels for 1 second, then fades to off over 1 second.

Experiment with different fade times, duty cycles, and brightness values to achieve the desired effects. Keep in mind that faster fade times and higher duty cycles and brightness values will increase power consumption and may cause your ESC32 to heat up over time.

In essence, controlling an LED with an ESP32 using PWM offers numerous possibilities. By mastering PWM and exploring the advanced features available, you can create stunning visual effects and bring your IoT projects to life.

ESP32 PWM - The Engineering Projects
ESP32 PWM - The Engineering Projects
In-Depth ESP32 PWM Tutorial | How to use PWM in ESP32?
In-Depth ESP32 PWM Tutorial | How to use PWM in ESP32?
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
PWM With ESP32 and ESP8266 Controlling LED Brightness-MicroPython
PWM With ESP32 and ESP8266 Controlling LED Brightness-MicroPython
ESP-IDF: ESP32 GPIO PWM with LEDC (LED Control) | Random Nerd Tutorials
ESP-IDF: ESP32 GPIO PWM with LEDC (LED Control) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE ( LED Fading Example )
ESP32 PWM with Arduino IDE ( LED Fading Example )
💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
WLED ON XIAO ESP32
WLED ON XIAO ESP32
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP8266 PWM with Arduino IDE – LED Fading
ESP8266 PWM with Arduino IDE – LED Fading
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
In-Depth: Generating a PWM Signal on the ESP32
In-Depth: Generating a PWM Signal on the ESP32
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
How to send SMS using ESP32
How to send SMS using ESP32
How to Use ESP32 to Control LED With Blynk Via WiFi
How to Use ESP32 to Control LED With Blynk Via WiFi
IOT Made Simple: Playing With the ESP32 on Arduino IDE
IOT Made Simple: Playing With the ESP32 on Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
ESP32 Interrupts and Timers with PIR Sensor using Arduino IDE
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes