// Example sketch to control a stepper motor with A4988 stepper motor driver
// and Arduino without a library.
// More info: https://www.makerguides.com
// Define stepper motor connections and steps per revolution:
#define M1dirPin 8
#define M1stepPin 9
#define M1stepsPerRevolution 200
#define M2dirPin 11
#define M2stepPin 12
#define M2stepsPerRevolution 200
void setup() {
// Declare pins as output:
pinMode(M1stepPin, OUTPUT);
pinMode(M1dirPin, OUTPUT);
pinMode(M2stepPin, OUTPUT);
pinMode(M2dirPin, OUTPUT);
}
void loop() {
// Set the spinning direction clockwise:
digitalWrite(M1dirPin, HIGH);
digitalWrite(M2dirPin, HIGH);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < M1stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(M1stepPin, HIGH);
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M1stepPin, LOW);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
// Set the spinning direction counterclockwise:
digitalWrite(M1dirPin, LOW);
digitalWrite(M2dirPin, LOW);
// Spin the stepper motors 1 revolution quickly:
for (int i = 0; i < M1stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(M1stepPin, HIGH);
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(M1stepPin, LOW);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000);
// Set the spinning direction clockwise:
digitalWrite(M1dirPin, HIGH);
digitalWrite(M2dirPin, HIGH);
// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * M1stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(M1stepPin, HIGH);
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(M1stepPin, LOW);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
// Set the spinning direction counterclockwise:
digitalWrite(M1dirPin, LOW);
digitalWrite(M2dirPin, LOW);
//Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * M1stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(M1stepPin, HIGH);
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(M1stepPin, LOW);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}