MicroPython servo sweep on a Pico, signal on GP16
MicroPython on a Raspberry Pi Pico: 50 Hz PWM on GP16 sweeps a servo from 0 to 180 degrees and back, one degree every 15 ms. The whole build, a Raspberry Pi Pico 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.50. On Mokxi they're free.
What you'll learn
- Driving a servo with 50 Hz PWM
- Pulse width as angle
- Using a small class from a second file
- Sweeping with a loop and a delay
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico
- Parts: 3
How it works
The servo's signal wire is on GP16, and it is powered from 5 V with a shared ground. A hobby servo reads its angle from the width of a pulse sent 50 times a second. servo.py makes those pulses: 0.5 ms for 0 degrees up to 2.5 ms for 180.
The main loop counts the angle up from 0 to 180, one degree every 15 ms, and prints "at 180". Then it counts back down to 0 and prints "at 0". It repeats forever.
Parts list
3 parts, plus the jumper wires. They retail for about $6.50; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × Power, 5 V
Kits that can build it
These 3 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 pin | Connects to |
|---|---|
| Hobby servo pin PWM | Raspberry Pi Pico pin GP16 |
| Hobby servo pin GND | Ground |
| Hobby servo pin VCC | 5 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.
# Servo sweep: the servo on GP16 turns from 0 to 180 degrees and back, a
# degree every 15 ms. servo.py, in the next tab, turns an angle into the
# pulse width the servo reads.
import time
from servo import Servo
servo = Servo(16)
while True:
for angle in range(0, 181):
servo.angle(angle)
time.sleep_ms(15)
print("at 180")
for angle in range(180, -1, -1):
servo.angle(angle)
time.sleep_ms(15)
print("at 0")
# servo.py: a hobby servo on a PWM pin.
# The servo wants a pulse every 20 ms (50 Hz); the pulse's width picks the
# angle: 0.5 ms is 0 degrees and 2.5 ms is 180 degrees.
from machine import Pin, PWM
class Servo:
def __init__(self, pin):
self.pwm = PWM(Pin(pin))
self.pwm.freq(50)
def angle(self, degrees):
degrees = max(0, min(180, degrees))
self.pwm.duty_ns(500_000 + int(degrees * 2_000_000 / 180))
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The servo does not move This project
The signal wire must be on GP16. Power goes to 5 V and ground must be shared with the Pico. - The sweep is too fast or slow This project
Change the 15 intime.sleep_ms(15). A bigger number gives a slower 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. - The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.