from machine import Pin, PWM
import time
# Set up PWM on Pin 17 with a high frequency (1000Hz)
pwm = PWM(Pin(5))
pwm.freq(1000)
STEPS = 100
while True:
# --- RAMP UP (Triangle rising edge) ---
for i in range(STEPS + 1):
# MicroPython duty cycles go from 0 to 65535
duty = int((i / STEPS) * 65535)
pwm.duty_u16(duty)
time.sleep_ms(5)
# Optional: Un-comment the line below to make it Trapezoidal!
# time.sleep_ms(200)
# --- RAMP DOWN (Triangle falling edge) ---
for i in range(STEPS, -1, -1):
duty = int((i / STEPS) * 65535)
pwm.duty_u16(duty)
time.sleep_ms(5)
# Optional: Un-comment the line below to make it Trapezoidal!
# time.sleep_ms(200)