from machine import Pin, PWM
# Step 1: Set up the LED on GPIO Pin 5 with PWM
led = PWM(Pin(5))
led.freq(1000) # Set the frequency to 1kHz (this controls how fast the LED blinks)
while True:
try:
# Step 2: Get user input for brightness (0 to 100)
value = input("Enter brightness (0-100): ")
# Step 3: Convert the input value to an integer and scale it to PWM duty cycle range (0-65535)
brightness = int(value)
if brightness < 0: brightness = 0
if brightness > 100: brightness = 100
# Step 4: Convert brightness (0-100) to PWM duty cycle (0-65535)
duty = int(brightness * 65535 / 100)
# Step 5: Set the LED brightness by adjusting the PWM duty cycle
led.duty_u16(duty)
except:
print("Invalid input")