import machine
from time import sleep
# Set up PWM Pin
led1 = machine.Pin(0) # pin led is connected to
led_pwm1 = machine.PWM(led1) # led(pin0) is controlled with pwm. Led_pwm is only variable mentioned from here onwards
duty_step = 150 # duty cycle is steps of 65536 jumped per cycle, smaller number is smoother but slower
#Set PWM frequency
frequency = 5000
led_pwm1.freq (frequency)
try:
while True:
# Increase the duty cycle gradually
for duty_cycle in range(0, 65536, duty_step): # range(start, end, steps, steps can be an integer or a variable with an integer assigned)
led_pwm1.duty_u16(duty_cycle) # duty_u16 means 16 bit number, 65535 is the larges binary 16 bit number
# ^ this line is telling the led to take the duty step
sleep(0.005) # time waited between duty cycles
# Decrease the duty cycle gradually
for duty_cycle in range(65536, 0, -duty_step):
led_pwm1.duty_u16(duty_cycle)
sleep(0.005)
except KeyboardInterrupt:
print("Keyboard interrupt")
led_pwm1.duty_u16(0)
print(led_pwm1)
led_pwm1.deinit()