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.
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:
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.