How to Connect a Servo Motor to Raspberry Pi Using a Servo Motor Driver Module

Micro Servo Motor with Raspberry Pi / Visits:161

For hobbyists, educators, and robotics enthusiasts, the union of a Raspberry Pi and a micro servo motor unlocks a universe of motion. From animating model robots and building automated camera sliders to creating smart pet feeders and interactive art installations, these tiny, precise motors are the muscles of the maker world. However, any seasoned tinkerer knows the cardinal rule: you should never connect a servo motor directly to your Raspberry Pi's GPIO pins.

Why? The Raspberry Pi is a brilliant brain, but a frail body when it comes to power delivery. Its GPIO pins operate at 3.3V logic and can only safely supply tiny amounts of current (typically around 16mA per pin). A micro servo, especially under load, can draw hundreds of milliamps. Attempting to power it directly from the Pi risks damaging the GPIO circuitry, causing system instability, or triggering a reboot. The solution is elegantly simple and essential: a Servo Motor Driver Module.

This guide will walk you through not just the "how," but the "why," ensuring your projects move smoothly without a puff of magic smoke.


Why the Humble Micro Servo Deserves Your Respect

Before we connect a single wire, it's crucial to understand the component at the heart of our project.

What is a Micro Servo Motor?

Unlike standard DC motors that spin continuously, a servo motor is designed for precise angular positioning. You send it a signal telling it what angle to move to, and its internal circuitry (a control board, motor, and potentiometer) works to hold that position against resistance. Micro servos, like the ubiquitous SG90 or MG90S, are defined by their compact size (often weighing ~9g) and their operating range, typically 0 to 180 degrees.

The Three Wires: Decoding the Harness

Every hobby servo has a three-wire connector: * Brown or Black (Ground): The common return path for current. This must be connected to the Pi's ground. * Red (Power/VCC): This supplies the main operating power for the motor. This is where the driver module becomes critical. * Orange or Yellow (Signal/PWM): This is the "command" wire. It carries a Pulse Width Modulation (PWM) signal from the Raspberry Pi, which dictates the target angle.

The Power Hungry Nature of Motion

This is the core of the problem. A micro servo at rest might draw 5-10mA. When moving unloaded, it can draw 100-200mA. When under load—like lifting an arm or pushing a lever—that demand can instantly spike to 500-700mA or more. The Raspberry Pi's 5V pin is typically fused at around 1.1A for the entire board. A single stalling servo could starve your Pi of power, let alone the risk posed to the delicate GPIO pin tasked with sending the signal.

Enter the Hero: The Servo Driver Module

A servo driver module acts as a dedicated, intelligent power supply and signal manager for your servos.

Common Types of Driver Modules

  1. Basic PCA9685 Module: This is the gold standard for Raspberry Pi servo projects. The PCA9685 is a 16-channel, 12-bit PWM driver IC. It communicates with the Pi via the I2C protocol (using just two GPIO pins), can control up to 16 servos independently, and is powered by a separate supply. It handles all the complex PWM signal generation, offloading that task from the Pi's CPU.
  2. Dedicated HATs/Boards: These are Raspberry Pi-specific boards that stack on top of the GPIO header, offering neat, integrated solutions with multiple channels and sometimes additional features like motor drivers.
  3. Simple Power & Signal Isolator Boards: Less common, these boards provide a separate power input for servos and pass the PWM signal through from the Pi while keeping the power grounds connected. They offer protection but don't provide the multi-channel scalability of the PCA9685.

For this guide, we will focus on the PCA9685 module, as it is the most versatile, professional, and widely used solution.


Step-by-Step: Connecting the PCA9685 to Your Raspberry Pi

Hardware & Prerequisites

  • Raspberry Pi (any model with a 40-pin GPIO header, like a 3B+, 4, or Zero 2 W)
  • Micro Servo Motor (e.g., SG90)
  • PCA9685 Servo Driver Module
  • External 5V-6V Power Supply (e.g., a 5V 2A DC wall adapter with a barrel jack or screw terminals). Important: For most micro servos, do not exceed 6V.
  • Jumper Wires (Female-to-Female, and possibly Male-to-Female)
  • Breadboard (optional, but helpful for organization)

Physical Connection Diagram

Let's build the circuit system by system.

1. Powering the PCA9685 Module

This is the most critical step. The driver module has two sets of power terminals: * VCC & GND (for logic): Connect these to the Raspberry Pi's 3.3V (Pin 1) and Ground (Pin 6). This powers the chip's brain at the Pi's safe logic level. * V+ & GND (for servos): Connect these to your external 5V power supply. This is the muscle power for your servos. * ⚠️ Crucial: You must connect a jumper wire between the Ground of the Pi and the Ground of the external power supply. This creates a common reference (a "common ground") so the PWM signals are understood correctly. The PCA9685 board usually has a solder jumper to connect the two grounds; if not, use a wire.

2. Linking the Pi to the Driver (I2C)

The PCA9685 uses the I2C bus, which requires only four connections: * Pi 3.3V -> Module VCC (already done above) * Pi Ground -> Module GND (already done above) * Pi GPIO 2 (SDA) -> Module SDA * Pi GPIO 3 (SCL) -> Module SCL

3. Connecting the Servo

This is the easy part. Plug your micro servo's 3-pin connector into one of the 16 channels on the PCA9685 board. Ensure the orientation is correct: Brown/Black (Ground) is on the side labeled "GND" or "–", Red (V+) is in the middle, and Yellow/Orange (Signal) is on the other side.

Software Configuration: Enabling the Magic

With hardware assembled, we need to prepare the Pi's software.

1. Enable I2C Interface

Open a terminal on your Raspberry Pi (either directly or via SSH). bash sudo raspi-config Navigate to Interface Options -> I2C -> select Yes to enable. Reboot if prompted.

2. Install Necessary Packages

bash sudo apt update sudo apt install python3-pip python3-smbus i2c-tools -y

3. Detect the Module

Run the I2C detection tool. You should see the PCA9685 appear at address 0x40 (the default). bash sudo i2cdetect -y 1 (Use -y 0 on very early Raspberry Pi Model B boards).

4. Install the Adafruit PCA9685 Library

This library simplifies control immensely. bash pip3 install adafruit-circuitpython-pca9685 pip3 install adafruit-circuitpython-servokit

The Moment of Truth: Python Control Script

Create a new Python file, servo_test.py.

python

!/usr/bin/env python3

import time from adafruit_servokit import ServoKit

Initialize the ServoKit for 16 channels, using the default address (0x40)

kit = ServoKit(channels=16)

Define which channel your servo is on (e.g., channel 0)

servo_channel = 0

Configure the pulse width range for your specific servo.

These are typical values for a 180-degree micro servo.

If movement is less than 180 degrees, adjust minpulse and maxpulse.

kit.servo[servochannel].setpulsewidthrange(500, 2500)

print("Servo test starting. Press Ctrl+C to stop.")

try: while True: print("Moving to 0 degrees") kit.servo[servo_channel].angle = 0 time.sleep(1)

    print("Moving to 90 degrees")     kit.servo[servo_channel].angle = 90     time.sleep(1)      print("Moving to 180 degrees")     kit.servo[servo_channel].angle = 180     time.sleep(1)      print("Moving to 90 degrees")     kit.servo[servo_channel].angle = 90     time.sleep(1) 

except KeyboardInterrupt: print("\nProgram stopped by user.") # Optionally, move servo to a neutral position kit.servo[servo_channel].angle = 90

Run the script: bash python3 servo_test.py

Your servo should now sweep smoothly between its positions! The Raspberry Pi's CPU is free, the GPIO pins are safe, and the servo has all the clean power it needs from its dedicated supply.


Advanced Tips & Troubleshooting

Calibrating for Your Specific Servo

Not all servos are created equal. If your servo doesn't quite hit 0 or 180 degrees, you need to calibrate the pulse widths. The set_pulse_width_range(min_pulse, max_pulse) function is your friend. Start with (500, 2500) and adjust in small increments. Use a script to find the exact values where your servo starts to stall at each extreme.

Power Supply Considerations

  • Voltage: 5V is standard and safe. 6V can provide more torque but may shorten the lifespan of some budget servos. Never use a voltage higher than your servo's rated maximum.
  • Current: A 2A (2000mA) supply is ample for several micro servos. Always ensure your supply can deliver more current than the combined stall current of all moving servos.

Controlling Multiple Servos

This is where the PCA9685 shines. Simply plug servos into other channels (0-15) and control them in your script: python kit.servo[0].angle = 45 kit.servo[1].angle = 135 kit.servo[15].angle = 90 All 16 servos can be controlled simultaneously via the single I2C connection.

Common Issues & Fixes

  • Servo Jitters or Doesn't Move: 99% of the time, this is a power issue. Check all power connections. Ensure your external supply is on and capable of delivering sufficient current. Verify common ground.
  • I2C Device Not Found: Run sudo i2cdetect -y 1 again. If nothing appears, check your SDA/SCL wiring. Ensure I2C is enabled in raspi-config.
  • Servo Moves Erratically: Electrical noise from the motor can feed back into the control circuit. Place a large capacitor (e.g., 100µF electrolytic) across the V+ and GND terminals on the PCA9685 board, close to the servo connections. This acts as a tiny local battery to smooth out sudden current demands.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/micro-servo-motor-with-raspberry-pi/connect-servo-driver-module-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