How to Control Servo Motors Using Raspberry Pi and the RPi.GPIO Library

Micro Servo Motor with Raspberry Pi / Visits:13

Why Micro Servo Motors Are Still a Big Deal in Raspberry Pi Projects

Micro servo motors occupy a sweet spot in electronics: they are small, cheap, widely available, and surprisingly capable. The classic SG90—probably the most recognizable micro servo on the planet—weighs around 9 grams, draws modest current, and can rotate its arm roughly 180 degrees. For hobbyists building robotic arms, pan-tilt camera mounts, automated blinds, animatronic eyes, or tiny walking robots, that combination is hard to beat.

The Raspberry Pi, meanwhile, has become the default brain for these projects. It runs a full Linux environment, supports Python, and exposes GPIO pins that can generate the pulse-width modulation (PWM) signals a servo needs. The catch is that servo control on the Pi is not as plug-and-play as blinking an LED. Jitter, power brownouts, and timing inconsistencies are common complaints. This article walks through the practical side of controlling micro servos with the Raspberry Pi and the RPi.GPIO library, including the pitfalls that trip up most beginners.

Understanding What a Micro Servo Actually Wants

The PWM Signal Behind the Motion

A hobby servo does not respond to voltage level the way a DC motor does. Instead, it expects a repeating pulse train, typically at 50 Hz—one pulse every 20 milliseconds. The width of that pulse encodes the target angle:

  • About 1.0 ms pulse → one extreme (often 0 degrees)
  • About 1.5 ms pulse → center (often 90 degrees)
  • About 2.0 ms pulse → the other extreme (often 180 degrees)

The servo’s internal control board compares the incoming pulse width to the signal from its potentiometer and drives the motor until the two match. That closed-loop behavior is why servos hold position under load, and why they twitch when the signal is unstable.

Why Micro Servos Are Different from Standard Servos

Micro servos like the SG90, MG90S, or TS90D share the same signal protocol as larger servos, but their physical characteristics matter:

  • Lower torque: Typically 1.5–2.5 kg·cm at 4.8V. They cannot push heavy loads.
  • Lower current draw: Usually 100–250 mA idle, with brief spikes up to 700 mA or more when stalled.
  • Plastic or metal gears: Metal-gear versions survive more abuse but cost more.
  • Limited rotation range: Most are 180 degrees; continuous-rotation variants exist but behave differently.

These traits shape your wiring and power decisions. A micro servo will not brown out a Raspberry Pi by itself, but three or four moving at once absolutely can.

Wiring a Micro Servo to the Raspberry Pi

The Three Wires You Cannot Ignore

Every hobby servo has three connections:

  1. Brown or black – Ground (GND)
  2. Red – Power (typically 4.8V to 6V)
  3. Orange, yellow, or white – Signal (PWM input)

The signal wire goes to a GPIO pin capable of PWM. On most Raspberry Pi models, GPIO 18 (physical pin 12) and GPIO 12/13 (pins 32/33) are the hardware PWM-capable pins, though RPi.GPIO can bit-bang software PWM on any GPIO.

Powering the Servo Without Killing Your Pi

This is the single most important lesson for beginners: do not power a servo from the Raspberry Pi’s 5V pin if you can avoid it. The Pi’s onboard regulator is not designed for the current spikes a servo produces. Symptoms of a shared supply include:

  • The Pi rebooting when the servo moves
  • Random Wi-Fi dropouts
  • Servo jitter that disappears when you unplug other peripherals

The clean solution is a separate 5V supply—a battery pack, a UBEC, or a dedicated bench supply—with the grounds tied together. The servo’s ground and the Pi’s ground must share a common reference, or the signal will float and the servo will behave erratically.

A simple wiring table looks like this:

| Servo Wire | Connects To | |---|---| | Brown/Black | Common GND (Pi GND + external supply GND) | | Red | External 5V supply | | Orange/Yellow | GPIO 18 (pin 12) |

Setting Up RPi.GPIO on Your Raspberry Pi

Installation and Permissions

RPi.GPIO ships with Raspberry Pi OS in most cases. If it is missing, install it with:

bash sudo apt update sudo apt install python3-rpi.gpio

You will need sudo to run scripts that touch GPIO, or you can add your user to the gpio group. For quick experiments, running with sudo python3 script.py is fine.

A Minimal First Script

Here is the smallest useful example: sweep a micro servo from 0 to 180 degrees and back.

python import RPi.GPIO as GPIO import time

SERVO_PIN = 18

GPIO.setmode(GPIO.BCM) GPIO.setup(SERVO_PIN, GPIO.OUT)

pwm = GPIO.PWM(SERVO_PIN, 50) # 50 Hz pwm.start(0)

def set_angle(angle): duty = 2.5 + (angle / 180.0) * 7.5 pwm.ChangeDutyCycle(duty) time.sleep(0.5)

try: while True: for a in range(0, 181, 10): setangle(a) for a in range(180, -1, -10): setangle(a) except KeyboardInterrupt: pwm.stop() GPIO.cleanup()

The duty cycle math deserves attention. At 50 Hz, one period is 20 ms. A 1 ms pulse is 5% duty cycle, 1.5 ms is 7.5%, and 2 ms is 10%. Many tutorials use 2.5% to 12.5% to stretch the range, but micro servos vary. The SG90 often responds well to 3%–11%, while some metal-gear models prefer 2.5%–12%. Always test gently and listen for buzzing—that means the servo is straining against its mechanical limit.

Taming Jitter and Improving Smoothness

The Problem with Software PWM

RPi.GPIO’s PWM is software-timed. On a busy Pi—especially a Pi Zero or a Pi running desktop software—the pulse timing can drift. The result is the infamous servo jitter: a faint buzzing and small random movements even when the angle should be constant.

Several fixes help:

  • Stop the PWM after reaching the target. Send the pulse for 300–500 ms, then call pwm.stop() or set duty cycle to 0. The servo holds its last position mechanically, and the signal noise disappears.
  • Use pigpio instead of RPi.GPIO for serious work. The pigpio library uses DMA-timed PWM and produces rock-solid pulses. RPi.GPIO is fine for learning and simple projects, but pigpio is the professional choice.
  • Reduce CPU load. Close unnecessary processes, avoid running the servo loop alongside heavy video processing.
  • Add a capacitor across the servo’s power terminals. A 470 µF to 1000 µF electrolytic capacitor near the servo smooths current spikes.

A Jitter-Reducing Helper Function

python def move_servo(pwm, angle, hold_time=0.4): duty = 2.5 + (angle / 180.0) * 7.5 pwm.ChangeDutyCycle(duty) time.sleep(hold_time) pwm.ChangeDutyCycle(0) # stop pulsing, servo holds position

This pattern eliminates most of the buzzing that beginners notice immediately.

Practical Project Ideas for Micro Servos and Raspberry Pi

Pan-Tilt Camera Mount

Two micro servos—one for horizontal pan, one for vertical tilt—plus a Pi Camera Module make a compact surveillance rig. Use GPIO 18 for pan and GPIO 19 for tilt, and drive them with separate PWM instances. Keep the camera’s ribbon cable slack so it does not fight the servo motion.

Automated Plant Watering Valve

A micro servo can press a small lever or pinch a tube. Combine it with a soil moisture sensor and a cron job, and you have a self-watering system that costs less than a commercial unit.

Animatronic Props for Halloween

Micro servos are perfect for moving eyeballs, flapping wings, or opening coffin lids. Because they are quiet and small, they hide easily inside foam props. Power them from a 4×AA battery pack to keep the Pi isolated from current spikes.

Robot Arm with Four Degrees of Freedom

A 4-DOF arm using MG90S servos can lift small objects. The challenge is coordinated motion: move one servo at a time, or interpolate angles in small steps so the arm does not jerk. A simple linear interpolation loop works well:

python def smooth_move(pwm, start, end, steps=20, delay=0.02): for i in range(steps + 1): angle = start + (end - start) * i / steps duty = 2.5 + (angle / 180.0) * 7.5 pwm.ChangeDutyCycle(duty) time.sleep(delay)

Troubleshooting Checklist

When a micro servo refuses to cooperate, work through these in order:

  1. Is the ground shared? The Pi and the servo power supply must have a common ground.
  2. Is the signal pin correct? Double-check BCM versus BOARD numbering. Mixing them is the most common beginner error.
  3. Is the power supply adequate? A weak 5V supply causes twitching and resets.
  4. Is the duty cycle in range? Values outside 2.5%–12.5% can drive the servo into its mechanical stops and burn it out.
  5. Is the servo damaged? Plastic gears strip easily. If the servo buzzes constantly and does not move, the gears may be gone.
  6. Is the Pi overloaded? Software PWM suffers when the CPU is busy. Try a lighter script or switch to pigpio.

Final Thoughts on Choosing the Right Approach

Micro servos and the Raspberry Pi are a natural pairing, but the relationship works best when you respect the servo’s power needs and the Pi’s timing limitations. For quick prototypes, RPi.GPIO is perfectly adequate—just remember to stop the PWM when the servo reaches its target. For projects that demand smooth motion, multiple servos, or long-term reliability, invest the time in pigpio or a dedicated servo controller board. Either way, the micro servo remains one of the most satisfying components to bring to life with a few lines of Python.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/micro-servo-motor-with-raspberry-pi/rpigpio-servo-control-raspberry-pi.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags