Home > Industry Insights >BLDC
TECHNICAL SUPPORT

Product Support

Controlling 20 Servos with a Raspberry Pi: A Complete Step-by-Step Guide

Published 2026-04-08

This guide provides a practical, field-tested solution for controlling up to 20servos simultaneously using a single-board computer like the Raspberry Pi. Directly connecting more than twoservos to the GPIO pins causes voltage drops, PWM timing conflicts, and board damage. The reliable method is using a dedicated PWM driver module (e.g., PCA9685). Below you will find the exact hardware setup, wiring diagrams, power calculations, Python code,and troubleshooting steps—all based on common real-world robotics and animatronics projects.

01Why You Cannot Connect 20servos Directly to GPIO

Current limit: Each GPIO pin supplies max 16mA, while a standard servo draws 150–500mA when moving. 20 servos would require over 10A—far beyond the board’s 3.3V/5V rail capacity.

PWM hardware limit: Raspberry Pi has only two hardware PWM channels (GPIO 18 & 19). Software PWM on other pins introduces jitter and CPU overload for 20 servos.

Voltage collapse: Operating 20 servos from the board’s 5V pin will cause immediate voltage drop, resetting the system.

02Core Solution Overview

Use anI²C‑based PWM driver module(16‑channel PCA9685 is the industry standard). Connect two such modules (each handles up to 16 servos) or one module plus a multiplexer. This guide uses two PCA9685 boards (total 32 channels, using only 20).

03Required Components (No Brand Names)

1 single‑board computer (Raspberry Pi 3B+ or newer)

2 PCA9685 16‑channel 12‑bit PWM driver boards

20 standard 5V servos (e.g., SG90, MG90S, or MG996R – choose based on torque needs)

1 external 5V DC power supply – calculate current: 20 servos × 0.5A = 10A minimum. Use a10A–15A regulated 5V supply.

Capacitors: 2x 1000µF electrolytic (≥10V) for power smoothing

Jumper wires (female‑to‑female for I²C, male‑to‑female for servo connections)

Breadboards or terminal blocks for power distribution

04Wiring Diagram (Step‑by‑Step)

Step 1 – I²C connection between Raspberry Pi and first PCA9685

Pi 3.3V → VCC of PCA9685 (Note: Some modules accept 5V logic; check your module. For 5V‑logic modules, use Pi’s 3.3V only if module is 3.3V tolerant. Safer: connect VCC to Pi’s 5V pin only if module datasheet confirms 5V tolerance. Most PCA9685 boards work with 3.3V I²C logic but need 5V for servo V+ – see next step.)

Correct standard wiring (for 99% of PCA9685 boards):

Pi 5V (pin 2 or 4) → PCA9685 VCC (power for the chip’s logic – yes, many run on 5V logic)

Pi GND → PCA9685 GND

Pi SDA (GPIO 2) → PCA9685 SDA

Pi SCL (GPIO 3) → PCA9685 SCL

Step 2 – External power for servos

External 5V supply (10A+) positive (+) → PCA9685 V+ terminal (often labelled “V+” or “servo power”)

External supply GND → PCA9685 GND (must share common ground with Pi – connect Pi GND to PCA9685 GND as already done above)

Place 1000µF capacitor across V+ and GND near the PCA9685 to absorb back‑EMF spikes.

Step 3 – Connecting servos

Each servo’s red (power) → PCA9685 V+ rail (use a breadboard power rail)

Servo brown/black (GND) → PCA9685 GND rail

Servo orange/yellow (signal) → PCA9685 PWM output pin (0 to 15 on first board, 0 to 3 on second board)

Step 4 – Second PCA9685

Set I²C address of second board: solder the address jumpers. Default address of PCA9685 is 0x40. For second board: solder A0 jumper → address becomes 0x41.

Connect second board’s SDA/SCL/GND/VCC in parallel with the first board (same I²C bus).

Connect its V+ to the same external 5V supply.

05Software Setup (Raspberry Pi OS, terminal commands)

Enable I²C on the Pi:

sudo raspi-config
# Interface Options → I2C → Yes
sudo reboot

Install required libraries:

sudo apt update
sudo apt install python3-pip python3-smbus i2c-tools
sudo pip3 install adafruit-circuitpython-pca9685

Verify I²C devices:

sudo i2cdetect -y 1

You should see 0x40 and 0x41 (if second board addressed correctly).

06Python Code to Control 20 Servos Independently

Create a fileservo_control.py:

import time
import board
import busio
from adafruit_pca9685 import PCA9685
# Initialize I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# First PCA9685 at address 0x40
pca1 = PCA9685(i2c, address=0x40)
pca1.frequency = 50  # 50Hz for standard servos
# Second PCA9685 at address 0x41
pca2 = PCA9685(i2c, address=0x41)
pca2.frequency = 50
# Servo pulse width range (typical 500 to 2500 microseconds)
# Convert to duty cycle: duty = pulse_us / 20000  65535
def angle_to_duty(angle, min_us=500, max_us=2500):
    pulse = min_us + (angle / 180.0)  (max_us - min_us)
    return int((pulse / 20000.0) * 65535)
# Control any servo by board and channel
def set_servo(board, channel, angle):
    if angle  180:
        angle = 180
    board.channels[channel].duty_cycle = angle_to_duty(angle)
# Example: Move all 20 servos to 90°
servos = [(pca1, i) for i in range(16)] + [(pca2, i) for i in range(4)]  # total 20
for board, ch in servos:
    set_servo(board, ch, 90)
    time.sleep(0.01)  # stagger to reduce current surge
# Sweep servo on board1 channel0 from 0 to 180
print("Sweeping servo on board1 ch0")
for angle in range(0, 181, 10):
    set_servo(pca1, 0, angle)
    time.sleep(0.05)
# Release all servos (stop sending PWM) - important to prevent jitter
pca1.deinit()
pca2.deinit()

07Power Management – Most Critical Part

A real case: A hobbyist once connected 10 servos directly to a 5V/3A power supply. During simultaneous movement, voltage dropped to 3.8V, servos stalled, and the Raspberry Pi rebooted. Solution: Use a 5V/15A regulated supply (e.g., an LED power supply or computer ATX power supply’s 5V rail). Add large capacitors (1000µF per 5-8 servos) near each driver board.

Actionable rule: Calculate your maximum simultaneous current. If all 20 servos move at once, assume 10A. For safety, add 30% margin → 13A minimum. Use 14AWG or thicker wires for power distribution.

08Calibrating Servo Pulse Widths

Not all servos have the same min/max pulse. Common values:

500 µs (0°) – 2500 µs (180°) for analog servos

600 µs (0°) – 2400 µs (180°) for many digital servos

To calibrate: Write a test script that sweeps from 300 to 2700 µs, find the mechanical limits, then update min_usandmax_us in the function above.

09Common Problems and Solutions

Problem Most Likely Cause Fix
Servos twitch or don't move Insufficient power supply current Upgrade to 15A supply, use shorter thick wires
Raspberry Pi reboots when servos start Voltage drop on 5V line Use external supply only for servos, keep Pi powered separately (via USB-C)
Only 16 servos work, not 20 Second board not detected Check I²C address (use i2cdetect -y 1), ensure SDA/SCL connections
Servos move but jitter Missing common ground Connect Pi GND to PCA9685 GND and external supply GND together
One servo not responding Bad signal wire or channel Swap with a known working servo on same channel to isolate

10Performance Optimization for 20 Servos

Stagger movement: Do not command all servos to change angle at exactly the same microsecond. Add time.sleep(0.005) between each set_servo call. This reduces peak current by spreading the load.

Update rate: 50Hz (20ms period) is standard. Do not increase frequency above 100Hz – most servos will overheat.

CPU usage: The PCA9685 hardware generates PWM independently. Your Python script only sends I²C commands every time you change angles. For static positions, the script can exit – servos hold last position as long as power is applied.

11Complete Action Plan to Implement This Today

1. Order components – Two PCA9685 boards, 20 servos, 5V/15A power supply, 1000µF capacitors.

2. Test with one servo – Wire a single servo to first PCA9685, run the sweep code.

3. Add power supply – Connect external 5V/15A to the V+ rail, verify servo moves strongly.

4. Scale to 5 servos – Add more servos gradually, monitoring temperature of the power supply wires.

5. Add second PCA9685 – Set address to 0x41, connect 4 servos, test.

6. Full 20‑servo test – Run the provided code moving all servos to 90°, then a slow sweep of all channels.

12Key Takeaway

You cannot control 20 servos directly from a Raspberry Pi’s GPIO – use PCA9685 PWM driver boards with a dedicated high‑current 5V power supply. This method is used in thousands of real animatronics, hexapod robots, and automation projects. It is reliable, scalable, and well‑documented. Start with a small test, verify power stability, then scale up. Your success depends on proper grounding and sufficient amperage – both are non‑negotiable.

Now take action: wire one PCA9685 with a single servo and external 5V supply. Once that works, add the rest. You will have a stable 20‑servo system within two hours of assembly.

Update Time:2026-04-08

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
+86 0769 8399 3238
 
kpowerMap