Skip to content

Instantly share code, notes, and snippets.

@labsguru
Created December 22, 2021 05:40
Show Gist options
  • Select an option

  • Save labsguru/6b2a52ce3807a7fbf390c70cd8a4140e to your computer and use it in GitHub Desktop.

Select an option

Save labsguru/6b2a52ce3807a7fbf390c70cd8a4140e to your computer and use it in GitHub Desktop.
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
void setup() { //https://kitsguru.myshopify.com/products/reprap-a4988-stepper-motor-driver-3d-printer-heat-sink-with-sticker
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(1000);
// Set the spinning direction counterclockwise:
digitalWrite(dirPin, LOW);
// Spin the stepper motor 1 revolution quickly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(1000);
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
// Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
// Set the spinning direction counterclockwise:
digitalWrite(dirPin, LOW);
//Spin the stepper motor 5 revolutions fast:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
} //CREDITS : https://www.makerguides.com/a4988-stepper-motor-driver-arduino-tutorial/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment