8000 docs/esp32: Explain ESP32 PWM modes, timers, and channels. · micropython/micropython@71111cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 71111cf

Browse files
IhorNehrutsadpgeorge
authored andcommitted
docs/esp32: Explain ESP32 PWM modes, timers, and channels.
1 parent 52636fa commit 71111cf

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

docs/esp32/quickref.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ working with this board it may be useful to get an overview of the microcontroll
1616
:maxdepth: 1
1717

1818
general.rst
19-
tutorial/intro.rst
19+
tutorial/index.rst
2020

2121
Installing MicroPython
2222
----------------------
@@ -225,13 +225,31 @@ Use the ``machine.PWM`` class::
225225
from machine import Pin, PWM
226226

227227
pwm0 = PWM(Pin(0)) # create PWM object from a pin
228-
pwm0.freq() # get current frequency
228+
pwm0.freq() # get current frequency (default 5kHz)
229229
pwm0.freq(1000) # set frequency
230-
pwm0.duty() # get current duty cycle
230+
pwm0.duty() # get current duty cycle (default 512, 50%)
231231
pwm0.duty(200) # set duty cycle
232232
pwm0.deinit() # turn off PWM on the pin
233233

234- 8000
pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
234+
pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
235+
236+
ESP chips have different hardware peripherals:
237+
238+
===================================================== ======== ======== ========
239+
Hardware specification ESP32 ESP32-S2 ESP32-C3
240+
----------------------------------------------------- -------- -------- --------
241+
Number of groups (speed modes) 2 1 1
242+
Number of timers per group 4 4 4
243+
Number of channels per group 8 8 6
244+
----------------------------------------------------- -------- -------- --------
245+
Different of PWM frequencies (groups * timers) 8 4 4
246+
Total PWM channels (Pins, duties) (groups * channels) 16 8 6
247+
===================================================== ======== ======== ========
248+
249+
A maximum number of PWM channels (Pins) are available on the ESP32 - 16 channels,
250+
but only 8 different PWM frequencies are available, the remaining 8 channels must
251+
have the same frequency. On the other hand, 16 independent PWM duty cycles are
252+
possible at the same frequency.
235253

236254
ADC (analog to digital conversion)
237255
----------------------------------

docs/esp32/tutorial/index.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.. _esp32_tutorial:
2+
3+
MicroPython tutorial for ESP32
4+
==============================
5+
6+
This tutorial is intended to get you started using MicroPython on the ESP32
7+
system-on-a-chip. If it is your first time it is recommended to follow the
8+
tutorial through in the order below. Otherwise the sections are mostly self
9+
contained, so feel free to skip to those that interest you.
10+
11+
The tutorial does not assume that you know Python, but it also does not attempt
12+
to explain any of the details of the Python language. Instead it provides you
13+
with commands that are ready to run, and hopes that you will gain a bit of
14+
Python knowledge along the way. To learn more about Python itself please refer
15+
to `<https://www.python.org>`__.
16+
17+
.. toctree::
18+
:maxdepth: 1
19+
:numbered:
20+
21+
intro.rst
22+
pwm.rst

docs/esp32/tutorial/pwm.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. _esp32_pwm:
2+
3+
Pulse Width Modulation
4+
======================
5+
6+
Pulse width modulation (PWM) is a way to get an artificial analog output on a
7+
digital pin. It achieves this by rapidly toggling the pin from low to high.
8+
There are two parameters associated with this: the frequency of the toggling,
9+
and the duty cycle. The duty cycle is defined to be how long the pin is high
10+
compared with the length of a single period (low plus high time). Maximum
11+
duty cycle is when the pin is high all of the time, and minimum is when it is
12+
low all of the time.
13+
14+
More comprehensive example with all 16 PWM channels and 8 timers::
15+
16+
from machine import Pin, PWM
17+
try:
18+
f = 100 # Hz
19+
d = 1024 // 16 # 6.25%
20+
pins = (15, 2, 4, 16, 18, 19, 22, 23, 25, 26, 27, 14 , 12, 13, 32, 33)
21+
pwms = []
22+
for i, pin in enumerate(pins):
23+
pwms.append(PWM(Pin(pin), freq=f * (i // 2 + 1), duty= 1023 if i==15 else d * (i + 1)))
24+
print(pwms[i])
25+
finally:
26+
for pwm in pwms:
27+
try:
28+
pwm.deinit()
29+
except:
30+
pass
31+
32+
Output is::
33+
34+
PWM(pin=15, freq=100, duty=64, resolution=10, mode=0, channel=0, timer=0)
35+
PWM(pin=2, freq=100, duty=128, resolution=10, mode=0, channel=1, timer=0)
36+
PWM(pin=4, freq=200, duty=192, resolution=10, mode=0, channel=2, timer=1)
37+
PWM(pin=16, freq=200, duty=256, resolution=10, mode=0, channel=3, timer=1)
38+
PWM(pin=18, freq=300, duty=320, resolution=10, mode=0, channel=4, timer=2)
39+
PWM(pin=19, freq=300, duty=384, resolution=10, mode=0, channel=5, timer=2)
40+
PWM(pin=22, freq=400, duty=448, resolution=10, mode=0, channel=6, timer=3)
41+
PWM(pin=23, freq=400, duty=512, resolution=10, mode=0, channel=7, timer=3)
42+
PWM(pin=25, freq=500, duty=576, resolution=10, mode=1, channel=0, timer=0)
43+
PWM(pin=26, freq=500, duty=640, resolution=10, mode=1, channel=1, timer=0)
44+
PWM(pin=27, freq=600, duty=704, resolution=10, mode=1, channel=2, timer=1)
45+
PWM(pin=14, freq=600, duty=768, resolution=10, mode=1, channel=3, timer=1)
46+
PWM(pin=12, freq=700, duty=832, resolution=10, mode=1, channel=4, timer=2)
47+
PWM(pin=13, freq=700, duty=896, resolution=10, mode=1, channel=5, timer=2)
48+
PWM(pin=32, freq=800, duty=960, resolution=10, mode=1, channel=6, timer=3)
49+
PWM(pin=33, freq=800, duty=1023, resolution=10, mode=1, channel=7, timer=3)

0 commit comments

Comments
 (0)
0