/*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 = 45; // variable to store the servo position // define microstepping signal pins #define dirPin 26 #define stepPin 27 #define sleepPin 28 #define resetPin 29 #define m0 0 #define m1 7 #define m2 6 #define stepsPerRevolution 200 void setup() { myservo.attach(3); // attaches the servo on pin 26 to the servo object // Declare pins as output: pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(sleepPin, OUTPUT); pinMode(resetPin, OUTPUT); pinMode(m0, OUTPUT); pinMode(m1, OUTPUT); pinMode(m2, OUTPUT); } void loop() { digitalWrite(sleepPin, HIGH); digitalWrite(resetPin, HIGH); // Set 1/16 microstepping 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 = 45; pos <= 90; 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 = 90; pos >= 45; 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); }