Servo Motor Control Using Raspberry Pi

Posted on

by

in

Servo Motor Control Using Raspberry Pi

Introduction

In this post we are gonna be exploring how to control your servo motor using your raspberry pi and using python code. We will explore some issue(s) that you might encounter while programming this component and how to solve it.

If your Internet of Things (IOT) project requires precision control like controlling the movement of a robot or a needle movement of a 3d printer or a simple movement mechanism like opening or closing a door lock then a servo motor is what you need together with your raspberry pi.

If you like video presentation then please see this project in action in my youtube channel.

Prerequisites

The following component/tools are needed in order to follow along with this post.

  • Raspberry Pi ( I used Raspberry Pi Zero W)
  • Servo Motor (either SG90 or MG90)
  • Breadboard
  • Connecting wires

My Raspberry Pi Zero is using the latest Raspberry Pi OS and if you need help in setting up new OS in your Pi then you can follow my earlier post. This is a headless setup so no external monitor or keyboard is needed.

What is Servo motor?

Servo motors are excellent choice if you want precision control of movement for your projects mechanical parts. The range of motion for these type of special motor is limited to 180 degrees.

A servo motor or (servomotor) is a type of motor that combines a rotary encoder and and a potentiometer to create what we call as servomechanism. If your Internet of Things (IOT) project requires precise motion control then a servo motor can help you in this.

If you take a look inside the construction of a servo motor then you would see that it has a dc or ac motor, some gear assembly connected to an output shaft and a controlling feedback circuit. This is more of a closed-loop system

For a much detailed explanation on how servo motor works then please see this wikipedia link.

Servo Pinout

The servo usually comes in 3 pins with the Vcc, Gnd and Data or PWM signal pin. The Data or PWM signal pins comes in orange sometimes and it connected to the GPIO of your microcontroller. In the case of Raspberry Pi, we can either connect it with any GPIO pins or much better to pins that has hardware PWM setup by default.

Raspberry Pi Servo Motor - Pinout

How to control Servo?

To control the position of our Servo Motor then we need to send a series of pulses to its data or signal pin. In the case of the raspberry pi, we will send a PWM signal with different duty cycle.

Looking at the datasheet of an SG90 servo motor from here, the pulse cycle is 20ms (equivalent to 50Hz) and to control the position then we need to set the following duty cycle. Take note of the following diagram below as to how we can control its direction.

We need to take note of the following mathematical items before we proceed.

  • Period = 1/Frequency = 1/50Hz = 20ms
  • To set the servo motor shaft to the full left position then we need to send a pulse width of 1ms.
Duty Cycle = Pulse Width (in secs) /Period (in secs) = .001(secs)/.02(secs) = 0.05 or 5%
  • To set the servo motor shaft to the middle position then we need to send a pulse width of 1.5ms.
Duty Cycle = Pulse Width (in secs) /Period (in secs) = .0015(secs)/.02(secs) = 0.075 or 7.5%
  • To set the servo motor shaft to the full right position then we need to send a pulse width of 2ms.
Duty Cycle = Pulse Width (in secs) /Period (in secs) = .002(secs)/.02(secs) = 0.1 or 10%

To summarize, we need to set our PWM signal duty cycle for our Raspberry Pi GPIO pins between 5% to 10% to make it move fully from left to right. You will see this later when we are coding and why knowledge of duty cycle is needed.

NOTE: This is the ideal case for servo motor however you would find out that sometimes it does not move the full 180 degree by using this setup. Please see the issue section below.

Wiring/Schematic

Raspberry Pi Servo Motor - Wiring
ServoRaspberry Pi
GNDGND
VinVin
Data or PWM SignalGPIO12

Note: It is much better to supply a different power source with constant current to the servo motor as it might draw more current. Just remember to connect the ground of the raspberry pi and the power source.

Code

Using the information about the duty cycle above then we can create the following code to drive our servo motor using our raspberry pi. This is available also in my github account so either you download or clone my repository from there.

We will be coding in two raspberry pi package using the RPi.GPIO and the gpiozero. Both of these packages are now installed by default in the latest raspberry pi os.

RPi.GPIO

# Title:  SG90 Servo Driver Code - RPi.GPIO
# Author: donskytech

import RPi.GPIO as GPIO
import time

# setup RPi
GPIO.setwarnings(False)
servo_pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin,GPIO.OUT)

# 50 Hz or 20 ms PWM period
pwm = GPIO.PWM(servo_pin,50) 

print("Starting at zero...")
pwm.start(5) 

try:
    while True:
        print("Setting to zero...")
        pwm.ChangeDutyCycle(5) 
        time.sleep(1)

        print("Setting to 180...")
        pwm.ChangeDutyCycle(10) 
        time.sleep(1)

        print("Setting to 90...")
        pwm.ChangeDutyCycle(7.5) 
        time.sleep(1)
except KeyboardInterrupt:
    pwm.stop() 
    GPIO.cleanup()
    print("Program stopped")


  • Line 4-5 : we import the necessary packages
  • Line 8-11: configure the Rpi.GPIO setup including the servo pins
  • Line 14-17: start our PWM pin passing a 50Hz and the servo pin
  • Line 20: we have an infinite loop that will cycle thru our servo movement
  • Line 21-31: we change the duty cycle from 5% to 10% to move the servo horn from 0(zero) to 180 degree
  • Line 32-35: we cleanup in case we terminate our python script.

gpiozero

Another popular library is the gpiozero which provides classes that abstract much of the GPIO complexity. They have a servo class that represents this component and we can just call methods from that class. Please see sample code below.

# Title:  SG90 Servo Driver Code  - GPIOZero
# Author: donskytech

from gpiozero import  Servo
from time import sleep

servo = Servo(12)

try:
    while True:
        print("Setting to min...")
        servo.min()
        sleep(1)

        print("Setting to mid...")
        servo.mid()
        sleep(1)

        print("Setting to max...")
        servo.max()
        sleep(1)
except KeyboardInterrupt:
    print("Exiting program")
  • Line 4-5: import the gpiozero packages
  • Line 7: initialize a servo class
  • Line 10: we create an infinite loop to run our sequence
  • Line 11-2: we move the servo from zero to 90 to 180 degree by calling the min(), mid() and max() method of the servo class.

Issues:

Why is my servo motor not moving full 180o (degree)?

The image above shows that the movement of your servo is not the full 180 degree even though we follow the recommended duty cycle. Different servo have different peculiarities so it is much better to test it while you are coding.

To fix the issue, let us adjust the duty cycle in our python code. In my SG90 Servo, the following code works. As you can see, the major difference is the duty cycle setup.

RotationRecommendedModified
0 degree53
180 degree1013
90 degree7.58
# Title:  SG90 Servo Driver Code - RPi.GPIO
# Author: donskytech

import RPi.GPIO as GPIO
import time


# setup RPi
GPIO.setwarnings(False)
servo_pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin,GPIO.OUT)

# 50 Hz or 20 ms PWM period
pwm = GPIO.PWM(servo_pin,50) 

print("Starting at zero...")
pwm.start(3) 

try:
    while True:
        print("Setting to zero...")
        pwm.ChangeDutyCycle(3) #  Duty Cycle Adjusted
        time.sleep(1)

        print("Setting to 180...")
        pwm.ChangeDutyCycle(13) #  Duty Cycle Adjusted
        time.sleep(1)

        print("Setting to 90...")
        pwm.ChangeDutyCycle(8) #  Duty Cycle Adjusted
        time.sleep(1)
except KeyboardInterrupt:
    pwm.stop() 
    GPIO.cleanup()
    print("Program stopped")


If you are using gpiozero in driving your servo motor then take a look at the following modified code. Can you guess how did I arrive with the new setup for min_pulse_width and max_pulse_width?

# Title:  SG90 Servo Driver Code - GPIOZero
# Author: donskytech

from gpiozero import Servo
from time import sleep

servo = Servo(12, min_pulse_width=0.0006, max_pulse_width=0.0026)

try:
    while True:
        print("Setting to min...")
        servo.min()
        sleep(1)

        print("Setting to mid...")
        servo.mid()
        sleep(1)

        print("Setting to max...")
        servo.max()
        sleep(1)
except KeyboardInterrupt:
	print("Exiting program")

From the documentation of the gpiozero for servo, we can deduced that the default min and max pulse width is between 1ms to 2ms which is equivalent to a 5% and 10% duty cycle. However from our testing and the table above, it looks like the best way to control it is from 3% to 13%.

So using this new values then we can substitute it now to the default value of the servo class of the gpiozero when we initialize servo = Servo(12, min_pulse_width=0.0006, max_pulse_width=0.0026)

#### For a 3% min duty cycle
Duty Cycle = Pulse Width/Period 
.03 = Pulse Width/0.02
Pulse Width = 0.03 * 0.02
Pulse Width = 0.0006

#### For a 13% min duty cycle
Duty Cycle = Pulse Width/Period 
.13 = Pulse Width/0.02
Pulse Width = 0.13 * 0.02
Pulse Width = 0.0026

How about other servo like MG90 not in full 180 degree movement?

Then you need to figure it out yourselves also. Mine was like this code.

# Title:  SG90 Servo Driver Code - RPi.GPIO
# Author: donskytech

import RPi.GPIO as GPIO
import time


# setup RPi
GPIO.setwarnings(False)
servo_pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin,GPIO.OUT)

# 50 Hz or 20 ms PWM period
pwm = GPIO.PWM(servo_pin,50) 

print("Starting at zero...")
pwm.start(2) 

try:
    while True:
        print("Setting to zero...")
        pwm.ChangeDutyCycle(2) 
        time.sleep(1)

        print("Setting to 180...")
        pwm.ChangeDutyCycle(11) 
        time.sleep(1)

        print("Setting to 90...")
        pwm.ChangeDutyCycle(6.5) 
        time.sleep(1)
except KeyboardInterrupt:
    pwm.stop() 
    GPIO.cleanup()
    print("Program stopped")


Servo motor is jittering

Usually this might be cause by not enough current flowing to the servo motor. Power up the servo with an external supply and connect the common ground with your raspberry pi.

Wrap Up

We have explored how to program and connect your servo motor with your raspberry pi in this post. I have explained the logic behind the programming and stated several issues that you might encounter.

That’s it! Happy Exploring!

If you like my post then please consider sharing this. Thanks!

2 responses to “Servo Motor Control Using Raspberry Pi”

  1. Common Raspberry Pi Errors – donskytech.com

    […] Please see the following link. […]

  2. Keypad with a Database Security System using Raspberry Pi

    […] The following table is the wiring diagram between the raspberry pi and our servo motor. If you are not familiar with how to interface your servo motor with your raspberry pi then please see this Servo Motor Control Using Raspberry Pi. […]

Leave a Reply

Your email address will not be published.