The advantage of a stepper motor is that it can be precisely positioned to turn "one step" at a time, either forward or reverse, and can also be rotated continuously.
#!/usr/bin/env python######################################################### #File name:stepmotor.py# Author: Jason dai# date:2015/01/26######################################################## #import RPi.GPIO as GPIOimport Tim eIN1 = one # pin11in2 = 12in3 = 13in4 = 15def setstep (W1, W2, W3, W4): Gpio.output (IN1, W1) gpio.output (IN2, W2) GPIO.OUTPU T (IN3, W3) gpio.output (IN4, W4) def stop (): SetStep (0, 0, 0, 0) def forward (delay, steps): For I in range (0, steps): SetStep (1 , 0, 0, 0) time.sleep (delay) setstep (0, 1, 0, 0) time.sleep (delay) setstep (0, 0, 1, 0) time.sleep (delay) setstep (0, 0, 0, 1) time . Sleep (delay) def backward (delay, steps): For I in range (0, steps): SetStep (0, 0, 0, 1) time.sleep (delay) setstep (0, 0, 1, 0) Time.sleep (delay) setstep (0, 1, 0, 0) time.sleep (delay) setstep (1, 0, 0, 0) time.sleep (delay) def setup (): Gpio.setwarnings (False) Gpio.setmode (GPIO). BOARD) # Numbers GPIOs by physical Locationgpio.setup (IN1, GPIO. Out) # Set Pin ' s mode is Outputgpio.setup (IN2, GPIO. Out) GPIO. Setup (IN3, GPIO. Out) Gpio.setup (IN4, GPIO. def loop (): While True:print "Backward ..." backward (0.003, up to) # Steps---angleprint "Stop ..." Stop () # Stoptime.sleep (3) # sleep 3sprint "forward ..." forward (0.005) print "Stop ..." Stop () Time.sleep (3) def Destroy (): Gpio.cleanup () # Release Resourceif __name__ = = ' __main__ ': # program start from Heresetup () Try: Loop () except Keyboardinterrupt: # when ' CTRL + C ' is pressed, the child program Destroy () 'll be Executed.destroy ()
To run the program:
Python stepmotor.py
Using Raspberry Pi to control stepper motor--python language