/*Example sketch to control a stepper motor with A4988/DRV8825 stepper motor driver and Arduino without a library. More info: https://www.makerguides.com */ #include Servo myservo; // create servo object to control a servo int pos = 0; // variable to store the servo position // define microstepping signal pins #define m0 27 #define m1 28 #define m2 29 // Define stepper motor connections and steps per revolution: #define dirPin 2 #define stepPin 3 #define stepsPerRevolution 200 void setup() { myservo.attach(26); // attaches the servo on pin 26 to the servo object // Declare pins as output: pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(m0, OUTPUT); pinMode(m1, OUTPUT); pinMode(m2, OUTPUT); } void loop() { digitalWrite(m0, LOW); digitalWrite(m1, LOW); digitalWrite(m2, HIGH); // 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(500); digitalWrite(stepPin, LOW); delayMicroseconds(500); } for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(2000); // 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(500); digitalWrite(stepPin, LOW); delayMicroseconds(500); } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(2000); }