// 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 DIR_PIN 2
#define STEP_PIN 3
#define STEPS_PER_REVOLUTION 200
void setup() {
// Declare pins as output:
Serial.begin(115200);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
}
void loop() {
// Set the spinning direction clockwise:
Serial.println("Spinning clockwise slow");
digitalWrite(DIR_PIN, HIGH);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < STEPS_PER_REVOLUTION; i++) {
// These four lines result in 1 step:
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(2000);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(2000);
}
delay(1000);
// Set the spinning direction counterclockwise:
Serial.println("Spinning counterclockwise faster");
digitalWrite(DIR_PIN, LOW);
// Spin the stepper motor 1 revolution quickly:
for (int i = 0; i < STEPS_PER_REVOLUTION; i++) {
// These four lines result in 1 step:
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(1000);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1000);
}
delay(1000);
// Set the spinning direction clockwise:
Serial.println("Spinning clockwise fast");
digitalWrite(DIR_PIN, HIGH);
// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * STEPS_PER_REVOLUTION; i++) {
// These four lines result in 1 step:
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500);
}
delay(1000);
// Set the spinning direction counterclockwise:
Serial.println("Spinning counterclockwise fast");
digitalWrite(DIR_PIN, LOW);
//Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * STEPS_PER_REVOLUTION; i++) {
// These four lines result in 1 step:
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500);
}
delay(1000);
}