Getting Started with Micro Servo Motors and Raspberry Pi
If you’ve ever watched a robotic arm pivot with surgical precision or a camera gimbal smoothly track a moving subject, you’ve witnessed the quiet magic of a micro servo motor. These tiny actuators—often no bigger than a matchbox—are the unsung heroes of the maker world. And when you pair them with a Raspberry Pi, you unlock a playground of interactive projects: from animatronic eyes that follow you across the room to automated plant waterers that tilt a spout with millimeter accuracy.
But here’s the catch: micro servos are analog creatures in a digital world. Your Pi speaks in 1s and 0s, while a servo speaks in pulses of electricity measured in microseconds. Bridging that gap is where the real fun begins. In this guide, I’ll walk you through everything you need to know—hardware wiring, software libraries, power pitfalls, and pro-level tuning—so you can go from blinking an LED to commanding a mini robotic claw in under an hour.
Why Micro Servo Motors? (And Why They’re Not Just “Small” Servos)
Let’s clear up a common misconception. A “micro servo” isn’t simply a scaled-down version of a standard servo. It’s a distinct class of actuator designed for low-voltage, low-torque, high-cycle applications. The classic SG90 (the unofficial starter servo) weighs just 9 grams, runs on 3.3–5V, and delivers about 1.8 kg·cm of stall torque. That’s enough to lift a small plastic lever, rotate a lightweight sensor turret, or flap a paper bird’s wing—but not enough to break a gearbox if you stall it.
What makes micro servos special for Pi projects:
- Low current draw: A single SG90 at idle pulls ~10mA, and peaks around 250mA under load. Your Pi’s 5V rail can handle two or three of these if you’re careful (more on that later).
- Closed-loop feedback: Unlike stepper motors, servos have an internal potentiometer that reports the shaft’s absolute position. You command a target angle; the servo’s onboard circuit does the PID correction. Your Pi doesn’t need to track steps—just send a pulse width and walk away.
- Zero maintenance: No encoders to wire, no driver boards to solder. Three wires (power, ground, signal) and you’re in business.
The catch? Micro servos are not continuous rotation out of the box. Standard ones sweep 0–180°. But you can buy “continuous rotation” variants (like the FS90R) that turn your servo into a tiny gear motor with speed control. We’ll cover both modes later.
The Three-Wire Secret: Decoding Your Servo’s Cable
Before you touch a single jumper wire, let’s decode that three-color ribbon cable. It’s not standardized across all brands, but 95% of micro servos follow this convention:
| Wire Color (most common) | Function | Pi Pin Connection | |--------------------------|----------|-------------------| | Brown or Black | Ground (GND) | Any GND pin (e.g., Pin 6, 14, 20, 30, 34, 39) | | Red or Orange | Power (VCC) | 5V pin (e.g., Pin 2 or 4) or external 5V supply | | Yellow or White | Signal (PWM) | Any GPIO pin (e.g., GPIO18 = Pin 12) |
Critical warning: Do not power the servo from the Pi’s 3.3V rail. Micro servos need 4.8–6V for reliable torque. The 3.3V rail can’t supply enough current, and you’ll get jittery, weak movements—or worse, a brownout that reboots your Pi.
The Power Supply Trap (And How to Avoid It)
Here’s where most beginners hit a wall. You plug a servo into your Pi’s 5V pin, run a test script, and the Pi suddenly reboots or the servo twitches violently. Why? Because a stalled servo can draw 500mA+, and the Pi’s 5V rail is shared with the USB ports, HDMI, and the board’s own logic. When the servo demands a burst of current, the voltage sags below 4.75V, and the Pi’s under-voltage detector triggers a shutdown.
The professional fix: Use a separate 5V/2A power supply for the servo (or a 4×AA battery pack giving ~6V). Connect the servo’s ground to the Pi’s ground (essential for signal reference), but connect its power directly to the external supply. This isolates the servo’s current spikes from the Pi’s delicate 3.3V logic.
For a quick test with one servo, you can use the Pi’s 5V pin—just don’t expect to run three servos simultaneously. If you’re building a hexapod later, buy a dedicated UBEC or a 5V/5A switching regulator.
Software Setup: Two Roads to PWM
Now that your servo is wired up (and powered safely), let’s talk software. The Raspberry Pi has two ways to generate the 50Hz PWM signal (20ms period) that servos crave:
- Hardware PWM via the
pigpiolibrary (uses the Pi’s dedicated PWM hardware on GPIO18). - Software PWM via the
RPi.GPIOlibrary (bit-bangs the signal, works on any GPIO).
Which should you use? If you want smooth, jitter-free motion, use pigpio. It uses the Pi’s DMA (Direct Memory Access) controller to generate precise pulses without loading the CPU. Software PWM on RPi.GPIO is fine for slow hobby projects, but you’ll notice a slight stutter if you try to sweep the servo continuously.
Step 1: Install the Libraries
Open a terminal on your Pi (or SSH in) and run:
bash sudo apt update sudo apt install python3-pigpio python3-rpi.gpio sudo systemctl enable pigpiod # Enables the pigpio daemon on boot sudo systemctl start pigpiod
The pigpiod daemon runs in the background and lets Python talk to the hardware PWM via socket commands. If you prefer not to run it as a service, you can call pigpio.pi() directly in your script.
Step 2: The Classic “Sweep” Test (Using pigpio)
Create a file called servo_sweep.py:
python import pigpio import time
SERVO_PIN = 18 pi = pigpio.pi() # Connect to local Pi
Set the PWM frequency to 50Hz (20ms period)
pi.setPWMfrequency(SERVO_PIN, 50)
Define pulse widths in microseconds 500us = 0 degrees, 1500us = 90 degrees, 2500us = 180 degrees But most micro servos respond well to 1000-2000us range
But most micro servos respond well to 1000-2000us range
def setangle(angle): # Map 0-180 degrees to 500-2500us pulsewidth = 500 + (angle / 180.0) * 2000 pi.setservopulsewidth(SERVOPIN, pulsewidth)
try: while True: for angle in range(0, 181, 5): # 0 to 180 in steps of 5 setangle(angle) time.sleep(0.05) for angle in range(180, -1, -5): setangle(angle) time.sleep(0.05) except KeyboardInterrupt: pass finally: pi.setservopulsewidth(SERVO_PIN, 0) # Stop sending pulses pi.stop()
Run it with python3 servo_sweep.py. Your servo should sweep back and forth smoothly. Notice the set_servo_pulsewidth method—that’s the magic function. It takes a value in microseconds, and the pigpio daemon handles the precise timing.
Step 3: The “Angle Mapping” Gotcha
Not all servos respond identically to the same pulse widths. An SG90 typically goes to 0° at 500µs and 180° at 2500µs, but some clones only respond between 1000µs and 2000µs. If your servo buzzes or hits its mechanical limit early, adjust the mapping:
python def safe_set_angle(angle, min_pulse=500, max_pulse=2500): pulse = min_pulse + (angle / 180.0) * (max_pulse - min_pulse) pi.set_servo_pulsewidth(SERVO_PIN, max(500, min(pulse, 2500)))
Always test your servo’s actual range with a manual pulse sweep (e.g., set pulse to 1000, 1500, 2000) and observe the physical limits. Pushing beyond 2500µs can strip the internal gears.
Beyond 180°: Continuous Rotation Servos
What if you want your servo to spin a wheel instead of sweeping an arm? That’s where continuous rotation servos (like the FS90R) come in. They’re internally modified to ignore the position potentiometer and instead use pulse width to control speed and direction:
- 1500µs = full stop
- 1000µs = full speed clockwise
- 2000µs = full speed counter-clockwise
- Values in between = proportional speed
The wiring is identical, and you can reuse the same pigpio code—just change your interpretation:
python def set_speed(speed_percent): # speed_percent: -100 (full reverse) to +100 (full forward) pulse = 1500 + (speed_percent * 5) # 5us per percent pi.set_servo_pulsewidth(SERVO_PIN, pulse)
This makes micro servos perfect for small differential-drive robots. Two continuous servos + a caster wheel + a Pi Zero W = a tiny rover that can roam your desk.
Building a Real Project: The Pan-Tilt Camera Mount
Let’s put theory into practice. A classic first servo project is a pan-tilt mechanism—two servos stacked to move a camera (or a laser pointer) in two axes. You’ll need:
- 2× SG90 micro servos
- A pan-tilt bracket (cheap plastic kits from Amazon, or 3D-print your own)
- A small camera module (or a paper cutout for testing)
- Your Pi with the
pigpiosetup from above
Wiring Diagram
| Servo | Power | Ground | Signal | |-------|-------|--------|--------| | Pan (horizontal) | External 5V | Common GND | GPIO18 (Pin 12) | | Tilt (vertical) | External 5V | Common GND | GPIO17 (Pin 11) |
Use a breadboard to distribute the external 5V and GND to both servos. Do not daisy-chain power from one servo’s red wire to the other—use a shared bus.
The Control Script
python import pigpio import time
pi = pigpio.pi() PANPIN = 18 TILTPIN = 17
pi.setPWMfrequency(PANPIN, 50) pi.setPWMfrequency(TILTPIN, 50)
def moveservo(pin, angle, minpulse=500, maxpulse=2500): pulse = minpulse + (angle / 180.0) * (maxpulse - minpulse) pi.setservopulsewidth(pin, pulse)
Home position
moveservo(PANPIN, 90) moveservo(TILTPIN, 90) time.sleep(1)
Sweep pan left and right while tilting up and down
for panangle in range(30, 151, 2): tiltangle = 90 + int(30 * (panangle - 90) / 60) # Simple diagonal moveservo(PANPIN, panangle) moveservo(TILTPIN, tilt_angle) time.sleep(0.02)
pi.setservopulsewidth(PANPIN, 0) pi.setservopulsewidth(TILTPIN, 0) pi.stop()
This creates a smooth diagonal sweep. For a more organic motion, use a sine wave to modulate both angles:
python for t in range(0, 360, 2): pan = 90 + int(30 * math.sin(math.radians(t))) tilt = 90 + int(20 * math.cos(math.radians(t * 2))) move_servo(PAN_PIN, pan) move_servo(TILT_PIN, tilt) time.sleep(0.02)
Advanced Tuning: Eliminating Jitter and Extending Servo Life
You’ve got the basics working, but your servo has a slight tremor at rest or buzzes when holding position. Here’s how to fix the most common issues:
Problem 1: Servo Jitter at Standstill
Cause: The pulse width is drifting by a few microseconds, or your power supply is noisy.
Fixes: - Use pigpio’s hardware PWM (you already are). Software PWM is the usual culprit. - Add a 100–470µF electrolytic capacitor across the servo’s power and ground terminals (close to the servo). This smooths out voltage dips during rapid direction changes. - If you’re using a battery pack, replace it with a regulated bench supply. Alkaline batteries sag under load.
Problem 2: Servo Buzzes But Doesn’t Move
Cause: The signal wire is too long (over 20cm) or has high resistance, causing the pulse to degrade.
Fixes: - Shorten the wire to under 10cm. - Use a shielded twisted pair for the signal and ground. - Add a 1kΩ resistor in series with the signal line to dampen ringing.
Problem 3: Overheating When Holding Position
Cause: The servo is actively fighting against gravity or a load, drawing constant current.
Fixes: - Reduce the holding torque by mechanically balancing the load (add a counterweight). - Use a “relax” function that sets pulse width to 0 after reaching the target angle. The servo will free-wheel, but you’ll save power. Only do this if your application doesn’t need to hold position. - Upgrade to a metal-gear servo (like the MG90S) if you’re repeatedly stalling.
Pro Tip: Calibrate Your Servo’s Actual Range
Every servo has a slightly different neutral point (the pulse width that centers it at 90°). Write a quick calibration script that lets you adjust the pulse width interactively using a keyboard:
python import pigpio import readchar
pi = pigpio.pi() PULSE = 1500 pi.setPWMfrequency(18, 50) pi.setservopulsewidth(18, PULSE)
print("Use A/D to decrease/increase pulse, S to save, Q to quit") while True: key = readchar.readkey() if key == 'a': PULSE -= 10 elif key == 'd': PULSE += 10 elif key == 's': print(f"Saved neutral pulse: {PULSE}us") break elif key == 'q': break pi.setservopulsewidth(18, PULSE) print(f"Pulse: {PULSE}us") pi.stop()
Save the found neutral value to a config file for your final project.
Scaling Up: Controlling Multiple Servos (Without a Servo HAT)
You can control up to 26 servos with a single Pi using software PWM, but the CPU load will spike and you’ll get jitter. For serious multi-servo projects (like a robot arm with 6 DOF), you have two better options:
PCA9685 Servo Driver Board (I2C): This 16-channel board handles all the PWM generation on its own. You just send I2C commands from the Pi. It’s the standard for any project with more than 4 servos. Wire it via the Pi’s I2C pins (GPIO2 for SDA, GPIO3 for SCL), and you can daisy-chain up to 62 boards.
Use
pigpio’s wave chains: This advanced feature lets you pre-program a sequence of servo moves and replay them with precise timing, freeing up your Python script to do other things. It’s overkill for beginners but worth knowing about.
For a quick 4-servo test without extra hardware, just assign four GPIO pins and use pi.set_servo_pulsewidth on each. The pigpio daemon can handle multiple servos on different pins simultaneously, as long as they share the same 50Hz frequency.
Common Pitfalls That Burn Beginners (And How to Dodge Them)
- Forgetting the common ground: If your external power supply isn’t connected to the Pi’s GND, the signal pulse has no reference and the servo will behave erratically. Always tie the grounds together.
- Using GPIO 14/15 (UART): These pins are often used for serial console output. If you plug a servo signal there, you’ll get garbage data. Stick to GPIO 18, 17, 22, 23, 24, 25 for PWM.
- Running the servo before the Pi finishes booting: If your script runs at boot and the servo tries to move before the 5V rail stabilizes, it can cause a brownout. Add a 2-second delay in your script or use a relay to cut servo power until the Pi is ready.
- Assuming 180° is exactly 180°: The mechanical limit is often 180°, but the electrical range (500–2500µs) might give you 170° or 190° depending on the brand. Always test with a protractor if precision matters.
Taking It Further: Sensor Feedback and Closed-Loop Control
A bare servo is an open-loop system—you command 90°, and you trust it goes there. But what if you want to know the actual position? For micro servos, you can hack the internal potentiometer (the one used for feedback) to read its voltage via an ADC (like the MCP3008). This gives you absolute position feedback, enabling:
- Stall detection: If the servo is blocked, the pot voltage won’t change, and you can trigger a stop command.
- Adaptive grip: In a robotic gripper, you can sense when the jaws touch an object by monitoring the current draw (via a current sensor) or the position error.
Here’s a quick sketch using an MCP3008 on SPI (channel 0):
python import spidev import pigpio
spi = spidev.SpiDev() spi.open(0, 0) spi.maxspeedhz = 1000000
def read_channel(channel): # MCP3008 protocol adc = spi.xfer2([1, (8 + channel) << 4, 0]) return ((adc[1] & 3) << 8) + adc[2]
Read servo pot (connect middle wire of pot to CH0)
position = read_channel(0) voltage = position * 3.3 / 1023
Convert voltage to angle (depends on your servo's pot range)
This is a deep rabbit hole, but it transforms your servo from a blind actuator into a smart one.
Final Thoughts (But Not a Conclusion)
You’ve now got the tools to make your Pi wiggle, wave, point, grip, and roam. The micro servo motor is the perfect first actuator because it hides all the complexity of motor control behind a simple pulse interface—yet it rewards you with immediate, tactile feedback.
Start with the sweep test. Then build the pan-tilt mount. Then break things—stall a servo, watch it heat up, learn why the current limit matters. The mistakes you make with a $2 servo will save you from burning out a $50 motor later.
Your next project is waiting. Grab an SG90, a breadboard, and let your Pi flex its tiny muscles. The only limit is how many GPIO pins you’re willing to sacrifice.
Copyright Statement:
Author: Micro Servo Motor
Source: Micro Servo Motor
The copyright of this article belongs to the author. Reproduction is not allowed without permission.
Recommended Blog
- Building a Servo-Powered Automated Sorting Robot with Raspberry Pi and Sensors
- Creating a Servo-Controlled Automated Sorting Machine with Raspberry Pi and Sensors
- Using Raspberry Pi to Control Servo Motors in IoT Applications
- Using Raspberry Pi to Control Servo Motors in Automated Sorting Systems
- How to Control Servo Motors Using Raspberry Pi and the RPi.GPIO Library for Industrial Applications
- How to Control SG90 Servo Motors Using Raspberry Pi
- How to Calibrate Servo Motors for Precise Control with Raspberry Pi
- Using Raspberry Pi to Control Servo Motors in Automated Packaging and Labeling Systems
- How to Control Servo Motors Using Raspberry Pi and the pigpio Library for Precision Robotics
- Implementing Servo Motors in Raspberry Pi-Based Automated Sorting and Packaging Systems
About Us
- Lucas Bennett
- Welcome to my blog!
Hot Blog
- How to Build a Remote-Controlled Car with Telemetry Sensors
- Micro Servo Motor Buying Guide: What to Look for and Where to Buy
- How to Build a Remote-Controlled Car with a Rack and Pinion Steering System
- How to Control SG90 Servo Motors Using Raspberry Pi
- Specification Declared Speed (s/60°) vs Real Time Tests
- How to Select Micro Servos for RC Airplanes & Park Flyers
- Choosing the Right Micro Servo Motor Based on Price and Performance
- Designing a Micro Servo Robotic Arm for Military Applications
- High Precision Micro Servos for Scale RC Airplanes
- What Voltage and Power Do Micro Servo Motors Require?
Latest Blog
- How to Use Raspberry Pi to Control Servo Motors in CNC Machines
- Getting Started with Micro Servo Motors and Raspberry Pi
- Smart Shelf Displays using Servo-Controlled Tilt Mechanics
- Micro Servo Motor Price Comparison: Which Brands Offer the Best Deals?
- Micro Servo Motors in Precision Positioning Systems: Innovations and Trends
- Building a Servo-Powered Automated Sorting Robot with Raspberry Pi and Sensors
- Diagnosing and Fixing RC Car ESC Throttle Limiting Issues
- Micro Servos in RC Boats: Handling Wave Impacts & Shock Loads
- How to Calibrate Micro Servo Motors for Accurate Movement
- What Is Inside a Micro Servo Motor? Components and Functions
- Micro Servo Motor Settings for Quiet Operation at Night
- How Micro Servo Motors Prevent Overshooting Position
- Effects of Shock & Impact on Micro Servo Gimbals after Hard Landings
- The Relationship Between Motor Torque and Efficiency
- Top 10 Micro Servo Motors Under $10
- Creating a Servo-Controlled Automated Sorting Machine with Raspberry Pi and Sensors
- The Impact of Gear Design on Servo Motor Efficiency
- Micro Servo Motor Control with ROS (Robot Operating System)
- The Impact of Motor Speed on Heat Generation and Dissipation
- BEGE's Micro Servo Motors: Meeting the Demands of Modern Industry