from machine import Pin, PWM
import time
# Pin definition
led = Pin(15, Pin.OUT) # Use GPIO15 (you can change this pin)
# PWM setup on the pin
pwm = PWM(led)
pwm.freq(1000) # Frequency in Hz (e.g., 1 kHz)
pwm.duty_u16(0) # Initially off (0% duty cycle)
# Variables for PWM control
fade_amount = 1000 # How much to increase/decrease brightness
brightness = 0 # Start with 0% brightness
while True:
pwm.duty_u16(brightness) # Set the PWM duty cycle (brightness)
# Adjust brightness
brightness += fade_amount
# Reverse direction at the end points
if brightness <= 0 or brightness >= 65535:
fade_amount = -fade_amount
# Wait before updating
time.sleep(0.03)