1 2 3 4 5 6 7 |
from machine import Pin, PWM pwm16 = PWM(Pin(16)) # create PWM object from GPIO 16 pwm16.freq(1000) # 1 kHz pwm16.duty(256) # duty cycle = 256/1024 * 100 = 25% # pwm16 = PWM(Pin(16), freq=1000, duty=256) # short Version |
The maximum PWM frequency is 40 MHz. However, due to the timer frequency of the ESP32 (80 MHz), you only get the full 10 bit resolution up to the following PWM frequency:

For larger frequencies, you can calculate the actual resolution as follows:

For example, if you choose 20 MHz as frequency, you only have a resolution of 4, i.e. you can set the duty cycles 0, 25, 50 and 75% by passing values in the ranges 0-255, 256-511, 512-767 or 768-1023 to pwm.duty()
.