Skip to content

Commit 5ec4b45

Browse files
authored
Merge pull request #576 from adafruit/Raspberry_Pi_Stepper_Motors
Raspberry pi stepper motors
2 parents 78f9f0c + 83051ea commit 5ec4b45

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Raspberry_Pi_Stepper_Motors
2+
3+
Code to accompany this tutorial:
4+
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-10-stepper-motors
Binary file not shown.
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import time
2+
import board
3+
import digitalio
4+
5+
enable_pin = digitalio.DigitalInOut(board.D18)
6+
coil_A_1_pin = digitalio.DigitalInOut(board.D4)
7+
coil_A_2_pin = digitalio.DigitalInOut(board.D17)
8+
coil_B_1_pin = digitalio.DigitalInOut(board.D23)
9+
coil_B_2_pin = digitalio.DigitalInOut(board.D24)
10+
11+
enable_pin.direction = digitalio.Direction.OUTPUT
12+
coil_A_1_pin.direction = digitalio.Direction.OUTPUT
13+
coil_A_2_pin.direction = digitalio.Direction.OUTPUT
14+
coil_B_1_pin.direction = digitalio.Direction.OUTPUT
15+
coil_B_2_pin.direction = digitalio.Direction.OUTPUT
16+
17+
enable_pin.value = True
18+
19+
def forward(delay, steps):
20+
i = 0
21+
while i in range(0, steps):
22+
setStep(1, 0, 1, 0)
23+
time.sleep(delay)
24+
setStep(0, 1, 1, 0)
25+
time.sleep(delay)
26+
setStep(0, 1, 0, 1)
27+
time.sleep(delay)
28+
setStep(1, 0, 0, 1)
29+
time.sleep(delay)
30+
i += 1
31+
32+
def backwards(delay, steps):
33+
i = 0
34+
while i in range(0, steps):
35+
setStep(1, 0, 0, 1)
36+
time.sleep(delay)
37+
setStep(0, 1, 0, 1)
38+
time.sleep(delay)
39+
setStep(0, 1, 1, 0)
40+
time.sleep(delay)
41+
setStep(1, 0, 1, 0)
42+
time.sleep(delay)
43+
i += 1
44+
45+
def setStep(w1, w2, w3, w4):
46+
coil_A_1_pin.value = w1
47+
coil_A_2_pin.value = w2
48+
coil_B_1_pin.value = w3
49+
coil_B_2_pin.value = w4
50+
51+
while True:
52+
user_delay = input("Delay between steps (milliseconds)?")
53+
user_steps = input("How many steps forward? ")
54+
forward(int(user_delay) / 1000.0, int(user_steps))
55+
user_steps = input("How many steps backwards? ")
56+
backwards(int(user_delay) / 1000.0, int(user_steps))

0 commit comments

Comments
 (0)