// STEP -> pin 3, DIR -> pin 4, ENABLE -> pin 5
// Assumes a 200-step-per-rev motor (1.8° full step) and A4988 in full-step mode.
// If you change microstepping, set STEPS_PER_REV accordingly.
const uint8_t STEP_PIN = 3;
const uint8_t DIR_PIN = 4;
const uint8_t ENABLE_PIN = 5;
const unsigned int STEPS_PER_REV = 1000; // change to 1600 for 1/16 microstep, etc.
const unsigned long STEP_DELAY_US = 1000; // pulse spacing in microseconds (controls speed)
void stepOne(bool dir) {
digitalWrite(DIR_PIN, dir ? HIGH : LOW);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(2); // STEP pulse width (min 1-2 µs)
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(STEP_DELAY_US); // time between steps
}
void stepRevolution(bool dir) {
for (unsigned int i = 0; i < STEPS_PER_REV; ++i) {
stepOne(dir);
}
}
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // ENABLE is active LOW on many A4988 modules (LOW = enabled)
}
void loop() {
stepRevolution(true); // one full revolution clockwise
delay(2000); // pause 2 seconds
stepRevolution(false); // one full revolution counterclockwise
delay(2000); // pause 2 seconds before repeating
}