Controlling Servo Motor with Raspberry pi

INTRODUCTION

Servo Motor is a combination of DC motor, position control system and gears. Servos have many applications in the modern world and with that, they are available in different shapes and sizes. We will be using SG90 Servo Motor in this tutorial, it is one of the popular and cheapest one. SG90 is a 180 degree servo. So with this servo we can position the axis from 0-180 degrees.

A Servo Motor mainly has three wires, one is for positive voltage, another is for ground and last one is for position setting. The Red wire is connected to power, Brown wire is connected to ground and Yellow wire (or WHITE) is connected to signal.

The table below shows the Servo Position for that particular Duty Ratio. You can get any angle in between by choosing the value accordingly. So for 45º of servo the Duty Ratio should be ‘5’ or 5%.

POSITION

DUTY RATIO

2.5

90º

7.5

180º

12.5

Circuit Diagram:servo-control-using-raspberry-pi-circuit-diagram.png

CODE:

import RPi.GPIO as IO        # calling for header file for GPIO’s of PI
import time                           # calling for time to provide delays in program
IO.setwarnings(False)          # do not show any warnings
IO.setmode (IO.BCM)            # programming the GPIO by BCM pin numbers. (like PIN29 as‘GPIO5’)
IO.setup(19,IO.OUT)             # initialize GPIO19 as an output
p = IO.PWM(19,50)              # GPIO19 as PWM output, with 50Hz frequency
p.start(7.5)                             # generate PWM signal with 7.5% duty cycle
while 1:                                                       # execute loop forever                                    
        p.ChangeDutyCycle(7.5)                   # change duty cycle for getting the servo position to 90º
        time.sleep(1)                                      # sleep for 1 second
        p.ChangeDutyCycle(12.5)                  # change duty cycle for getting the servo position to 180º
        time.sleep(1)                                     # sleep for 1 second
        p.ChangeDutyCycle(2.5)                  # change duty cycle for getting the servo position to 0º
        time.sleep(1)                                     # sleep for 1 second

Video of the servo motor in action click here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s