Built-in project · Raspberry Pi Pico W

Pico W servo sweep on GP15

MicroPython on a Pico W: 50 Hz PWM on GP15 sweeps an SG90 servo from 0 to 180 degrees and back, a pulse of 0.5 to 2.5 ms. The whole build, a Raspberry Pi Pico W and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $8.50. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a hobby servo reads a pulse width
  • Setting PWM frequency with freq(50)
  • Converting degrees to a pulse width and then a duty
  • Powering a servo from 5 V with a shared ground
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 3

How it works

A hobby servo reads a pulse every 20 milliseconds, which is 50 Hz. A 0.5 ms pulse sends it to 0 degrees and a 2.5 ms pulse to 180 degrees. GP15 makes that pulse, and the servo is powered from 5 V.

The loop steps the angle from 0 to 180 in 5 degree jumps, pausing 20 milliseconds between them, and prints at 180. Then it steps back to 0 and prints at 0.

Parts list

3 parts, plus the jumper wires. They retail for about $8.50; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Hobby servo pin PWMRaspberry Pi Pico W pin GP15
Hobby servo pin GNDGround
Hobby servo pin VCC5 V

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Swinging servo: a servo reads a pulse every 20 ms (50 Hz). 0.5 ms is
# 0 degrees and 2.5 ms is 180, so angle() turns degrees into a duty.
from machine import Pin, PWM
import time

servo = PWM(Pin(15))
servo.freq(50)

def angle(deg):
    pulse_us = 500 + deg * 2000 // 180
    servo.duty_u16(pulse_us * 65535 // 20000)

while True:
    for deg in range(0, 181, 5):
        angle(deg)
        time.sleep_ms(20)
    print("at 180")
    for deg in range(180, -1, -5):
        angle(deg)
        time.sleep_ms(20)
    print("at 0")

Code walkthrough

The key functions of the sketch, and what each one does.

angle main.py

Turns degrees into a PWM duty. First it works out the pulse width in microseconds: 500 plus the angle spread over 2000. Then it scales that against the 20000 microsecond period into a 16 bit value for servo.duty_u16.

def angle(deg):
    pulse_us = 500 + deg * 2000 // 180
    servo.duty_u16(pulse_us * 65535 // 20000)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The servo does not move This project
    Check that the PWM wire goes to GP15, power goes to 5 V and ground is shared with the Pico.
  • The servo moves in jerky steps This project
    The step of 5 degrees in range(0, 181, 5) is chunky. Use a smaller step for a smoother sweep.
  • The servo jitters, twitches or resets the board when it moves. Hobby servo
    Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply.
  • The servo hums at the end of its travel or will not reach full angle. Hobby servo
    Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.