PWMOut – Output a Pulse Width Modulated signal

PWMOut can be used to output a PWM signal on a given pin.

class pulseio.PWMOut(pin, *, duty_cycle=0, frequency=500, variable_frequency=False)

Create a PWM object associated with the given pin. This allows you to write PWM signals out on the given pin. Frequency is fixed after init unless variable_frequency is True.


When variable_frequency is True, further PWM outputs may be limited because it may take more internal resources to be flexible. So, when outputting both fixed and flexible frequency signals construct the fixed outputs first.

  • pin (Pin) – The pin to output to
  • duty_cycle (int) – The fraction of each pulse which is high. 16-bit
  • frequency (int) – The target frequency in Hertz (32-bit)
  • variable_frequency (bool) – True if the frequency will change over time

Simple LED fade:

import pulseio
import board

pwm = pulseio.PWMOut(board.D13)     # output on D13
pwm.duty_cycle = 2 ** 15            # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz

PWM at specific frequency (servos and motors):

import pulseio
import board

pwm = pulseio.PWMOut(board.D13, frequency=50)
pwm.duty_cycle = 2 ** 15                  # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz

Variable frequency (usually tones):

import pulseio
import board
import time

pwm = pulseio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True)
pwm.frequency = 880

Deinitialises the PWMOut and releases any hardware resources for reuse.


No-op used by Context Managers.


Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.


16 bit value that dictates how much of one cycle is high (1) versus low (0). 0xffff will always be high, 0 will always be low and 0x7fff will be half high and then half low.


32 bit value that dictates the PWM frequency in Hertz (cycles per second). Only writeable when constructed with variable_frequency=True.