raspberry pi gpio programming ,raspberry pi gpio servo motor Using Python,raspberry pi gpio programming Using Python

raspberry pi gpio programming example for servo motor Using Python 2

[otw_is sidebar=otw-sidebar-1]raspberry pi gpio programming ,raspberry pi gpio servo motor Using Python,raspberry pi gpio programming Using PythonIn this post I will discuss about how to interface and control the Servo Motor with Raspberry Pi using the Python IDLE. In the previous post I have discussed the Raspberry Pi interface with the Bipolar Stepper Motor I have used the L298 motor controller in the previous post. For the sake of this post only Raspberry PI and the Servo motor are used no other motor controller used the reason for will be clear in the sections to follow.

[otw_is sidebar=otw-sidebar-2]

 

raspberry pi gpio programming example for servo motor Using Python

 

After reading this post you will learn about the Servo Motor, the working of the Servo Motor and the interfacing of the Raspberry Pi and the Servo motor so sit back keep reading and enjoy learning.

Equipment Required:

For this post you will need the following equipment:

  • Raspberry PI
  • Servo motor.
  • Breadboard.
  • Jumper Wires.

What is a Servo Motor:

 

A servo motor is a type of motor which has shaft, the rotation or the angular position of which is controlled by the signal that appears on the signal line of the servo motor. The working of the servo motor is very different from that of the normal DC permanent motor in a way that the servo motor has three wires connected to it. Two of three wires of the servo motor are the power pins and the third pin is the signal pin to which the signal is connected in such a way that the pattern of the signal controls the angular position of the shaft of the Servo Motor. Due to this peculiar working the Servo motor finds unique applications in which the angular position is to be controlled.

[otw_is sidebar=otw-sidebar-3]

Working of the Servo Motor,Servo Motor basics, types of servo motor,What is a Servo Motor

 

Working of the Servo Motor:

The working of the Servo motor is quite simple and here I will only discuss the surface knowledge about the working of the servo motor that is relevant to the task in hand. In order to understand the working of the Servo motor first consider the pinout of the Servo Motor, as described in the previous section the Servo Motor has two power pins one for 5 Volt and one for the GND, and the third signal line controls the angular position of the Servo Motor shaft this can be seen in the following image.

Working of the Servo Motor,What is Servo Motor

 

 

Let us try to understand how the pattern of the signal on the signal line controls the angular position of the Servo motor. The pulse width modulation (PWM) signal is applied to the signal line of the Servo Motor, the duty cycle of this PWM signal determines the angular position of the Servo motor. The duty cycle of the PWM signal can vary from 0 % to the 100 % , 0% duty cycle means that PWM signal is completely off and the 100% duty cycle means that the PWM signal is on all the time and all the duty cycle within these two limits represents the corresponding on time of the PWM that is 25% duty cycle means that the PWM signal is on for 25% of its time period. In short the duty cycle of the PWM signal controls the angular position of the Servo motor and the duty cycle of the PWM signal can easily be controlled by the microcontroller, the controller we are using for this is Raspberry Pi. The whole can be seen in the following image.

[otw_is sidebar=otw-sidebar-2]

Applications of the Servo Motor:

 

Servo Motor holds a very specific role among the actuators. Some of its important applications are following:
Control the movements in the robotics.

  • Radar.
  • Conveyor belts.
  • Doors.
  • Solar tracking and many more.

 

Raspberry Pi Servo Motor Circuit Overview:

The connection between the Raspberry Pi and Servo motor is quite simple and the control strategy is also simple. The Raspberry Pi is connected to the power supply via the 40 pin header on the Raspberry Pi, the servo motor which also operates at 5 volts is connected to the same power supply that is why the we are not using the motor controller for the Servo motor as the current for driving the Servo motor is provided by the external power supply and not by the Raspberry Pi the raspberry Pi only provides the PWM signal which determines the angular position of the shaft of the servo motor. The General Purpose Input / Output pin of the Raspberry Pi is connected to the signal wire of the Raspberry PI that is all for the circuit. Yes it is this simple. The block diagram of the Raspberry Pi and the servo motor is given below to give the notion of the working.

Raspberry Pi Servo Motor Circuit ,Raspberry Pi programming,Raspberry Pi gpio

[otw_is sidebar=otw-sidebar-2]

 

The circuit diagram of the Raspberry Pi and the Servo motor is shown below.

Raspberry Pi Servo Motor Circuit ,Raspberry Pi Gpio Circuit ,Raspberry Pi Circuit

Raspberry Pi Python Programming code for the Servo motor:

[otw_is sidebar=otw-sidebar-3]

The Raspberry Pi can be programmed by using various programming platforms. One of the most popular programming environments for the Raspberry Pi is the Python IDLE. As the Python language is very common in the Raspberry Pi based projects so I am using the Python to write the code for controlling the angular position of the shaft of the Servo motor. Follow the following simple steps to be able the write the code using Python IDLE. Here I am assuming that you have already installed the Operating System in the Raspberry Pi and the GPIO library for the Python.

Step 1:

First of all open the Python IDLE as shown in the following image:

Step2:

Now you can copy paste the following code in your Python IDLE.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12, GPIO.OUT)

p = GPIO.PWM(12, 50)

p.start(7.5)

try:
while True:
p.ChangeDutyCycle(7.5) # turn towards 90 degree
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(2.5) # turn towards 0 degree
time.sleep(1) # sleep 1 second
p.ChangeDutyCycle(12.5) # turn towards 180 degree
time.sleep(1) # sleep 1 second
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()



 

Notice the library I have imported in the above code. This library comes by default in the newer version of the Raspbian which is the recommended Operating system of the Raspberry Pi id you do not have this library read my post on how to download the Python library for GPIO programming. GPIO. PWM is the module in the library that controls the PWm signaling on the Raspberry Pi.

That is all for now I hope this post would be helpful for you in the next article I will come up with more interesting applications of the Raspberry Pi till then stay connected, keep reading and enjoy learning.

2 Comments

  1. Overall Nice document.
    I have doubts. p = GPIO.PWM(12, 50) . Here, 12 is pin number it seems, and what 50 denotes? Could you elaborate on this line?

  2. Hi, It has come to our attention that you are using our client’s photographs on your site without a valid licence. We have already posted out all supporting documents to the address of your office. Please confirm once you have received them. In the meantime, we would like to invite you to settle this dispute by making the below payment of £500. Visual Rights Group Ltd, KBC Bank London, IBAN: GB39 KRED 1654 8703, 1135 11, Account Number: 03113511, Sort Code: 16-54-87 Once you have made the payment, please email us with your payment reference number. Please note that a failure to settle at this stage will only accrue greater costs once the matter is referred to court. I thank you for your cooperation and look forward to your reply. Yours sincerely, Visual Rights Group Ltd, Company No. 11747843, Polhill Business Centre, London Road, Polhill, TN14 7AA, Registered Address: 42-44 Clarendon Road, Watford WD17 1JJ

Leave a Reply

Your email address will not be published. Required fields are marked *