---
# System prepended metadata

title: How to connect servo to Raspberry Pi?
tags: [Raspberry Pi]

---

You can treat a hobby servo as “one signal wire + power” — the tricky part is powering it safely.

I’ll walk you through a simple, safe setup.

![Servo-Control](https://hackmd.io/_uploads/B1y3CuVeZl.jpg)

**1. What kind of servo are we talking about?**

Most small hobby servos (like SG90, MG90S, etc.) have 3 wires:

* Red – +5V (power)
* Brown/Black – GND
* Orange/Yellow/White – control signal (PWM)

That’s what I’ll assume below.

**2. The golden rule: separate power, shared ground**

Servos can pull a lot of current (300–800 mA peak each), which is too much for the Raspberry Pi’s 5 V pin if you have more than one or if the servo stalls.

**Recommended:**

* Use an external 5 V supply for the servo(s).
* Tie grounds together: servo GND, Pi GND, and power-supply GND must all be connected.

**Basic wiring**

**1. Servo power**

* Servo red → +5 V from external 5 V supply
* Servo brown/black → GND of external supply

**2. Common ground**

* External supply GND → Raspberry Pi GND (any GND pin)
* This makes sure the PWM signal has a common reference.

**3. Signal wire**

Servo signal (orange/yellow/white) → [Raspberry Pi](https://www.ampheo.com/c/raspberry-pi/raspberry-pi-boards) GPIO pin with PWM, e.g. GPIO18 (physical pin 12).

For one tiny servo (e.g. an SG90) you can sometimes power it from the Pi’s 5 V pin, but it’s safer to use an external 5 V brick if you notice brownouts or reboots.

**3. Which Raspberry Pi pin to use?**

Let’s use BCM GPIO numbers (what Python usually uses):

* GPIO18 – hardware PWM capable (good choice)
* Physical location: pin 12 on the 40-pin header

So for a single server test:

* Servo signal → GPIO18 (pin 12)
* Servo GND → Pi GND (pin 6 for example)
* Servo +5V → external 5 V (or Pi’s 5 V at your own risk)

**4. Control the servo in Python (simple example)**

Common approach: use pigpio or RPi.GPIO. pigpio gives very stable PWM, but RPi.GPIO is simpler to show.

**Example with RPi.GPIO**

Install (if needed):

```
sudo apt-get update
sudo apt-get install python3-rpi.gpio
```


Python code:

```
import RPi.GPIO as GPIO
import time

SERVO_PIN = 18  # BCM numbering

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

# 50 Hz servo signal (20 ms period)
pwm = GPIO.PWM(SERVO_PIN, 50)
pwm.start(0)

def set_angle(angle):
    # angle ~ 0..180
    # duty cycle ~ 2..12 (varies slightly by servo)
    duty = 2 + (angle / 18.0)
    GPIO.output(SERVO_PIN, True)
    pwm.ChangeDutyCycle(duty)
    time.sleep(0.3)      # allow servo to move
    GPIO.output(SERVO_PIN, False)
    pwm.ChangeDutyCycle(0)

try:
    while True:
        set_angle(0)
        time.sleep(1)
        set_angle(90)
        time.sleep(1)
        set_angle(180)
        time.sleep(1)

except KeyboardInterrupt:
    pass

pwm.stop()
GPIO.cleanup()
```


You might need to tweak the duty cycle range (that 2..12) depending on your servo; some like 0.5–2.5 ms pulses, some 1–2 ms.

**5. Driving multiple servos? Use a driver board**

If you want many servos, don’t drive them all directly from Pi PWM:

Use a [PCA9685](https://www.onzuu.com/search/PCA9685) servo driver board (I²C interface):

* It generates 16 channels of PWM in hardware.
* You still power servos from a separate 5 V supply.
* [Raspberry Pi](https://www.ampheoelec.de/c/raspberry-pi/raspberry-pi-boards) just sends position commands over I²C.

This dramatically simplifies timing and reduces CPU load.

**6. Quick checklist**

* Servo red → 5 V (prefer external supply)
* Servo black/brown → GND
* Servo signal → GPIO18 (pin 12) or other GPIO
* External GND tied to Pi GND
* Use 50 Hz PWM, ~1–2 ms pulse width
* Use separate 5 V supply if current draw is high