How to Control Servos Using Raspberry Pi
by HanaA11 in Circuits > Raspberry Pi
531 Views, 1 Favorites, 0 Comments
How to Control Servos Using Raspberry Pi
In this tutorial, we are learning the basics of how to control a servo using Raspberry Pi
You can make a bunch of projects with servos including robots, they are affordable, light, and don't require high voltage.
Supplies
Raspberry Pi completed with Screen (you can use a TV or and monitor you have), Keyboard and Mouse
Wiring: Connect the Circuit
1. Attach male to female jumper wires to the servo
2. Referring to the picture attached, Connect:
- Orange wire to GPIO17
- Red wire to 5V
- Brown wire to GND
Script
1. Launch Thonny or Python and paste the script below:
#!/usr/bin/env python<br>import RPi.GPIO as GPIO import time SigPin = 11 T = 0.02 #0.02s, 50HZ pluse_width = [0.0005, 0.0010, 0.00125, 0.0015, 0.00175, 0.0020, 0.0025] #0,45,68,90,112,135,180 def set_angle(angle): GPIO.output(SigPin, GPIO.HIGH) time.sleep(angle) GPIO.output(SigPin, GPIO.LOW) time.sleep(T-angle) def setup(): GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location GPIO.setup(SigPin, GPIO.OUT) #set pin mode is output GPIO.output(SigPin, GPIO.LOW) #set pin high level(3.3V) def loop(): while True: set_angle(pluse_width[0]) time.sleep(0.5) set_angle(pluse_width[1]) time.sleep(0.5) set_angle(pluse_width[2]) time.sleep(0.5) set_angle(pluse_width[3]) time.sleep(0.5) set_angle(pluse_width[4]) time.sleep(0.5) def destroy(): #When program ending, the function is executed. GPIO.output(SigPin, GPIO.LOW) GPIO.setup(SigPin, GPIO.IN) #set pin mode is input GPIO.cleanup() def ctrl(angle): n = 50 while n: n = n - 1 setup() if angle == 0: set_angle(0.0005) elif angle == 45: set_angle(0.0010) elif angle == 68: set_angle(0.00125) elif angle == 90: set_angle(0.0015) elif angle == 112: set_angle(0.00175) elif angle == 135: set_angle(0.0020) elif angle == 180: set_angle(0.0025) destroy() if __name__ == '__main__': #Program start from here setup() try: loop() except KeyboardInterrupt: destroy()
* Code sourced from https://www.sunfounder.com/learn/Basic-Kit-for-Ras...
2. Hit [Run]
The servo should rotate
*You can experiment with the rotation of the servo by editing the code just under
def loop(): while True:
Try to delete or change time.sleep from (0.5) to your desired time and figure out what's suits your project