Skip to content

esp32/machine_pwm: Add PWM duty_u16() and duty_ns(). #7907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions docs/esp32/quickref.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,24 @@ range from 1Hz to 40MHz but there is a tradeoff; as the base frequency
*increases* the duty resolution *decreases*. See
`LED Control <https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/ledc.html>`_
for more details.
Currently the duty cycle has to be in the range of 0-1023.

Use the ``machine.PWM`` class::
Use the :ref:`machine.PWM <machine.PWM>` class::

from machine import Pin, PWM

pwm0 = PWM(Pin(0)) # create PWM object from a pin
pwm0.freq() # get current frequency (default 5kHz)
pwm0.freq(1000) # set frequency
pwm0.duty() # get current duty cycle (default 512, 50%)
pwm0.duty(200) # set duty cycle
pwm0.deinit() # turn off PWM on the pin
pwm0 = PWM(Pin(0)) # create PWM object from a pin
pwm0.freq() # get current frequency (default 5kHz)
pwm0.freq(1000) # set PWM frequency from 1Hz to 40MHz
pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%)
pwm0.duty(256) # set duty cycle from 0 to 1023 as a ratio `duty / 1023`, (now 25%)
pwm0.duty_u16(2**16*3//4) # set duty cycle from 0 to 65535 as a ratio `duty_u16 / 65535`, (now 75%)
pwm0.duty_u16() # get current duty cycle, range 0-65535
pwm0.duty_ns(250_000) # set pulse width in nanoseconds from 0 to `1_000_000_000 / freq`, (now 25%)
pwm0.duty_ns() # get current pulse width in ns
pwm0.deinit() # turn off PWM on the pin

pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
print(pwm2) # view PWM settings

ESP chips have different hardware peripherals:

Expand All @@ -251,6 +255,8 @@ but only 8 different PWM frequencies are available, the remaining 8 channels mus
have the same frequency. On the other hand, 16 independent PWM duty cycles are
possible at the same frequency.

See more examples :ref:`esp32.PWM <esp32.PWM>`

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

Expand Down Expand Up @@ -409,14 +415,14 @@ I2S bus
See :ref:`machine.I2S <machine.I2S>`. ::

from machine import I2S, Pin

i2s = I2S(0, sck=Pin(13), ws=Pin(14), sd=Pin(34), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=44100, ibuf=40000) # create I2S object
i2s.write(buf) # write buffer of audio samples to I2S device

i2s = I2S(1, sck=Pin(33), ws=Pin(25), sd=Pin(32), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object
i2s.readinto(buf) # fill buffer with audio samples from I2S device
The I2S class is currently available as a Technical Preview. During the preview period, feedback from

The I2S class is currently available as a Technical Preview. During the preview period, feedback from
users is encouraged. Based on this feedback, the I2S class API and implementation may be changed.

ESP32 has two I2S buses with id=0 and id=1
Expand Down
1 change: 0 additions & 1 deletion docs/esp32/tutorial/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ to `<https://www.python.org>`__.

.. toctree::
:maxdepth: 1
:numbered:

intro.rst
pwm.rst
Expand Down
105 changes: 85 additions & 20 deletions docs/esp32/tutorial/pwm.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. _esp32_pwm:
.. _esp32.PWM:

Pulse Width Modulation
======================
Expand All @@ -11,7 +11,7 @@ compared with the length of a single period (low plus high time). Maximum
duty cycle is when the pin is high all of the time, and minimum is when it is
low all of the time.

More comprehensive example with all 16 PWM channels and 8 timers::
* More comprehensive example with all 16 PWM channels and 8 timers::

from machine import Pin, PWM
try:
Expand All @@ -29,21 +29,86 @@ More comprehensive example with all 16 PWM channels and 8 timers::
except:
pass

Output is::

PWM(pin=15, freq=100, duty=64, resolution=10, mode=0, channel=0, timer=0)
PWM(pin=2, freq=100, duty=128, resolution=10, mode=0, channel=1, timer=0)
PWM(pin=4, freq=200, duty=192, resolution=10, mode=0, channel=2, timer=1)
PWM(pin=16, freq=200, duty=256, resolution=10, mode=0, channel=3, timer=1)
PWM(pin=18, freq=300, duty=320, resolution=10, mode=0, channel=4, timer=2)
PWM(pin=19, freq=300, duty=384, resolution=10, mode=0, channel=5, timer=2)
PWM(pin=22, freq=400, duty=448, resolution=10, mode=0, channel=6, timer=3)
PWM(pin=23, freq=400, duty=512, resolution=10, mode=0, channel=7, timer=3)
PWM(pin=25, freq=500, duty=576, resolution=10, mode=1, channel=0, timer=0)
PWM(pin=26, freq=500, duty=640, resolution=10, mode=1, channel=1, timer=0)
PWM(pin=27, freq=600, duty=704, resolution=10, mode=1, channel=2, timer=1)
PWM(pin=14, freq=600, duty=768, resolution=10, mode=1, channel=3, timer=1)
PWM(pin=12, freq=700, duty=832, resolution=10, mode=1, channel=4, timer=2)
PWM(pin=13, freq=700, duty=896, resolution=10, mode=1, channel=5, timer=2)
PWM(pin=32, freq=800, duty=960, resolution=10, mode=1, channel=6, timer=3)
PWM(pin=33, freq=800, duty=1023, resolution=10, mode=1, channel=7, timer=3)
Output is::

PWM(Pin(15), freq=100, duty=64, resolution=10, mode=0, channel=0, timer=0)
PWM(Pin(2), freq=100, duty=128, resolution=10, mode=0, channel=1, timer=0)
PWM(Pin(4), freq=200, duty=192, resolution=10, mode=0, channel=2, timer=1)
PWM(Pin(16), freq=200, duty=256, resolution=10, mode=0, channel=3, timer=1)
PWM(Pin(18), freq=300, duty=320, resolution=10, mode=0, channel=4, timer=2)
PWM(Pin(19), freq=300, duty=384, resolution=10, mode=0, channel=5, timer=2)
PWM(Pin(22), freq=400, duty=448, resolution=10, mode=0, channel=6, timer=3)
PWM(Pin(23), freq=400, duty=512, resolution=10, mode=0, channel=7, timer=3)
PWM(Pin(25), freq=500, duty=576, resolution=10, mode=1, channel=0, timer=0)
PWM(Pin(26), freq=500, duty=640, resolution=10, mode=1, channel=1, timer=0)
PWM(Pin(27), freq=600, duty=704, resolution=10, mode=1, channel=2, timer=1)
PWM(Pin(14), freq=600, duty=768, resolution=10, mode=1, channel=3, timer=1)
PWM(Pin(12), freq=700, duty=832, resolution=10, mode=1, channel=4, timer=2)
PWM(Pin(13), freq=700, duty=896, resolution=10, mode=1, channel=5, timer=2)
PWM(Pin(32), freq=800, duty=960, resolution=10, mode=1, channel=6, timer=3)
PWM(Pin(33), freq=800, duty=1023, resolution=10, mode=1, channel=7, timer=3)

* Example of a smooth frequency change::

from utime import sleep
from machine import Pin, PWM

F_MIN = 500
F_MAX = 1000

f = F_MIN
delta_f = 1

p=PWM(Pin(5), f)
print(p)
while True:
p.freq(f)

sleep(10 / F_MIN)

f += delta_f
if (f >= F_MAX) or (f <= F_MIN):
delta_f = -delta_f

See PWM wave at Pin(5) with an oscilloscope.

* Example of a smooth duty change::

from utime import sleep
from machine import Pin, PWM

DUTY_MAX = 2**16 - 1

duty_u16 = 0
delta_d = 16

p=PWM(Pin(5), 1000, duty_u16=duty_u16)
print(p)
while True:
p.duty_u16(duty_u16)

sleep(1 / 1000)

duty_u16 += delta_d
if duty_u16 >= DUTY_MAX:
duty_u16 = DUTY_MAX
delta_d = -delta_d
elif duty_u16 <= 0:
duty_u16 = 0
delta_d = -delta_d

See PWM wave at Pin(5) with an oscilloscope.

Note:

* The Pin.OUT mode does not need to be specified. The channel is initialized to PWM mode internally once per certain Pin().

This code is wrong::

pwm=PWM(Pin(5, Pin.OUT), freq=1000, duty=512) # Pin(5) in PWM mode here
pwm=PWM(Pin(5, Pin.OUT), freq=500, duty=256) # Pin(5) in OUT mode here, PWM is off

Use this code instead of upper fragment::

pwm=PWM(Pin(5), freq=1000, duty=512)
pwm.init(freq=500, duty=256)
30 changes: 30 additions & 0 deletions docs/library/machine.PWM.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,33 @@ Methods
With no arguments the pulse width in nanoseconds is returned.

With a single *value* argument the pulse width is set to that value.

| Note 1) Not all frequencies can be generated with absolute accuracy due to
| the discrete nature of the computing hardware. Typically the PWM frequency
| is obtained by dividing some integer base frequency by an integer divider.
| For example, if the base frequency is 80MHz and the required PWM frequency is
| 300kHz the divider must be a non-integer number 80000000 / 300000 = 266.67!
| In fact, after rounding, the divider is set to 267, and the PWM frequency
| will be 80000000 / 267 = 299625.5 Hz, but not 300kHz.
| If the divider is set to 266, then the PWM frequency
| will be 80000000 / 266 = 300751.9 Hz, but again not 300kHz.

| Note 2) The duty cycle has the same discrete nature and its absolute accuracy is not achievable.
| On most hardware platforms the duty will be applied at the next frequency period.
| Therefore, you should wait more than "1/frequency" of a second before measuring the duty.

| Note 3) The frequency and the duty cycle resolution are interdependent.
| The higher the PWM frequency, the lower the duty resolution which is available,
| and vice versa. For example, a 300kHz PWM frequency has the duty cycle resolution of 8 bit,
| but not 16-bit as may be expected. In this case, the lowest 8 bits of *duty_u16*
| are insignificant. So

::

pwm=PWM(Pin(13), freq=300_000, duty_u16=2**16//2)

and ::

pwm=PWM(Pin(13), freq=300_000, duty_u16=2**16//2 + 255)

will generate PWM with the same 50% duty cycle.
Loading