-
Notifications
You must be signed in to change notification settings - Fork 0
/
StpperMotor.py
49 lines (44 loc) · 1.78 KB
/
StpperMotor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
Stepper Motor interfacing with Raspberry Pi
http:///www.electronicwings.com
'''
import RPi.GPIO as GPIO
from time import sleep
import sys
#assign GPIO pins for motor
motor_channel = (3,5,7,11)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#for defining more than 1 GPIO channel as input/output use
GPIO.setup(motor_channel, GPIO.OUT)
motor_direction = input('select motor direction a=anticlockwise, c=clockwise: ')
while True:
try:
if(motor_direction == 'c'):
print('motor running clockwise\n')
GPIO.output(motor_channel, (GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.HIGH))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.LOW,GPIO.HIGH,GPIO.HIGH,GPIO.LOW))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.HIGH))
sleep(0.002)
elif(motor_direction == 'a'):
print('motor running anti-clockwise\n')
GPIO.output(motor_channel, (GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.HIGH))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.HIGH))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.LOW,GPIO.HIGH,GPIO.HIGH,GPIO.LOW))
sleep(0.002)
GPIO.output(motor_channel, (GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW))
sleep(0.002)
#press ctrl+c for keyboard interrupt
except KeyboardInterrupt:
#query for setting motor direction or exit
motor_direction = input('select motor direction a=anticlockwise, c=clockwise or q=exit: ')
#check for exit
if(motor_direction == 'q'):
print('motor stopped')
sys.exit(0)