PWM Class

This document introduces the PWM class in the PinPong library, including how to initialize PWM output and configure parameters such as frequency and duty cycle.

PWM Class

Constructor

Creates and initializes a PWM (Pulse Width Modulation) to output PWM signals.

pwm0 = PWM(board, Pin(board, Pin.D6))
  • board: The board object created by the PinPong class. This parameter can be omitted if there is only one board.
  • Pin: The pin object created by the Pin class.

Methods

pwm0.freq()
freq(), When called with no arguments, it returns the frequency.
f = pwm0.freq()  # Get the PWM frequency
pwm0.freq(x)
freq(x), When called with an argument, it sets the PWM frequency to x.
pwm0.freq(1000)  # Set the PWM frequency to 1000
pwm0.duty()
duty(), When called with no arguments, it returns the duty cycle.
f = pwm0.duty()  # Get the PWM duty cycle
pwm0.duty(x)
duty(x), When called with an argument, it sets the PWM duty cycle, the range is 0-255.
pwm0.duty(127)  # Set the PWM duty cycle to 50%
pwm0.deinit()

Was this article helpful?

TOP