MantisIDE

Fade an LED (PWM)

Guide by MantisIDE Team · Fri Nov 27 2026 · 0 points · 0 comments

Smoothly brighten and dim an LED with PWM.

A starter program from the MantisIDE Full IDE (Tools › Examples). Press Open in Full IDE to run it on your board, change it, and save it as your own.

# Smoothly brighten and dim an LED with PWM.
from machine import Pin, PWM
import time

pwm = PWM(Pin(2), freq=1000)

while True:
    for level in list(range(0, 65536, 2048)) + list(range(65535, -1, -2048)):
        pwm.duty_u16(level)
        time.sleep_ms(15)
Loading post…

Dialog