6. Dim and move
You will be able to
- use PWM to dim an LED,
- set the frequency and the pulse width,
- drive a servo to an angle,
- play a tone on a passive buzzer.
You need: an LED, a servo, a passive buzzer (one at a time is fine). Problems: PWM fade, Servo sweep, A steady tone, Three notes, Point where you look, Motor speed.
Wiring note. A servo's power pin goes to VBUS (5 V), its ground to GND, and only its signal wire goes to a GPIO. Do not power a servo from 3V3: the Pico cannot supply the current.
The idea
A pin can only be high or low. Pulse-width modulation (PWM) switches it on and off so fast that the part sees the average:

- Frequency is how many pulses per second (Hz).
- Duty is the fraction of each pulse that is high: 0 = always off, 1 = always on.
- An LED sees brightness; a servo sees the width of the pulse; a buzzer hears the frequency.
The exact tools
| What | MicroPython |
|---|---|
| Make a PWM output | pwm = PWM(Pin(13), freq=1000) |
| Set brightness | pwm.duty_u16(32768) — 0 to 65535 |
| Set pulse width | pwm.duty_ns(1_500_000) — nanoseconds |
| Change frequency | pwm.freq(440) |
| Stop and release the pin | pwm.deinit() |
Build it
Dim an LED
from machine import Pin, PWM
import utime
pwm = PWM(Pin(13), freq=1000)
while True:
for duty in range(0, 65536, 8192):
pwm.duty_u16(duty)
utime.sleep_ms(200)
The LED climbs from off to full in eight steps, then starts again.
Point a servo
A servo expects 50 Hz and a 0.5–2.5 ms pulse:
from machine import Pin, PWM
import utime
pwm = PWM(Pin(12), freq=50)
for width in (500_000, 1_500_000, 2_500_000):
pwm.duty_ns(width)
utime.sleep(1)
Play a tone
from machine import Pin, PWM
import utime
pwm = PWM(Pin(13), freq=262, duty_u16=32768) # middle C, half duty
utime.sleep(1)
pwm.freq(392) # G
utime.sleep(1)
pwm.duty_u16(0) # silence
Fun fact. The notes 262, 330 and 392 Hz are C, E and G — a major chord. Music is just frequencies in a pattern, and a buzzer is just a speaker that knows one trick.
What you should see
- The fade: a smooth staircase in brightness.
- The servo: three positions, one second apart.
- The tone: a note, a higher note, then silence.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| The LED stays full/dark | Duty outside 0–65535 | Use min/max or stay in range |
| The servo buzzes and jitters | Wrong frequency | freq=50 |
| The servo does not reach the ends | Pulse outside 0.5–2.5 ms | Use 500 000–2 500 000 ns |
duty_ns does nothing | Frequency is 0 | Set a frequency first |
| A damage warning | Too much current through a buzzer or motor | Add the series resistor or driver the lesson specifies |
Check yourself
- What does duty 0.25 look like to an LED?
- What frequency does a servo need?
- Why does the buzzer go silent with
duty_u16(0)?
Answers: a quarter brightness · 50 Hz · no pulses means no sound.
Remember
- PWM = fast switching; duty is the average, frequency is the pulse rate.
- LEDs use brightness; servos use pulse width; buzzers use frequency.
deinit()releases the pin.
Go further
- Fade up and down — PWM fade.
- Sweep a servo end to end — Servo sweep.
- Play a three-note doorbell — Doorbell.
- Let a divider set the servo angle — Point where you look.
Next: Ten things to build · Module 2 — Devices on a bus · The pinout · The machine reference