Raspberry Pi Pico Servo Simulator: Wiring, Code and a Live Circuit
This is an SG90 hobby servo wired to a Raspberry Pi Pico, sweeping from 0 to 180 degrees and back, live in your browser. The horn on the drawing turns as the sketch writes each angle.
Three wires: brown to GND, red to VBUS (5 V from USB) and orange to GP16 (physical pin 21). The Raspberry Pi Pico’s 3.3 V signal drives a 5 V servo without a level shifter.
- 3.3 V logic
- Library: Servo.h
- Signal on GP16 (physical pin 21)
- Powered from VBUS (5 V from USB)
Wiring the Servo to the Raspberry Pi Pico
Every connection in the circuit above, with the board’s own pin names.
| Servo pin | Raspberry Pi Pico pin | Why |
|---|---|---|
| Brown (GND) | GND | The signal is measured against this, so it must be shared with the board. |
| Red (V+) | VBUS (5 V from USB) | An SG90 wants 4.8 to 6 V. At 3.3 V it barely moves, so it takes the 5 V rail, not 3V3. |
| Orange (signal) | GP16 (physical pin 21) | Any GP pin; the RP2040 has a PWM slice behind every one. |
The servo runs from VBUS (5 V from USB) while its signal comes from a 3.3 V pin. That works: an SG90 reads anything above about 2 V as high, so no level shifter is needed. On a real bench, a servo under load can pull more current than USB gives; then power it from its own 5 V supply and join the grounds.
Pins to leave alone on the Raspberry Pi Pico
The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.
On a real bench
Solder headers on the Pico and press it into the breadboard across the center channel; the Servo goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include <Servo.h>
const int SERVO_PIN = 16; // signal, GP16 (physical pin 21)
Servo servo;
void setup() {
Serial.begin(115200);
servo.attach(SERVO_PIN); // 544 to 2400 us, 50 times a second
Serial.println("Sweeping");
}
void loop() {
for (int angle = 0; angle <= 180; angle += 2) {
servo.write(angle);
delay(15);
}
Serial.println("at 180");
for (int angle = 180; angle >= 0; angle -= 2) {
servo.write(angle);
delay(15);
}
Serial.println("back at 0");
}
Servo.h is the stock Arduino library, and it compiles here as written. attach() takes any pin on the Raspberry Pi Pico.
servo.attach(SERVO_PIN) starts a pulse every 20 ms. write(angle) sets its width: 544 microseconds for 0 degrees, 1472 for 90 and 2400 for 180, the library’s defaults and the simulated servo’s too.
The loops step two degrees every 15 ms, about 1.4 seconds for a sweep, which an SG90 can follow; it needs roughly 0.1 s per 60 degrees. Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.
Common mistakes with the Servo on the Raspberry Pi Pico
Powering the servo from 3V3
The Raspberry Pi Pico’s 3.3 V regulator is sized for the chip, not a motor, and an SG90 at 3.3 V jitters or stalls. The red wire goes to VBUS (5 V from USB).
Forgetting the common ground
With the servo on a separate supply and only the signal wire to the board, the servo has nothing to measure the pulse against and twitches at random. Connect the supply’s ground to the board’s GND.
Counting physical pins instead of GP numbers
The sketch says 15, which means GP15, and that is physical pin 20 on the header. Counting 15 pins down the side lands on a different GPIO, and the part never answers.
Questions and answers
Can I simulate a servo with a Raspberry Pi Pico online?
Yes. The circuit at the top of this page is a Raspberry Pi Pico with a servo, running in your browser on a simulated RP2040. The simulated servo reads the real pulse width the board makes, so a wrong angle in the code is a wrong angle on the drawing. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which Raspberry Pi Pico pins can drive a servo?
Any GP pin from GP0 to GP22 and GP26 to GP28. Each has a PWM slice channel behind it.
Why does my servo only turn about 160 degrees?
Many SG90s reach their end stops before 544 or 2400 microseconds. Find your servo’s real limits and pass them to attach(pin, min, max), for example attach(pin, 600, 2300).
How do I control a continuous rotation servo?
Same wiring and the same library: write(90) stops it, smaller angles turn it one way and larger ones the other, faster the further from 90. In the editor, set the servo’s mode property to continuous.
Build your own Servo project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.