PyRasp

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:

Two PWM signals at the same frequency. At 50 percent duty the pin is on half of each pulse, and the average is half. At 25 percent duty the pulses are short, and the average is a quarter.

  • 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

WhatMicroPython
Make a PWM outputpwm = PWM(Pin(13), freq=1000)
Set brightnesspwm.duty_u16(32768) — 0 to 65535
Set pulse widthpwm.duty_ns(1_500_000) — nanoseconds
Change frequencypwm.freq(440)
Stop and release the pinpwm.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 seeWhyFix
The LED stays full/darkDuty outside 0–65535Use min/max or stay in range
The servo buzzes and jittersWrong frequencyfreq=50
The servo does not reach the endsPulse outside 0.5–2.5 msUse 500 000–2 500 000 ns
duty_ns does nothingFrequency is 0Set a frequency first
A damage warningToo much current through a buzzer or motorAdd the series resistor or driver the lesson specifies

Check yourself

  1. What does duty 0.25 look like to an LED?
  2. What frequency does a servo need?
  3. 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

Next: Ten things to build · Module 2 — Devices on a bus · The pinout · The machine reference