Skip to content
Boris Lovosevic edited this page Jan 13, 2018 · 19 revisions

machine Module

Class PWM


This class includes PWM support using ESP32 LEDC peripheral

Features

  • 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


Create the pwm instance object

`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
duty optional, default 50% kHz; pwm duty cycle in % (0 - 100)
timer optional, default 0; pwm timer

PWM channel is selected automaticall from 8 available pwm channels.

pwm.init([ freq=f] [, duty=d] [, timer=tm])`

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.

Clone this wiki locally