Using the SG90 Servo Motor with the Raspberry Pi

Using the SG90 Servo Motor with the Raspberry Pi

The SG90 servo motor is a small, lightweight servo commonly used in robotics, automation, and DIY projects. It allows precise control of angular position using Pulse Width Modulation (PWM). This guide will show you how to control an SG90 servo motor with a Raspberry Pi using Python.


What You Will Need

  1. Raspberry Pi (any model with GPIO support, e.g., Pi 3, Pi 4)
  2. SG90 Servo Motor
  3. External Power Source (5V, optional for multiple servos)
  4. Breadboard and Jumper Wires
  5. Python installed on the Raspberry Pi

Step 1: Wiring the SG90 Servo to the Raspberry Pi

The SG90 servo motor has three pins:

SG90 Pin Raspberry Pi Pin Function
VCC (Red) 5V (Pin 2) Power Supply
GND (Brown) GND (Pin 6) Ground
Signal (Orange) GPIO18 (Pin 12) PWM Signal Control

Note: If using multiple servos, use an external 5V power supply to avoid overloading the Raspberry Pi’s 5V pin.


Step 2: Enable PWM on the Raspberry Pi

The Raspberry Pi generates PWM signals to control the servo position.

  1. Install the Raspberry Pi GPIO library (if not already installed):
    sudo apt update && sudo apt install python3-rpi.gpio
    
  2. Open a Python script editor:
    nano servo_control.py
    
  3. Copy the following Python script to control the SG90 servo motor:

Step 3: Python Code to Control the Servo

import RPi.GPIO as GPIO
import time

# Set up the GPIO pin for PWM
SERVO_PIN = 18  # Use GPIO18 (Pin 12)
GPIO.setmode(GPIO.BCM)
GPIO.setup(SERVO_PIN, GPIO.OUT)

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

def set_angle(angle):
    duty_cycle = (angle / 18) + 2.5  # Convert angle to duty cycle
    GPIO.output(SERVO_PIN, True)
    pwm.ChangeDutyCycle(duty_cycle)
    time.sleep(0.5)  # Wait for servo to move
    GPIO.output(SERVO_PIN, False)
    pwm.ChangeDutyCycle(0)

try:
    while True:
        angle = int(input("Enter angle (0-180): "))
        if 0 <= angle <= 180:
            set_angle(angle)
        else:
            print("Invalid angle! Enter a value between 0 and 180.")

except KeyboardInterrupt:
    print("Exiting...")
    pwm.stop()
    GPIO.cleanup()

Step 4: Running the Servo Control Script

  1. Save the script and exit:

    • Press CTRL+X, then Y, then ENTER.
  2. Run the script:

    python3 servo_control.py
    
  3. Enter angles between 0 and 180 to move the servo.


Step 5: Understanding PWM for Servo Control

  • PWM Frequency: The SG90 operates at 50Hz.
  • Duty Cycle Calculation:
    • 0° → 2.5% Duty Cycle
    • 90° → 7.5% Duty Cycle
    • 180° → 12.5% Duty Cycle
  • Formula: duty_cycle = (angle / 18) + 2.5

Step 6: Applications of SG90 Servo with Raspberry Pi

  1. Robotic Arms – Control joints for movement.
  2. Automated Doors & Locks – Open/close mechanisms using a servo.
  3. Pan-Tilt Camera Systems – Move cameras for surveillance or photography.
  4. Smart Home Automation – Control levers, locks, and switches remotely.

Troubleshooting

  1. Servo Not Moving?

    • Ensure correct wiring (VCC, GND, and GPIO18 for signal).
    • If using multiple servos, use an external 5V power source.
  2. Erratic Movements?

    • Ensure stable power; use capacitors (100µF) if needed.
    • Use higher precision PWM control, such as a PCA9685 PWM module for multiple servos.
  3. Permission Denied Error?

    • Run the script with sudo:
      sudo python3 servo_control.py
      

Conclusion

The SG90 servo motor is a great way to add motion control to Raspberry Pi projects. By using PWM signals, you can precisely control the servo’s position for robotics, automation, and IoT applications. Experiment with different angles and applications to unlock its full potential! 🚀

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.