-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservoTest.py
46 lines (36 loc) · 973 Bytes
/
servoTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Import libraries
import RPi.GPIO as GPIO
import time
# Set GPIO numbering mode
GPIO.setmode(GPIO.BOARD)
# Set pin 11 as an output, and set servo1 as pin 11 as PWM
GPIO.setup(7,GPIO.OUT)
servo1 = GPIO.PWM(7,50) # Note 11 is pin, 50 = 50Hz pulse
#start PWM running, but with value of 0 (pulse off)
servo1.start(0)
print ("Waiting for 2 seconds")
time.sleep(2)
#Let's move the servo!
print ("Rotating 180 degrees in 10 steps")
# Define variable duty
duty = 2
# Loop for duty values from 2 to 12 (0 to 180 degrees)
while duty <= 12:
servo1.ChangeDutyCycle(duty)
time.sleep(1)
duty = duty + 1
# Wait a couple of seconds
time.sleep(2)
# Turn back to 90 degrees
print ("Turning back to 90 degrees for 2 seconds")
servo1.ChangeDutyCycle(7)
time.sleep(2)
#turn back to 0 degrees
print ("Turning back to 0 degrees")
servo1.ChangeDutyCycle(2)
time.sleep(0.5)
servo1.ChangeDutyCycle(0)
#Clean things up at the end
servo1.stop()
GPIO.cleanup()
print ("Goodbye")