motor.py

 avatar
unknown
python
a year ago
1.2 kB
3
Indexable
# Set up libraries and overall settings
import RPi.GPIO as GPIO  # Imports the standard Raspberry Pi GPIO library
from time import sleep   # Imports sleep (aka wait or pause) into the program
GPIO.setmode(GPIO.BOARD) # Sets the pin numbering system to use the physical layout

# Set up pin 11 for PWM
GPIO.setup(11,GPIO.OUT)  # Sets up pin 11 to an output (instead of an input)
p = GPIO.PWM(11, 50)     # Sets up pin 11 as a PWM pin
p.start(0)               # Starts running PWM on the pin and sets it to 0

# Move the servo back and forth
p.ChangeDutyCycle(2.5)     # Changes the pulse width to 2.5. sets it to 0 degree
sleep(1)                 # Wait 1 second
p.ChangeDutyCycle(12.5)    # Changes the pulse width to 12.5. sets it to 180 degree
sleep(1)
p.ChangeDutyCycle(7.5)    # Changes the pulse width to 7.5. sets it to 90 degree
sleep(1)
p.ChangeDutyCycle(5)    # Changes the pulse width to 5. sets it to 45 degree
sleep(1)
p.ChangeDutyCycle(10)    # Changes the pulse width to 10. sets it to 135 degree
sleep(1)



# Clean up everything
p.stop()                 # At the end of the program, stop the PWM
GPIO.cleanup()           # Resets the GPIO pins back to defaults
Editor is loading...
Leave a Comment