from machine import Pin, PWM
import time
led_pin = Pin(15)
# Initialize PWM on the LED pin
pwm = PWM(led_pin)
pwm.freq(1000) # Set PWM frequency (1 kHz)
while True:
# Gradually increase brightness
for duty in range(0, 65535, 256): # 256-step increments for smooth fade
pwm.duty_u16(duty)
time.sleep(0.001) # This introduces a
#small delay of 0.01 seconds (10 milliseconds) between each step of the loop.The delay ensures that the brightness changes smoothly rather than instantly, creating a fading effect.
# Gradually decrease brightness
for duty in range(65535, -1, -256):
pwm.duty_u16(duty)
time.sleep(0.001)
# PWM operates with 16-bit resolution 2^16 =65536