Skip to content

Commit 4a6ded4

Browse files
committed
a4988 examples
Example code for the A4988 in CircuitPython and Arduino
1 parent 4dd60a2 commit 4a6ded4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
const int DIR = 5;
6+
const int STEP = 6;
7+
const int microMode = 16; // microstep mode, default is 1/16 so 16; ex: 1/4 would be 4
8+
// full rotation * microstep divider
9+
const int steps = 200 * microMode;
10+
11+
void setup()
12+
{
13+
// setup step and dir pins as outputs
14+
pinMode(STEP, OUTPUT);
15+
pinMode(DIR, OUTPUT);
16+
}
17+
18+
void loop()
19+
{
20+
// change direction every loop
21+
digitalWrite(DIR, !digitalRead(DIR));
22+
// toggle STEP to move
23+
for(int x = 0; x < steps; x++)
24+
{
25+
digitalWrite(STEP, HIGH);
26+
delay(2);
27+
digitalWrite(STEP, LOW);
28+
delay(2);
29+
}
30+
delay(1000); // 1 second delay
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
from digitalio import DigitalInOut, Direction
8+
9+
# direction and step pins as outputs
10+
DIR = DigitalInOut(board.D5)
11+
DIR.direction = Direction.OUTPUT
12+
STEP = DigitalInOut(board.D6)
13+
STEP.direction = Direction.OUTPUT
14+
15+
# microstep mode, default is 1/16 so 16
16+
# another ex: 1/4 microstep would be 4
17+
microMode = 16
18+
# full rotation multiplied by the microstep divider
19+
steps = 200 * microMode
20+
21+
while True:
22+
# change direction every loop
23+
DIR.value = not DIR.value
24+
# toggle STEP pin to move the motor
25+
for i in range(steps):
26+
STEP.value = True
27+
time.sleep(0.001)
28+
STEP.value = False
29+
time.sleep(0.001)
30+
print("rotated! now reverse")
31+
# 1 second delay before starting again
32+
time.sleep(1)

0 commit comments

Comments
 (0)