-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpico_stepper.py
More file actions
executable file
·99 lines (82 loc) · 2.15 KB
/
pico_stepper.py
File metadata and controls
executable file
·99 lines (82 loc) · 2.15 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
#--------------------------------------
# ___ ___ _ ____
# / _ \/ _ \(_) __/__ __ __
# / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/ /_/___/ .__/\_, /
# /_/ /___/
#
# Stepper Motor Test
#
# A simple script to control
# a stepper motor.
#
# Author : Matt Hawkins
# Date : 28/09/2015
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
# Import required libraries
from machine import Pin, Timer
import time
led = Pin('LED', Pin.OUT)
timer = Timer()
pins = {}
def GPIO_setup(list, dir):
"Initialise a list of GPIOs"
for i in list:
pin = Pin(i, dir)
pins[i] = pin
def GPIO_output(list, values):
"Set one or more GPIOs to one or more values"
for (i,j) in zip(list, values):
pins[i].value(j)
# Define GPIO signals to use
# Physical pins 11,15,16,18
# GPIO17,GPIO22,GPIO23,GPIO24
#StepPins = [16,2,17,3] # motor 0
#StepPins = [4,6,5,7] # motor 1
#StepPins = [8,10,9,11] # motor 2
#StepPins = [12,14,13,15] # motor 3
StepPins = [16,2,17,3] # motor 3
# Set all motor pins as outputs
GPIO_setup(StepPins, Pin.OUT)
# and set to off
GPIO_output(StepPins, [0, 0, 0, 0])
# Define advanced sequence
# as shown in manufacturer's datasheet
Seq = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
StepCount = len(Seq)
StepDir = 1 # Set to 1 or 2 for clockwise
# Set to -1 or -2 for anti-clockwise
# Read wait time from command line
# if len(sys.argv)>1:
# WaitTime = int(sys.argv[1])/float(1000)
# else:
WaitTime = 20/float(1000)
# Initialise variables
StepCounter = 0
try:
print("Running, WaitTime = ", WaitTime, "s")
# Start main loop
while True:
led.toggle()
GPIO_output(StepPins, Seq[StepCounter])
# If we reach the end of the sequence
# start again
StepCounter = (StepCounter + StepDir) % StepCount
# Wait before moving on
time.sleep(WaitTime)
except KeyboardInterrupt:
print ("Done.")
GPIO_output(StepPins, [0, 0, 0, 0])
# and set to input
GPIO_setup(StepPins, Pin.IN)