Skip to content

PWM with 16-bit alternating

Initialization (pwm_RP2040)

What it does: Creates a PWM output on a pin input, with a frequency in Hz and an alternating hollow in the range 0-65535. Zero means zero shift, 65535 full. The simulator also uses this variant.

pwm_RP2040

The generator sets the ID number according to the pin when creating the code. Select the corresponding object in the subsequent blocks; don't initialize one pin repeatedly each time you go through the loop.

from machine import Pin, PWM
pwm1 = PWM(Pin(1), freq=1000, duty_u16=32768)

Shift change (pwm.duty_u16)

What it does: Sets the already created PWM object of the selected ID to a new duty value. This block itself does not generate PWM. Keep the value in the range 0-65535.

pwm.duty_u16

pwm1.duty_u16(int(32768))

Both blocks are command and do not return a value. Frequency and shutdown are handled by common blocks PWM frequency and PWM deinit. Do not confuse this scale with PWM duty which calls a different method.