-
Notifications
You must be signed in to change notification settings - Fork 344
pwm
This class includes PWM support using ESP32 LEDC peripheral
- up to 8 pwm channels can be used
- 4 pwm timers are available, multiple channels can use the same timer
- all pwm channels using the same timer have the same frequency, but can have different duty cycles
- maximum pwm frequency is 40 MHz
- pwm duty resolution can be 1-15 bits and is automatically set to maximum value for requested frequency.
The frequency and the duty resolution are interdependent. The higher the PWM frequency, the lower duty resolution is available and vice versa.
>>> PWM.list()
PWM(pin: 21, freq=10000 Hz, duty=25% [1024], duty resolution=12 bits, channel=0, timer=0)
PWM(pin: 23, freq=10000 Hz, duty=9% [409], duty resolution=12 bits, channel=1, timer=0)
PWM(pin: 25, freq=2003 Hz, duty=25% [8192], duty resolution=15 bits, channel=2, timer=2)
PWM(pin: 27, freq=20000 Hz, duty=25% [512], duty resolution=11 bits, channel=3, timer=1)
PWM(pin: 4, freq=20000 Hz, duty=75% [1536], duty resolution=11 bits, channel=4, timer=1)
PWM(pin: 19, freq=10000 Hz, duty=50% [2048], duty resolution=12 bits, channel=5, timer=0)
PWM(pin: 22, freq=2003 Hz, duty=25% [8192], duty resolution=15 bits, channel=6, timer=2)
PWM(pin: 26, freq=10000 Hz, duty=75% [3072], duty resolution=12 bits, channel=7, timer=0)
pwm = machine.PWM(pin [, freq=f] [, duty=d] [, timer=tm])
Arg | Description |
---|---|
pin | esp32 GPIO number to be used as pwm output can be given as integer value or machine.Pin object |
freq | optional, default 5 kHz; pwm frequeny in Hz (1 - 40000000) |
duty | optional, default 50% kHz; pwm duty cycle in % (0 - 100) |
timer | optional, default 0; pwm timer (0 - 3) |
PWM channel is selected automatically from 8 available pwm channels.
Reinitialize the pwm channel
Arg | Description |
---|---|
freq | optional, if not given, the frequency is not changed |
duty | optional, if not given, the duty cycle is not changed |
timer | optional, if not given, the pwm timer is not changed |
Changing the frequency or timer will affect all pwm channels using the same timer.
Deinitialize and free the pwm channel, stop pwm output.
The echannel can be reinitializeded using pwm.init()
.
With no argument, return the current pwm channel frequency.
Set the new pwm frequency to ḟreq
Hz.
All pwm channels using the same timer will be affected.
With no argument, return the current pwm channel duty cycle.
Set the new pwm duty cycle to duty_perc
in %. The value can be given as float.
Pause the pwm channel timer, no pwm output will be present on pwm pin.
All pwm channels using the same timer will be affected.
Resume the pwm channel timer, pwm output will be present on pwm pin.
All pwm channels using the same timer will be affected.
List the characteristics of all active pwm channels.
from machine import PWM
# Init all 8 pwm channels to the defaults
pwm1=PWM(21)
pwm2=PWM(23)
pwm3=PWM(25)
pwm4=PWM(27)
pwm5=PWM(4)
pwm6=PWM(19)
pwm7=PWM(22)
pwm8=PWM(26)
# --> Image 1
# Set duty cycles of all pwm channels
pwm1.duty(10)
pwm2.duty(20)
pwm3.duty(30)
pwm4.duty(40)
pwm5.duty(50)
pwm6.duty(60)
pwm7.duty(70)
pwm8.duty(80)
# --> Image 2
# Reinitialize pwm5 to use timer 1 with frequency of 20 kHz
pwm5.init(freq=20000,timer=1)
# --> Image 3
# Set the frequency of pwm1 to 1 MHz
# All channels using the same timer (0) will be affected
# Duty cycles of all channels are preserved
pwm1.freq(1000000)
# --> Image 4
# Set the frequency of pwm5 to 2 MHz
pwm5.freq(2000000)
# --> Image 5
>>> PWM.list()
PWM(pin: 21, freq=1000000 Hz, duty=7% [5], duty resolution=6 bits, channel=0, timer=0)
PWM(pin: 23, freq=1000000 Hz, duty=18% [12], duty resolution=6 bits, channel=1, timer=0)
PWM(pin: 25, freq=1000000 Hz, duty=28% [18], duty resolution=6 bits, channel=2, timer=0)
PWM(pin: 27, freq=1000000 Hz, duty=37% [24], duty resolution=6 bits, channel=3, timer=0)
PWM(pin: 4, freq=2000000 Hz, duty=50% [16], duty resolution=5 bits, channel=4, timer=1)
PWM(pin: 19, freq=1000000 Hz, duty=57% [37], duty resolution=6 bits, channel=5, timer=0)
PWM(pin: 22, freq=1000000 Hz, duty=68% [44], duty resolution=6 bits, channel=6, timer=0)
PWM(pin: 26, freq=1000000 Hz, duty=78% [50], duty resolution=6 bits, channel=7, timer=0)
Image 1 Image 2 Image 3 Image 4 Image 5