# Simple MicroPython script to fade the onboard LED with perceptual brightness scaling
# using PWM (Pulse Width Modulation) and gamma correction.
from machine import Pin, PWM
from time import sleep
import math
# Set up PWM on the onboard LED (GPIO 25)
pwm_led = PWM(Pin(25))
pwm_led.freq(1000)
# Gamma value for perceptual scaling (approx. 2.2 for human eye)
gamma = 2.2
# Number of steps for the fade (tweak for smoothness/performance)
steps = 100
# Precompute gamma-corrected values for fading
fade_values = [
int((math.pow(i / steps, gamma)) * 65535)
for i in range(steps + 1)
]
# Fade loop
while True:
# Fade in
for duty in fade_values:
pwm_led.duty_u16(duty)
sleep(0.01)
# Fade out
for duty in reversed(fade_values):
pwm_led.duty_u16(duty)
sleep(0.01)