In this wiki section you will find the sample code and the hardware interfacing for controlling a servo motor using Raspberry Pi 2. Raspberry Pi is a hot topic and also you might have seen many wiki posts on Raspberry Pi 2!!..But whats so much fascinating about controlling a servo??. It is fascinating for many reasons, but it will be more than that for you, if you are entranced in robotics. Robotics is a very popular field of research right now with many ideas on what will be the next big advancement. Over the years robotics have branched off into many different specialisation streams; Humanoid robotics, Biomimetics, Telerobotics, Adaptive robotics are the most popular of them. Advances in robotics are happening at a truly amazing rate. The capabilities that we have now compared to 10 years ago are staggering and the trend of increasing capabilities will extend into the future.
As quoted by someone in the past, “Anyone who is interested in robotics has to learn from the basics of servo motors”. Time has gone far by, but the quote stands true for all. Servo Motor is nothing but a simple device which uses error-sensing feedback signals to determine and control the position of a motor shaft. In section below you will find the hardware interfacing for controlling a servo using the Raspberry Pi 2, a sample sode is also given below which you can modify and suit yourself!
The hardware modules used in this tutorial are mentioned below:
Servo motor has got three connection pins, Vcc, ground and signal. It requires a 5V 1A power, which is provided from a suitable supply source. On the Raspberry Pi board, GPIO pin number 22 is selected as the signal pin. The GND pin of the power supply is connected to the ground pin of the servo and also to the 6th pin on the Raspberry Pi. After providing suitable connections open the python editor and program the Raspberry Pi. A sample code is given below, which you can use as it is or you can modify it to suit yourself.
import RPi ## Import GPIO Library.
import time ## Import ‘time’ library for a delay.
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering.
GPIO.setup(22, GPIO.OUT) ## set output.
pwm=GPIO.PWM(22,100) ## PWM Frequency
duty1= float(angle1)/10 + 2.5 ## Angle To Duty cycle Conversion
duty2= float(angle2)/10 + 2.5
As per the above program the servo rotates 180 degrees. The length of the pulse will determine the position of the servo. Equivalent duty cycle is calculated for an angle and the RPi will generate a pulse for the calculated duty cylce, which will be send to the servos signal pin every 10 milliseconds. When executed the servo will rotate in a to and fro motion as shown in the illustration above.
Raspberry Pi kit along with servo is a great package for developing real time robotic projects as the RPi supports both image and audio processing units. The servo can be controlled on the basis of the output obtained from image processing operations, which would aid realizing innovative robotic ideas.
- Raspberry Pi 2: Logging on to the 2nd generation
- Programming in Raspberry Pi 2 using Python
- An Arduino-RPi 2 Interface using Serial Communication
Shop With Us