Deep ESP32 PWM Tutorial How to use PWM in ESP32

picture

A brief note on ESP32PWM

The ESP32 SoC is loaded with very useful peripherals, and PWM is one of them. Yes. There is a dedicated PWM hardware module in the ESP32 chip. Pulse width modulation, or PWM for short, is a mature and widely used power supply technology.

You can use ESP32's PWM to drive LEDs, motors (common DC motors and brushless motors) and smart lights. The PWM controller in ESP32 consists of two main sub-modules: the LED control or LEDC peripheral and the motor control pulse width modulator or MCPWM peripheral.

Although we have limited the PWM demonstration in ESP32 to fading LEDs, it would be good to look at the Motor Control PWM (MCPWM) module with input capture module in ESP32.

If you have ever worked with a brushless DC (BLDC) motor, you are aware of the importance of sensing rotor position (using Hall effect sensors) for precise speed control.

ESP32 Light Emitting Diode (LEDC)  

The ESP32's LEDC peripheral consists of 16 PWM channels capable of generating independent waveforms, primarily for RGB LED control, but can be used for other purposes as well.

There are a few interesting points you should note about the LED PWM controller in the ESP32.

16 independent PWM channels, divided into two groups, each group has 8 channels.

Programmable resolution between 1 and 16 bits.

The frequency of the PWM wave depends on the resolution of the PWM.

Automatically increase/decrease duty cycle without processor intervention.

Configure the PWM channel of ESP32  

Do you remember the 'analogWrite()' function in Arduino programming? It is the function responsible for generating PWM in the Arduino UNO (and other "Arduino" boards).

Since almost everything in the ESP32's LED PWM is user-configurable (channels, resolution, and frequency), we will use a different (dedicated) set of functions to configure the PWM in the ESP32 instead of using "analogWrite( )"Function.

The following is a list of all LEDC APIs exposed by the driver. These functions are written for the Arduino IDE port of the ESP32.

ledcSetup(channel, frequency, resolution_bits);

ledcAttachPin(pin, channel);

ledcWrite(channel, Occupational cycle);

ledcRead(channel);

ledcWriteTone(channel, frequency);

ledcWriteNote(channel, note, octave);

ledcReadFreq(channel);

ledcDetachPin(pin);

Of the 8 functions, we will focus on the first three as they are more useful (and the minimum required) for generating PWM.

A few key points to remember when configuring PWM channels in ESP32:

Since there are 16 PWM channels, the "channel" parameter takes any value between 0 and 15.

Next is the frequency of the PWM signal. You can set the frequency as per your requirement such as 1 KHz, 5 KHz, 8 KHz and 10 KHz.

The resolution of the PWM is also configurable, the ESP32 PWM can be programmed anywhere between 1-bit to 16-bit resolution.

PWM frequency is inversely proportional to resolution and depends on the clock source. Therefore, be careful when choosing frequency and resolution values.

Finally, assign a GPIO pin for the PWM output. You can assign any GPIO pin, but be careful while doing so (do not use an already used GPIO pin like UART, SPI, etc.).

The table below shows several commonly used PWM frequencies and resolutions.

picture

LED fading using PWM in ESP32  

With all the necessary information about PWM in ESP32 released, we can now move on to implement our first project ESP32 PWM to fade an LED. This is a very simple project where the brightness of an LED connected to an ESP32 GPIO pin gradually increases and decreases repeatedly. [ESP32 Project for Beginners]

This project is more about understanding the functions of LEDC: ledcSetup, ledcAttachPin and ledcWrite, and how to generate PWM in ESP32, rather than fading the LED itself.

Required components  

ESP32 development kit development board

3 x 5mm LEDs

220Ω resistor

3 x 5KΩ potentiometers

Breadboard

Connection cable

Micro USB cable

Circuit diagram  

The image below shows the connection for fading an LED using the ESP32 PWM controller.

picture

code  

You can use any GPIO pin to output PWM signals. So, I'm using GPIO 16, which is also the UART2 RX pin. Next, we have to set up the LEDC channel using the "ledcSetup" function. The first parameter is the channel. Any value between 0 and 15 can be given as a channel.

The next parameter is frequency. You can provide any frequency, but for convenience I set the frequency to 5KHz. Additionally, you have to set the resolution of the PWM. This value must be a number between 1 and 16. I chose 10-bit resolution.

For the rest of the setup, see the code below, where I've commented out the important lines.

picture

Note: You can connect multiple GPIO pins to the same LEDC PWM channel. If you do this, all GPIO pins will share the channel's properties (resolution and frequency).

ESP32 Pulse Width Modulator with ADC  

An important feature of PWM in ESP32 is that all 16 channels can be configured independently, that is, each channel can have its own resolution and frequency. To demonstrate this, we use the ADC peripheral to independently adjust the duty cycle of three different LEDC PWM channels by turning the potentiometer.

Three 5KΩ potentiometers are connected to the three ADC input pins of the ESP32. Based on the output of the ADC, we will set the duty cycle of the three PWM channels, which are configured with different parameters.

To make it easier to understand, I connected three LEDs: red, green and blue to three GPIO pins. These three GPIO pins are connected to three different LEDC PWM channels, each initialized with its own frequency and resolution.

picture

Another important point to remember is that the ESP32's ADC resolution is 12 bits. So we have to carefully map it to the PWM resolution to get full control.

Circuit diagram  

The image below shows the connections for adjusting the PWM channel duty cycle using an ADC (potentiometer).

picture

code  

picture

in conclusion  

A complete tutorial on the ESP32 PWM peripheral. You learned how to use PWM to fade an LED in and out of the ESP32, how to configure the PWM channel, and set the frequency and resolution. You also learned how to independently configure the PWM channels in the ESP32.

         

Guess you like

Origin blog.csdn.net/qq_58404700/article/details/132748551