Built-in project · Raspberry Pi Pico

MicroPython servo knob on a Pico: a pot points the servo

MicroPython on a Raspberry Pi Pico: a potentiometer on GP26 sets the angle of a servo on GP15, from 0 to 180 degrees. The whole build, a Raspberry Pi Pico and 2 more parts, 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 $7. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading an analog knob on the Pico
  • Converting a reading to an angle
  • How pulse width sets a servo's angle
  • Splitting code into a helper file and a main file
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 5

How it works

A potentiometer on GP26 gives the Pico a voltage from 0 to 3.3 V. The code reads it as a number from 0 to 65535 and turns that into an angle from 0 to 180 degrees. The servo's signal wire is on GP15, and the servo is powered from 5 V.

A hobby servo reads an angle from the width of a pulse sent 50 times a second. A short helper file, servo.py, handles that: 0.5 ms is 0 degrees and 2.5 ms is 180. The main loop sets the angle every 50 ms and prints it when the knob moves by more than one degree.

Parts list

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

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 pinConnects to
Hobby servo pin PWMRaspberry Pi Pico pin GP15
Potentiometer pin 2Raspberry Pi Pico pin GP26
Potentiometer pin 1Ground
Potentiometer pin 33.3 V
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
# Point the servo with a knob: the reading on GP26 (0 to 65535) becomes
# an angle from 0 to 180 degrees for the servo on GP15.
from machine import ADC
import time
from servo import Servo

knob = ADC(26)
servo = Servo(15)
last = -1

while True:
    angle = knob.read_u16() * 180 // 65535
    servo.angle(angle)
    if abs(angle - last) > 1:
        print("angle", angle)
        last = angle
    time.sleep_ms(50)
servo.py · Raspberry Pi Pico
# 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 twitches or does not move This project
    Power it from 5 V and tie its ground to the Pico's ground. The signal wire goes to GP15.
  • The angle jumps around This project
    Check the knob wiring: 3.3 V and ground on the outer pins, and the middle pin on GP26.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • 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.