#define STEP_PIN 3 // Connect A4988 STEP pin to Arduino D3
#define DIR_PIN 4 // Connect A4988 DIR pin to Arduino D4
#define EN_PIN 8 // Connect A4988 ENABLE pin to Arduino D8 (optional)
const int stepsPerRevolution = 200; // 200 steps per revolution for 1.8° stepper motor
const int microstepping = 1; // Adjust this if you're using microstepping (1, 2, 4, 8, 16)
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable the motor driver (LOW to enable, HIGH to disable)
digitalWrite(DIR_PIN, HIGH); // Set initial direction
Serial.begin(9600);
Serial.println("A4988 stepper motor ready...");
}
void loop() {
digitalWrite(DIR_PIN, HIGH);
Serial.println("Moving clockwise...");
rotateMotor(stepsPerRevolution * microstepping); // Move 1 full revolution clockwise
delay(1000);
Serial.println("Moving counterclockwise...");
digitalWrite(DIR_PIN, LOW); // Change direction
rotateMotor(stepsPerRevolution * microstepping); // Move 1 full revolution counterclockwise
delay(1000);
}
// Function to pulse STEP pin to rotate motor
void rotateMotor(int steps) {
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(800); // Adjust delay for speed control
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(800);
}
}