Build it step by step
0 of 6 doneWire the knob
Outer legs to 3V3(OUT) and GND, wiper to GP26.
Wire the servo
Signal to GP16, power from the separate supply, grounds joined.
Save the program as main.py
On the Pico.
Run it
The servo goes to the middle.
Turn the knob
The servo follows.
Change the limits
Find the safe ends for your servo.
How do you build a pico knob-controlled servo?
A Pico moves a hobby servo with a knob, using hardware PWM at 50 Hz from MicroPython, with the servo on its own supply. You need Raspberry Pi Pico H, SG90 Micro Servo, Potentiometer 10 kΩ and Breadboard 830 Tie Points. It takes 6 steps and about 120 minutes.
- Level
- Intermediate
- Time
- About 120 min
- Parts
- 4
Parts list
- Development BoardsBuy
- Motors & ActuatorsBuySG90 Micro Servo × 1
- PassivesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Pot outer legs | Pico 3V3(OUT) pin 36 and GND | Voltage divider |
| Pot wiper | Pico GP26 pin 31 (ADC0) | Analog input |
| Servo signal (orange or yellow) | Pico GP16 pin 21 | PWM output |
| Servo power (red) | Separate 5 V supply, not the Pico | A servo can pull hundreds of milliamps |
| Servo ground (brown or black) | Supply ground and Pico GND | Common ground |
Code
A hobby servo expects a pulse every 20 milliseconds, which is 50 hertz, and the width of the pulse sets the angle: about 1 ms for one end, 1.5 ms for the middle and 2 ms for the other end. The Pico makes these pulses with a hardware PWM block that counts on its own, so the arm stays steady while the program does other work. In MicroPython the duty is a 16-bit number, so a pulse of a given width is that width divided by the 20 ms period, times 65535. The knob is read with the ADC and smoothed a little so the arm does not shake. Small servos accept the 3.3 V signal, but the servo itself must have its own 5 V supply, since a moving servo can draw far more current than the Pico USB socket should give. The grounds of the supply and the Pico must be joined. If the arm buzzes at the ends, narrow the pulse limits a little.
import time
from machine import Pin, PWM, ADC
pot = ADC(26)
servo = PWM(Pin(16))
servo.freq(50) # one pulse every 20 ms
MIN_US, MAX_US = 1000, 2000 # pulse width range in microseconds
def set_pulse(us):
servo.duty_u16(int(us * 65535 / 20000)) # fraction of the 20 ms period
smooth = pot.read_u16()
while True:
smooth = int(0.9 * smooth + 0.1 * pot.read_u16()) # gentle smoothing
us = MIN_US + (MAX_US - MIN_US) * smooth // 65535
set_pulse(us)
time.sleep_ms(20)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Knob in the middle | Servo near the centre |
| Knob to one end | Servo moves to one limit |
| Knob to the other end | Servo moves to the other limit |
| Knob moved slowly | Smooth movement |
| Servo on its own supply | Pico does not reset when the servo moves |
If it does not work
Servo twitches or the Pico resets
Power the servo from its own 5 V supply and join the grounds.
Servo does not move
Check GP16 and the signal wire colour.
Servo hums at the ends
Reduce MIN_US and MAX_US slightly.
Jittery movement
Add a 100 nanofarad capacitor from GP26 to GND.
Program does not start on power-up
Save it on the Pico as main.py.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico knob-controlled servo?
Raspberry Pi Pico H, SG90 Micro Servo, Potentiometer 10 kΩ and Breadboard 830 Tie Points.
How long does the pico knob-controlled servo take to build?
About 120 minutes for a intermediate maker, in 6 steps.
How do I know the pico knob-controlled servo works?
Knob in the middle: Servo near the centre. Knob to one end: Servo moves to one limit. Knob to the other end: Servo moves to the other limit.
Servo twitches or the Pico resets: what should I check?
Power the servo from its own 5 V supply and join the grounds.
Servo does not move: what should I check?
Check GP16 and the signal wire colour.
Servo hums at the ends: what should I check?
Reduce MIN_US and MAX_US slightly.
Jittery movement: what should I check?
Add a 100 nanofarad capacitor from GP26 to GND.
