from machine import Pin, PWM # import Pin and PWM classes
import time # import time module
# set PWM
pwm = PWM(Pin(15)) # create PWM object from Pin15
pwm.freq(10000) # set PWM frequency to 10kHz
try: # if have no error, run the program between try: and except:
while True: # create an infinite loop
for i in range(0, 65535): # repeat 65535 times
pwm.duty_u16(i) # set PWM duty cycle
time.sleep_us(100) # sleep 100 microseconds
for i in range(65535, 0, -1): # repeat 65535 times
pwm.duty_u16(i) # set PWM duty cycle
time.sleep_us(100) # sleep 100 microseconds
except: # if have error, run the program between except:
pwm.deinit() # turn off PWM