This tutorial explains how to control servo with Raspberry Pi.
1. Introduction:
A servo is an actuator that rotates to a precise angle through command. The servo example included in this chapter rotates between 0 to 180 degree. It can move to any angle between 0-180 degree.
A servo receives command from the Pi Wedge B +, moves to the commanded angle and stops there. A servo has three interface in which two are for power supply and one is for signal input.
This tutorial is for Raspberry PI. Please note that the same tutorial can be performed on LUA as well.
1.2 Hardware Required:
S.No. | Item | Quantity |
1 | Raspberry Pi | 1 |
2 | Pi Wedge B + | 1 |
3 | Breadboard | 1 |
4 | Servo Motor | 1 |
5 | Jumper wire male to male | 3 |
2. Building Circuit
Schematic Diagram:
Circuit Layout:
3. Programming:
Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the python code to run this circuit.
You may download this code (Python Code) from here.
import RPi.GPIO as GPIO # 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 pwm.start(5) angle1=10 duty1= float(angle1)/10 + 2.5 # Angle To Duty cycle Conversion angle2=160 duty2= float(angle2)/10 + 2.5 ck=0 while ck<=5: pwm.ChangeDutyCycle(duty1) time.sleep(0.8) pwm.ChangeDutyCycle(duty2) time.sleep(0.8) ck=ck+1 time.sleep(1) GPIO.cleanup() # Import GPIO Library.
4. Output
Python Coding with Raspberry Pi connects your project to the real world.The easiest introduction to Python is through IDLE, a Python development environment.
Open IDLE (Python Development Environment) from the Desktop or applications menu:
Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.
Then write Python 3 file_name.py > Press Enter. (.py is file extension).
As the code executes, servo comes to 45 degree from whatever angles it was. Then there is a delay of one second. After that it goes to 90 degree stays there for one second and come back to 45 degree. This operation is performed in a continuous loop. Try this code for different angles.
5. Troubleshooting
Make connections and code as mentioned above, the servo should work as mentioned in the output. If still not working try changing the servo. It is recommended to use an external power supply while operating a servo. A computer’s USB power supply may not supply adequate current.
If you have any query please write us at support@roboindia.com
Thanks and Regards
Content Development Team
Robo India
https://roboindia.com