Programming The ESP32 Using Arduino - PWM
Written by Harry Fairhead   
Tuesday, 21 January 2025
Article Index
Programming The ESP32 Using Arduino - PWM
Arduino Simple DAC
Uses of PWM – Digital to Analog

Uses of PWM – Digital to Analog

What sorts of things do you use PWM for? There are lots of very clever uses for PWM. However, there are two applications which account for most PWM applications - voltage or power modulation and signaling to servos.

The amount of power delivered to a device by a pulse train is proportional to the duty cycle. A pulse train that has a 50% duty cycle is delivering current to the load only 50% of the time and this is irrespective of the pulse repetition rate. So the duty cycle controls the power, but the period still matters in many situations because you want to avoid any flashing or other effects. A higher frequency smooths out the power flow at any duty cycle.

If you add a low-pass filter to the output of a PWM signal then what you get is a voltage that is proportional to the duty cycle. This can be looked at in many different ways, but again it is the result of the amount of power delivered by a PWM signal. You can also think of it as using the filter to remove the high-frequency components of the signal, leaving only the slower components due to the modulation of the duty cycle.

How fast you can work depends on the duty cycle resolution. If you work with 8-bit resolution your D-to-A conversion will have 256 steps, which at 3.3V gives a potential resolution of 3.3/256 or about 13mV. This is often good enough. The PWM output in this configuration mimics the workings of a, very slow, 8-bit D‑to‑A converter.

To demonstrate the sort of approach that you can take to D-to-A conversion, the following program creates a sine wave. To do this we need to compute the duty cycle for 256 points in a complete cycle. We could do this each time a value is needed, but to make the program fast enough we have to compute the entire 256 points and store them in an array. Notice that the ESP32-S2 doesn’t have floating-point arithmetic implemented in hardware, but the original ESP32 and the ESP32-S3 do support hardware floating point although it isn’t very fast.

The program is:

uint8_t wave[256];
void setup() {
  for (int i = 0; i < 256; i++) {
    wave[i] = (uint8_t)((128.0 + sinf((float)i * 2.0 *
                            3.14159 / 255.0) * 128.0));
  }
}
void loop() {
  for (int i = 0; i < 256; i++) {
    analogWrite(2, wave[i]);
    delayMicroseconds(1000);
  }
}

The 8-bit duty cycle values needed are computed and stored in the wave array. A for loop is used to set the duty cycle from the array. To make it work we need to perform an update roughly every timer overflow and this is done automatically using the delayMicroseconds. The waveform repeats after around 250ms, which makes the frequency around 4Hz.

To see the analog waveform, we need to put the digital output into a low-pass filter. A simple resistor and capacitor work reasonably well:

filter

With R1 at 7.5kΩ and C1 at 0.22µF the filter's cutoff is around 100Hz and might be a little on the high side for this low-frequency, 4Hz, output, but it produces a reasonable waveform:

sinewave

You can use this technique to create a sine wave, or any other waveform you need, but for high-quality audio you need a higher sampling rate. This is also the highest frequency 256 level sine wave that you can produce by this method. To do better we need to increase the PWM frequency so that it can be updated faster.

In Chapter but not in this extract

  • ESP32 PWM
  • Duty Cycle and Phase
  • Setting up the PWM
  • Frequency Modulation
  • Hardware Fade
  • Phase
  • What Else Can You Use PWM For?

Summary

  • PWM, Pulse Width Modulation, has a fixed repetition rate but a variable duty cycle, i.e. the amount of time the signal is high or low changes.

  • PWM can be generated by software simply by changing the state of a GPIO line correctly, but it can also be generated in hardware so relieving the processor of some work.

  • As well as being a way of signaling, PWM can also be used to vary the amount of power or voltage transferred. The higher the duty cycle, the more power/voltage.

  • The ESP32 has 16 hardware PWM generators and the ESP32-S3 has 8. These can be used with any of the GPIO lines capable of output.

  • There are only four timers per 8 PWM generator group which determine the frequency that the PWM lines work at. A single timer can be connected to multiple controllers.

  • The higher the frequency of the PWM, the lower the duty cycle resolution.

  • You can modify the frequency and duty cycle of a running PWM generator.

  • PWM can be used to implement digital-to-analog conversion simply by varying the duty cycle. You can dim an LED in the same way.

  • The PWM controllers can be set to auto fade an LED by varying the duty cycle.

  • By adjusting hpoint you can generate a signal with a fixed phase relationship to another signal.

 

Programming The ESP32 In C
Using The Arduino Library

By Harry Fairhead

ESP32CArduino360

Available as a softback, hardback and kindle from Amazon

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
    Extract:PWM ***NEW!!!
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
  12. Using The I2C Bus
  13. One-Wire Protocols
  14. The Serial Port
  15. Using WiFi
    Extract: A Web Client
  16. Flash Files
  17. Direct To The Hardware
  18. Free RTOS 

<ASIN:B0DHKT342W>

<ASIN:187196282X>

<ASIN:1871962919>

espbook

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Tuesday, 21 January 2025 )