Skip to content

Instantly share code, notes, and snippets.

@khiet
Created June 20, 2019 19:12
Show Gist options
  • Save khiet/fd705d69c28da581671643b1f5544b73 to your computer and use it in GitHub Desktop.
Save khiet/fd705d69c28da581671643b1f5544b73 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
# https://www.youtube.com/watch?v=ddlDgUymbxc
# SG90 angle and pulse width
# 0: 0.5 ms
# neutral: 1.5 ms
# 180: 2.5 ms
p = GPIO.PWM(7,50) # 50 Hz
p.start(7.5) # neutral ((1.5/20) * 100) %
try:
while True:
p.ChangeDutyCycle(2.5) # 0
time.sleep(1)
p.ChangeDutyCycle(7.5) # 90
time.sleep(1)
p.ChangeDutyCycle(12.5) # 180
time.sleep(1)
p.ChangeDutyCycle(7.5) # 90
time.sleep(1)
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment