1# Example using PWM to fade an LED.
2
3import time
4from machine import Pin, PWM
5
6
7# Construct PWM object, with LED on Pin(25).
8pwm = PWM(Pin(25))
9
10# Set the PWM frequency.
11pwm.freq(1000)
12
13# Fade the LED in and out a few times.
14duty = 0
15direction = 1
16for _ in range(8 * 256):
17    duty += direction
18    if duty > 255:
19        duty = 255
20        direction = -1
21    elif duty < 0:
22        duty = 0
23        direction = 1
24    pwm.duty_u16(duty * duty)
25    time.sleep(0.001)
26