Skip to content

Instantly share code, notes, and snippets.

@sarful
Created June 2, 2020 02:44
Show Gist options
  • Save sarful/d822aa0edb04e8141bbf50be311c243e to your computer and use it in GitHub Desktop.
Save sarful/d822aa0edb04e8141bbf50be311c243e to your computer and use it in GitHub Desktop.
# 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(22,GPIO.OUT)
servo1 = GPIO.PWM(22,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(0.3)
servo1.ChangeDutyCycle(0)
time.sleep(0.7)
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(0.5)
servo1.ChangeDutyCycle(0)
time.sleep(1.5)
#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 ("Servo Stop")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment