/*
ESP32 + A4988 + Stepper (NEMA17) example for Wokwi
Wiring (matches diagram.json):
- STEP -> GPIO 18
- DIR -> GPIO 19
- EN -> GPIO 23 (LOW = enabled)
*/
#define STEP_PIN 18
#define DIR_PIN 19
#define EN_PIN 23
// A4988 microstep pins in diagram are tied LOW (full-step).
// Steps per revolution depends on your motor (commonly 200 for NEMA17).
const int STEPS_PER_REV = 200;
// Speed control (microseconds between step pulses)
int stepDelayUs = 800; // smaller = faster
void stepOnce() {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(3); // A4988 needs a short HIGH pulse (>= ~1us)
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelayUs);
}
void stepN(int steps) {
for (int i = 0; i < steps; i++) stepOnce();
}
void setup() {
Serial.begin(115200);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
// Enable driver (A4988 EN is active-low)
digitalWrite(EN_PIN, LOW);
Serial.println("Stepper demo: 1 rev CW, pause, 1 rev CCW...");
}
void loop() {
// Clockwise
digitalWrite(DIR_PIN, HIGH);
Serial.println("CW");
stepN(STEPS_PER_REV);
delay(500);
// Counter-clockwise
digitalWrite(DIR_PIN, LOW);
Serial.println("CCW");
stepN(STEPS_PER_REV);
delay(500);
}