Skip to content

Instantly share code, notes, and snippets.

@electronut
Created November 24, 2014 11:21
Show Gist options
  • Save electronut/646ec6157383cb7c43ad to your computer and use it in GitHub Desktop.
Save electronut/646ec6157383cb7c43ad to your computer and use it in GitHub Desktop.
Driving two servos with the hardware PWM on a Raspberry Pi Model A+.
"""
test_HW_PWM_servo.py
Driving two servos with the hardware PWM on a Raspberry Pi Model A+.
Connect GPIO18 and GPIO13 to servo signal pins. Connect GND of Pi to GND of Ser\
vos. Use separate power supply (5V) for servos.
Author: Mahesh Venkitachalam
Website: electronut.in
"""
import wiringpi2 as wiringpi
import time
# main() function
def main():
print("starting PWM")
# use BCM GPIO numbers
wiringpi.wiringPiSetupGpio()
# enable PWM0
wiringpi.pinMode(18,2)
wiringpi.pwmSetMode(0)
wiringpi.pwmSetClock(400)
wiringpi.pwmSetRange(1024)
wiringpi.pwmWrite(18, 0)
# enable PWM1
wiringpi.pinMode(13,2)
wiringpi.pwmSetMode(0)
wiringpi.pwmSetClock(400)
wiringpi.pwmSetRange(1024)
wiringpi.pwmWrite(13, 0)
# loop
dtMin, dtMax = 2, 128
dt = 2
while True:
try:
print(dt)
wiringpi.pwmWrite(18, dt)
wiringpi.pwmWrite(13, dtMax-dt)
dt = dt + 10
if dt > dtMax:
dt = dtMin
time.sleep(0.5)
except:
# clean up
wiringpi.pwmWrite(18, 0)
wiringpi.pwmWrite(13, 0)
print("exiting.")
break
# call main
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment