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.
For this post you will need the following equipment:
- Raspberry PI
- Servo motor.
- 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.
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.
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.
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.
- Conveyor belts.
- 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.
The circuit diagram of the Raspberry Pi and the Servo motor is shown below.
Raspberry Pi Python Programming code for the Servo motor:
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.
First of all open the Python IDLE as shown in the following image:
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.