electronicshub.org-in-depth esp32 pwm tutorial how to use pwm in esp32
electronicshub.org-in-depth esp32 pwm tutorial how to use pwm in esp32
ESP32?
electronicshub.org/esp32-pwm-tutorial
In this tutorial, we will learn how to use PWM in ESP32 SoC. Using PWM in ESP32, you
can control the brightness of LED, set the position of a Servo Motor and adjust the speed
of a DC Motor. This ESP32 PWM Tutorial contains all the important information for
configuring PWM in ESP32.
You can use ESP32’s PWM to drive LEDs, motors (normal DC Motors as well as Brushless
Motors) and smart lights. The PWM Controller in ESP32 consists of two main sub-
modules: LED Control or LEDC Peripheral and Motor Control Pulse Width Modulator or
MCPWM Peripheral.
Even though we will be limiting our demonstration of PWM in ESP32 to fading an LED, it
is good to know about the Motor Control PWM (MCPWM) block in ESP32, with input
capture modules.
If you ever worked with Brushless DC (BLDC) Motors, you will realize the importance of
sensing the position of the rotor (using Hall Effect Sensors) for precise speed control.
1/10
ESP32 LED PWM Controller (LEDC)
The LEDC Peripheral of ESP32 consists of 16 PWM Channels capable of generating
independent waveforms, mainly for RGB LED Control, but can also be used for other
purposes as well.
There are a couple of interesting points about LED PWM Controller in ESP32 that you
should be aware of.
16 independent PWM Channels, divided into group of two with 8 channels per
group.
Programmable resolution between 1-bit and 16-bits.
Frequency of the PWM wave depends on the resolution of PWM.
Automatically increases / decreases duty cycle without processor intervention.
Since, pretty much every thing in LED PWM of ESP32 is user configurable (channel,
resolution and frequency), instead of using ‘analogWrite()’ function, we will be using a
different (and dedicated) set of functions to configure PWM in ESP32.
Here is a list of all the LEDC APIs exposed by the driver. These functions are written for
Arduino IDE port of ESP32.
Of the 8 functions, we will be focusing on the first three, as they are more useful (and the
minimum required) for generating PWM.
As there are 16 PWM channels, the ‘channel’ argument takes any value between 0
and 15.
Next is the frequency of the PWM signal. You can set the frequency as per your
requirements like 1 KHz, 5 KHz, 8 KHz, and 10 KHz.
The resolution of the PWM is also configurable and ESP32 PWM can be
programmed anywhere between 1 bit to 16 bit resolution.
2/10
PWM frequency and resolution are inversely proportional and is dependent on the
clock source. So, be careful when selecting the values for frequency and resolution.
Finally, assign a GPIO pin for PWM Output. You can assign any GPIO Pin but be
careful when assigning (do not use already used GPIO pins like UART, SPI, etc.).
The following table shows a few commonly used PWM frequencies and resolutions.
This project is more about understanding the LEDC functions: ledcSetup, ledcAttachPin
and ledcWrite and how to generate PWM in ESP32 than the fading LED itself.
Components Required
ESP32 DevKit Development Board
3 x 5mm LED
220Ω Resistor
3 x 5KΩ Potentiometers
Breadboard
Connecting Wires
Micro-USB Cable
Circuit Diagram
3/10
The following image shows the connection for fading an LED using ESP32 PWM
Controller.
Code
You can use any GPIO Pin to output the PWM Signal. So, I am using GPIO 16, which is
also the UART2 RX Pin. Next, we have to setup the LEDC Channel using ‘ledcSetup’
function. The first argument is the channel. Any value between 0 and 15 can be given as
channel.
Next argument is the frequency. You can provide any frequency but as a convenience, I
will set the frequency as 5 KHz. Also, you have to set the resolution of the PWM. This
value must be a number between 1 and 16. I went with 10-bit resolution.
For the rest of the settings, refer to the following code, where I commented the important
lines.
int dutyCycle;
4/10
/* Setting PWM Properties */
void setup()
ledcAttachPin(LEDPin, PWMChannel);
void loop()
ledcWrite(PWMChannel, dutyCycle);
delay(3);
//delayMicroseconds(100);
ledcWrite(PWMChannel, dutyCycle);
delay(3);
//delayMicroseconds(100);
5/10
view raw ESP32-PWM-LED_Fading.ino hosted with ❤ by GitHub
NOTE: You can attach multiple GPIO pins to the same LEDC PWM Channel. If you do
so, all the GPIO pins will share the properties of the channel (resolution and frequency).
Three 5KΩ Potentiometers are connected to three ADC Input pins of ESP32. Based on the
output of the ADC, we will set the dutycycle of three PWM Channels, which are configured
with different parameters.
For easy understanding, I connected three LEDs: RED, GREEN and BLUE to three GPIO
Pins. These three GPIO Pins are attached to three different LEDC PWM Channels and
each channel is initialized with its own frequency and resolution.
6/10
Another important point to remember is the resolution of ADC of ESP32 is 12-bit. So, we
have to map this to the PWM resolution carefully, to get the full range of control.
Circuit Diagram
The following image shows the connections for adjusting the dutycycle of PWM Channels
using ADC (potentiometers).
Code
uint16_t redDutyCycle;
uint16_t greenDutyCycle;
uint16_t blueDutyCycle;
7/10
const int RED_MAX_DUTY_CYCLE = (int)(pow(2, redPWMResolution) - 1);
void setup()
Serial.begin(115200);
ledcAttachPin(redLEDPin, redPWMChannel);
ledcAttachPin(greenLEDPin, greenPWMChannel);
ledcAttachPin(blueLEDPin, bluePWMChannel);
void loop()
8/10
/* Read Analog Input from three ADC Inputs */
redDutyCycle = analogRead(A0);
greenDutyCycle = analogRead(A3);
blueDutyCycle = analogRead(A4);
ledcWrite(redPWMChannel, redDutyCycle);
ledcWrite(greenPWMChannel, greenDutyCycle);
ledcWrite(bluePWMChannel, blueDutyCycle);
Serial.print(redDutyCycle);
Serial.print(" -- ");
Serial.print(greenDutyCycle);
Serial.print(" -- ");
Serial.print(blueDutyCycle);
Serial.print("\n");
delay(1000);
9/10
Conclusion
A complete tutorial on ESP32 PWM peripheral. You learned how to use PWM in ESP32 to
fade an LED, how to configure the PWM Channel, set the frequency and resolution. You
also learned how PWM Channels in ESP32 can be configured independently.
2 Responses
Leave a Reply
Your email address will not be published. Required fields are marked *
10/10