from machine import Pin, PWM
from time import sleep
# Initialize the pin that will be used for PWM
pwm_pin = Pin(15)
# Set up PWM on the selected pin using the LEDC module
pwm = PWM(pwm_pin)
# Set the PWM frequency and resolution (number of bits)
pwm.freq(5000) # Set frequency to 5kHz
pwm.duty(1023) # Set duty cycle to 100% using 10-bit resolution (1024 steps)
sleep(2)
# Change the duty cycle to control the width of the pulse
pwm.duty(768)
sleep(2)
pwm.duty(512) # Set duty cycle to 50% using 10-bit resolution (1024 steps)
sleep(2)
pwm.duty(256) # Set duty cycle to 25%
sleep(2)
pwm.duty(0) # Set duty cycle to 0%