ESP32

ESP32 Tutorial: Getting Started with ESP32 PWM Pin a Step-by-Step Guide

Introduction:

Are you looking to create a project that involves controlling multiple LEDs, motors, or other types of actuators? If so, the ESP32 microcontroller board with its powerful PWM (Pulse Width Modulation) capabilities is the perfect tool for the job. By utilizing the Arduino programming language, you can easily control the ESP32 PWM pin to achieve precise control of the duty cycle and frequency of the signal, allowing you to control the brightness, speed, or position of your connected devices. Whether you’re building a custom lighting system, a motorized robot, or any other project that requires precise control of output signals, the ESP32 with Arduino is an excellent platform to work with. In this article, we’ll explore the basics of PWM(pulse width modulation) and show you how to set up and use the ESP32’s PWM outputs with Arduino, so you can start building your own amazing projects today!




Amazon link:

ESP32 development board

Breadboard

Jumper wires

330-ohm resistor

LED

Potentiometer (10k ohm)

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

ESP32 PWM Pins:

The ESP32 microcontroller board is equipped with a set of PWM pins, which stands for Pulse Width Modulation. PWM is a technique for controlling the amount of power delivered to a device by varying the duty cycle of a square wave signal. This technique is commonly used for controlling the brightness of LEDs, the speed of motors, or the position of servo motors. if you want to learn how to make fade-in and fade-out effects using Arduino Uno then read this article.

The ESP32 has 16 PWM pins in total, with pins 0-15 being available for PWM(pulse width modulation) output. These pins are arranged into two groups, each containing 8 pins. The first group is known as the “high-speed” group, and it includes pins 0, 2, 4, 5, 12, 13, 14, and 15. The second group is known as the “low-speed” group, and it includes pins 25, 26, 27, 32, 33, 34, 35, and 36.

The high-speed group of pins can operate at a maximum frequency of 80 MHz, while the low-speed group can operate at a maximum frequency of 40 MHz. However, the actual frequency of the PWM signal generated by the pins will depend on several factors, including the clock speed of the microcontroller, the resolution of the PWM signal, and the number of channels being used.

Each PWM pin on the ESP32 can be configured to operate in either “8-bit” mode or “16-bit” mode. In 8-bit mode, the duty cycle of the PWM signal can be varied between 0% and 100% in increments of approximately 4%, while in 16-bit mode, the duty cycle can be varied between 0% and 100% in increments of approximately 0.1%.



Features of Using ESP32 PWM Pins:

There are several great features of using ESP32 PWM pins in your projects. Here are some of the key benefits:

Precise Control:

The ESP32 PWM pins offer precise control over the duty cycle and frequency of the signal, which means you can control the brightness, speed, or position of your connected devices with a high degree of accuracy. This level of control is particularly useful for applications that require fine-tuning, such as controlling the speed of a motor or the brightness of an LED.

Multiple Channels:

The ESP32 has 16 PWM pins, which means you can control multiple devices simultaneously. This is particularly useful for applications that require multiple LEDs or motors to be controlled independently, such as a robot with multiple joints or a lighting system with multiple zones.

High Speed:

The ESP32’s PWM pins can operate at high frequencies, which means you can generate pulse width modulation signals that are suitable for driving a wide range of devices. This makes the ESP32 an excellent choice for applications that require fast response times, such as controlling the speed of a motor or the position of a servo.

Low CPU Usage:

The ESP32’s PWM pins have dedicated hardware that handles the PWM signal generation, which means that they require very little CPU usage. This frees up the microcontroller to perform other tasks, such as reading sensor data or running a control loop.

Easy to Use:

The ESP32’s PWM pins are easy to use, thanks to the Arduino programming environment. With just a few lines of code, you can configure the PWM pins and start generating signals.

Overall, the ESP32’s PWM pins are a powerful feature that can add a lot of functionality to your projects. Whether you’re building a robot, a lighting system, or anything in between, the ESP32’s PWM pins offer precise, reliable control that can take your project to the next level.



Control Single Esp32 PWM Pin:

The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth connectivity. One of the main features of ESP32 is its ability to generate Pulse Width Modulation (PWM) signals on its digital pins. pulse width modulation signals are used for controlling the intensity of LEDs, the speed of motors, and the position of servos. In this section, we will describe how to control a single ESP32 PWM pin.

Circuit diagram:

controlling single led with ESP32 PWM Pin

In this circuit, the LED is connected to the ESP32’s GPIO 16 pin via a current-limiting resistor. When the GPIO 16 pin is set to a high logic level, current flows through the LED, causing it to light up. The resistor is used to limit the amount of current flowing through the LED, ensuring that it operates within safe limits.

By using PWM signals, you can vary the brightness of the LED by adjusting the duty cycle of the PWM signal. For example, setting the duty cycle to 50% would result in the LED being on for half the time and off for the other half, resulting in a dimmer light.

Overall, this circuit is a simple example of how to use an ESP32 GPIO pin to control an LED. By adjusting the PWM signal and resistor values, you can create a variety of visual effects with the LED, making it a useful component in a wide range of projects.



Single ESP32 PWM Pin Controlling Code:

output:

Code explanation:

This block of code defines a constant integer ledPin that is set to 16, which represents the GPIO pin to which the LED is connected. The brightness variable is initialized to 0, which will be used to store the current brightness level of the LED.

The setup() function is a required function in Arduino sketches that is called once when the microcontroller is powered on or reset. In this code, the setup() function sets the ledPin as an output pin, which means that the ESP32 will be able to control the voltage applied to the pin.

The loop() function is another required function in Arduino sketches that is executed repeatedly as long as the microcontroller is powered on. In this code, the loop() function uses two for loops to gradually increase and decrease the brightness level of the LED.

The first for loop starts with the brightness variable set to 0 and increases it by 1 until it reaches 255. The analogWrite() function is used to set the voltage on the ledPin based on the current brightness level. The delay() function is used to pause the execution of the code for 10 milliseconds after each iteration, creating a smooth dimming effect.

The second for loop starts with the brightness variable set to 255 and decreases it by 1 until it reaches 0. The analogWrite() function is again used to set the voltage on the ledPin based on the current brightness level, and the delay() function is used to pause the code for another 10 milliseconds after each iteration.

this code creates a smooth dimming effect on an LED connected to GPIO pin 16 of an ESP32 microcontroller board, gradually increasing and decreasing the brightness level over time. The analogWrite() function used in this code is a special function available in the Arduino language that allows for the control of the duty cycle of a PWM signal, which is what enables the gradual increase and decrease in brightness level.




Controlling Multiple ESP32 PWM Pins:

The ESP32 is a powerful microcontroller that offers multiple hardware PWM (Pulse Width Modulation) pins, making it ideal for projects that require precise control over multiple devices such as motors, LED strips, and more. PWM is a technique used to control the speed of motors or the brightness of LEDs by varying the width of the pulse applied to them.

The ESP32 offers several hardware PWM channels that can be used to control multiple PWM pins simultaneously. Each channel can be configured with a specific frequency and resolution, allowing for greater flexibility and precision in controlling various devices. Additionally, the ESP32 provides built-in libraries that make it easy to set up and control these PWM channels with just a few lines of code.

With the ability to control multiple PWM pins, the ESP32 offers a powerful and versatile platform for a wide range of projects. Whether you’re building a robot, controlling an LED light show, or driving multiple motors, the ESP32’s hardware PWM pins can provide the precise control you need to achieve your project goals.

By using multiple PWM pins on an ESP32, you can control multiple LEDs simultaneously. In this case, gpio16, gpio21, and gpio22 are three popular PWM pins that can be used to control multiple LEDs. By controlling the duty cycle of the PWM signal applied to each pin, you can adjust the brightness of each LED independently.

The ESP32 provides a built-in library for controlling PWM pins, making it easy to set up and control the LEDs. With just a few lines of code, you can specify the pins you want to use as PWM outputs, set up the PWM channels, and attach each LED pin to its corresponding PWM channel.



Circuit diagram:

controlling multiple leds with ESP32 PWM Pin

In the above circuit diagram, I used three LEDs, a red LED connected to gpio16, a yellow LED connected to gpio21, and another red LED connected to gpio22. Each LED is also connected to the ESP32’s ground pin.

When the ESP32 is programmed to output a PWM signal on one of these GPIO pins, the PWM signal will be applied to the LED, causing it to light up. By varying the duty cycle of the PWM signal applied to each pin, the brightness of each LED can be adjusted independently.

For example, if the duty cycle of the PWM signal applied to gpio16 is increased, the brightness of the red LED connected to that pin will increase. Similarly, if the duty cycle of the PWM signal applied to gpio21 is decreased, the brightness of the yellow LED connected to that pin will decrease.

The ground pin is used to complete the circuit for each LED, allowing the current to flow through the LED and light it up. By connecting each LED to the same ground pin on the ESP32, a common ground is created for all the LEDs in the circuit.

Overall, this circuit provides a simple and flexible platform for controlling multiple LEDs using PWM(pulse width modulation) signals from an ESP32 microcontroller. With a little bit of programming, you can create a wide range of lighting effects and other projects using this circuit.



ESP32 Multiple PWM Pin Controlling Code:

Output:

Code Explanation:

These statements are called preprocessor directives and are used to define constant values that can be used throughout the program. In this case, three constant values are defined: RED_LED_PIN, YELLOW_LED_PIN, and RED2_LED_PIN. Each of these constants represents the pin number that will be used to control the corresponding LED light.

Moving on to the setup() function:

This function is executed once at the beginning of the program and is used to initialize the pins and PWM channels. The pinMode() function is used to set each pin as an output, which means that the pin will be used to send signals to the LED lights. The digitalWrite() function is used to set each pin to a low state, which turns off the LED lights initially.

Next, the ledcAttachPin() function is used to attach each pin to a specific PWM channel. The first argument specifies the pin number and the second argument specifies the PWM channel number. This function tells the Arduino board to use the specified pin to send PWM signals to the corresponding LED light.

After the pins have been attached to the PWM channels, the ledcSetup() function is used to initialize each PWM(pulse width modulation) channel. The first argument specifies the PWM channel number, the second argument specifies the frequency of the PWM signal in Hz, and the third argument specifies the resolution of the PWM signal in bits. In this case, each PWM channel is set to a frequency of 5000 Hz and a resolution of 8 bits.

Finally, let’s look at the loop() function:

This function is executed repeatedly in an infinite loop as long as the program is running on the microcontroller. The purpose of this function is to create a smooth, gradual increase and decrease in brightness for each LED light by varying the duty cycle of the PWM signal.

The first for loop gradually increases the brightness of each LED light by iterating through duty cycle values from 0 to 255. As the duty cycle increases, the LED light appears brighter because the LED is receiving more power and staying on for a longer period of time during each PWM cycle.

The second for loop gradually decreases the brightness of each LED light by iterating through duty cycle values from 255 down to 0. As the duty cycle decreases, the LED light appears dimmer because the LED is receiving less power and staying on for a shorter period of time during each PWM cycle.

Both for loops use the ledcWrite() function to set the duty cycle value for each PWM channel. The first argument specifies the Pulse Width Modulation channel number, which was assigned to a specific pin using the ledcAttachPin() function in the setup() function. The second argument specifies the duty cycle value, which ranges from 0 to 255 and determines how long the LED stays on during each PWM cycle.

The delay(10) function is used in both for loops to pause the program for 10 milliseconds before moving on to the next iteration. This delay creates a smooth transition between each duty cycle value and makes the LED light appear to fade in or out gradually.



Sending Sensor Data on ESP32 PWM Pin:

Controlling the brightness of an LED is a common requirement in many electronic projects. One effective way to do this is by using Pulse Width Modulation (PWM) signals. The ESP32 microcontroller is a powerful and versatile device that comes with a built-in Pulse Width Modulation that can be used to control the brightness of an LED. In this section, we will demonstrate how to use a potentiometer with the ESP32’s PWM pin to control the brightness of an LED.

Circuit diagram:

controlling single led with potentiometer using ESP32 PWM Pin

The LED is connected to GPIO16 of the ESP32 through a resistor. This resistor is typically used to limit the current flowing through the LED to prevent it from burning out. The value of the resistor may vary depending on the LED you’re using and the voltage you’re supplying to it.

The potentiometer is connected to GPIO32 of the ESP32. A potentiometer is a three-terminal variable resistor that allows you to adjust the resistance between two of its terminals by turning a knob. In this circuit, the middle terminal of the potentiometer is connected to GPIO32, while the other two terminals are connected to 3.3V and ground, respectively.

The potentiometer acts as a voltage divider, with the resistance between the middle terminal and one of the outer terminals changing as you turn the knob. By connecting the middle terminal to GPIO32, we can read the voltage at that point using the analog-to-digital converter (ADC) of the ESP32.

The ADC of the ESP32 is used to read the voltage at the middle terminal of the potentiometer, which is proportional to the position of the knob. The value read by the ADC is then used to set the duty cycle of the PWM signal generated by GPIO16. This duty cycle determines the average voltage applied to the LED, which in turn controls its brightness.

this circuit uses the potentiometer to adjust the voltage applied to the LED, and the ESP32’s PWM module to generate a signal with a variable duty cycle that controls the brightness of the LED.




Sending Sensor Data on ESP32 PWM Pin Code:

Output:

Code explanation:

These are global constant variables defined at the beginning of the code. The ledPin variable is the output pin used for the PWM signal. The freq variable sets the frequency of the PWM signal. The ledChannel variable specifies the PWM channel used. The resolution variable sets the resolution of the PWM signal (number of bits). The potPin variable specifies the input pin used for the potentiometer.

The setup() function is called once at the start of the program. In this function, the ledcSetup() function is used to set up the PWM channel with the specified frequency and resolution. The ledcAttachPin() function attaches the PWM signal to the ledPin output pin. The pinMode() function sets the potPin as an input.

The loop() function is called repeatedly after the setup() function. In this function, the analogRead() function reads the analog value from the potentiometer connected to the potPin. The map() function maps the analog value from a range of 0-4095 to a duty cycle value of 0-255. The ledcWrite() function writes the duty cycle value to the PWM channel specified by ledChannel. Finally, the delay() function adds a 10-millisecond delay before repeating the loop.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button