Page 1 of 3 The ESP32 S3 supports PWM, but it has features that go beyond the usual Ardunio core. This is an extract from Harry Fairhead's latest book on programming the ESP32 using C and the Arduino library.
By Harry Fairhead
Available as a softback, hardback and kindle from Amazon
Contents
Preface
- The ESP32 – Before We Begin
- Getting Started
- Getting Started With The GPIO
- Simple Output
- Some Electronics
- Simple Input
- Advanced Input – Interrupts
- Pulse Width Modulation
Extract:PWM ***NEW!!!
- Controlling Motors And Servos
- Getting Started With The SPI Bus
- Using Analog Sensors
- Using The I2C Bus
- One-Wire Protocols
- The Serial Port
- Using WiFi
Extract: A Web Client
- Flash Files
- Direct To The Hardware
- Free RTOS
<ASIN:B0DHKT342W>
<ASIN:187196282X>
<ASIN:1871962919>
One way around the problem of getting a fast response from a microcontroller is to move the problem away from the processor. In the case of the ESP32 there are some built-in devices that can use GPIO lines to implement protocols without the CPU being involved. In this chapter we take a close look at the use of Pulse Width Modulation (PWM) including generating sound, driving LEDs and servos.
When performing their most basic function, i.e. output, the GPIO lines can be set high or low by the processor. How fast they can be set high or low depends on the speed of the processor.
Using the GPIO line in its Pulse Width Modulation (PWM) mode you can generate pulse trains up to 40 MHz. The reason for the increase in speed is that the GPIO is connected to a pulse generator and, once set to generate pulses of a specific type, the pulse generator just gets on with it without needing any intervention from the GPIO line or the processor. In fact, the pulse output will continue after your program has ended. Of course, even though the PWM line can generate very fast pulses, usually what you want to do is change the nature of the pulses and this is a slower process involving the processor.
In this chapter we look at the LEDC hardware which is designed to provide PWM to specifically drive LEDs – however it is more generally useful as a PWM generator.
Some Basic PWM Facts
There are some facts worth getting clear right from the start, although their full significance will only become clear as we progress.
First, what is PWM? The simple answer is that a pulse width modulated signal has pulses that repeat at a fixed rate, say one pulse every millisecond, but the width of the pulse can be changed. There are two basic things to specify about the pulse train that is generated, its repetition rate and the width of each pulse. Usually the repetition rate is set as a simple repeat period and the width of each pulse is specified as a percentage of the repeat period, referred to as the duty cycle. So, for example, a 1ms repeat and a 50% duty cycle specifies a 1ms period, which is high for 50% of the time, i.e. a pulse width of 0.5ms. The two extremes are 100% duty cycle, i.e. the line is always high, and 0% duty cycle, i.e. the line is always low.
Notice it is the duty cycle that carries the information in PWM and not the frequency. What this means is that, in general, you select a repeat rate and stick to it and what you change as the program runs is the duty cycle.
In many cases PWM is implemented using special PWM-generator hardware that is either built into the processor chip or provided by an external chip. The processor simply sets the repeat rate by writing to a register and then changing the duty cycle by writing to another register. This provides the ideal sort of PWM with no load on the processor and glitch-free operation. You can even buy add-on boards that will provide additional channels of PWM without adding to the load on the processor.
The alternative to dedicated PWM hardware is to implement it in software. You can work out how to do this quite easily. All you need is a timing loop to set the line high at the desired repetition rate and then set it low again according to the duty cycle. You can implement this using either interrupts or a polling loop and in more advanced ways, such as using a DMA (Direct Memory Access) channel.
|