Servo
A servo is a small geared motor with feedback that holds the angle you ask for. The shop's part is an SG90-style micro servo for $5: three wires, and the pulse width on the signal wire sets the horn's angle — about 0.5 ms for 0°, 2.4 ms for 180°, repeated every 20 ms. Unlike a DC motor it does not spin freely; command 90° and it turns there and stays. That makes it the motor for gates, pointers, pan-and-tilt brackets and robotic arms.
Pins
| Pin | What it is | Type |
|---|---|---|
| GND | Ground (brown) | ground |
| V+ | Supply 4.8–6 V (red) | power |
| SIG | Control pulse (orange) | io |
Servo()— Servo — $5- Pins: 3
The wires are colour-coded: brown to ground, red to a 5 V supply, orange to a GPIO. SIG only needs the GPIO's signal, never its current — the servo's own power comes from V+, which is why the servo is not a chain-shaped part and every pin is wired by name.
Wiring it in circuit.py
from wiring import *
pi = Pi()
servo = Servo()
servo.v_plus >> pi.V5 # 5 V: a moving servo draws hundreds of mA
servo.gnd >> pi.GND
servo.sig >> pi.GPIO18 # any GPIO; the hardware-PWM pins 12/13/18/19 jitter leastNote the pattern: the signal goes to a GPIO, but the power comes from the 5 V rail. gpiozero's Servo keeps the pulse train going in the background, so the program does not have to time pulses itself.
Driving it in main.py
from gpiozero import AngularServo
from time import sleep
servo = AngularServo(18, min_angle=0, max_angle=180, min_pulse_width=0.0005, max_pulse_width=0.0024)
while True:
servo.angle = 0
sleep(1)
servo.angle = 180
sleep(1)servo.angle is in degrees, not duty cycle. gpiozero's plain Servo assumes 1–2 ms pulses — the middle of this part's range — so pass min_pulse_width and max_pulse_width (matching the datafeed's 500 µs and 2400 µs defaults) to reach the full 0–180°.
Electrical notes
- Supply is 4.8–6 V; run it from
V+, never from 3V3 or a GPIO. At rest the servo draws about 10 mA; while moving about 250 mA; stalled, 650 mA. That last number is why a servo on the Pi's 5 V pin wants a good supply. - Timing, from the datasheet model: a pulse every 20 ms, 500–2400 µs wide, a 10 µs deadband, and a top speed of 0.1 s per 60° (600 °/s).
- The pulse width, not the duty cycle, carries the angle:
duty = pulse ÷ 20 ms. A 5 % duty at 50 Hz is a 1 ms pulse — 30°-ish on this unit, not "5 % power". - Below about 4.8 V the servo weakens; the simulator warns under 4.3 V and stops listening under 2.5 V.
- Software PWM (any GPIO) jitters, so the horn twitches; hardware PWM on GPIO12, 13, 18 and 19 is steadier.
Simulator vs real hardware
- The behaviour reads the pulse width from the PWM on
SIGat each solve, then slews the horn towards the matching angle at its rated speed in 10 ms steps. No pulses (or a frame rate outside 20–400 Hz, or a duty of 0 or 100 %) means "hold still". - Every part gets one fixed pair of pulse endpoints and a small per-part twitch (
jitterDeg, default ±0.5°) at the end of a move, like a real servo hunting for its target. - Current follows motion: the model draws the moving current while slewing and the idle current when parked. A jammed real servo would stall at 650 mA; the simulator has no mechanical load to jam on.
- gpiozero's
Servo/AngularServoclass is the supported driver;RPi.GPIO.PWMat 50 Hz works too. Real servos mostly accept 1–2 ms pulses, so the shop's 0.5–2.4 ms range is on the generous side.
Going deeper
- Primer: PWM — pulse train, frequency and duty cycle.
- The library: gpiozero covers
ServoandAngularServo. - Other motion parts: DC motor + L298N for continuous drive, stepper + ULN2003 for precise steps.
Further reading
- TowerPro SG90 micro servo datasheet (Components101 mirror) — the 0.5–2.4 ms pulse range and current figures the model follows.
- gpiozero: Servo and AngularServo —
angle,min_pulse_width,max_pulse_width. - SparkFun: Hobby Servo Tutorial — how a servo works inside and how to power one without browning out the board.