Last Updated: 08/12/2021
Arduino and Nema 17 Stepper motor using micros()
Components/How To >> Arduino and Nema 17 Stepper motor using micros()
Arduino and Nema 17 Stepper motor using micros()
As part of my DCC Turntable project I needed to use a Nema 17 stepper motor. However, because the turntable is triggered by a DCC command that is also activating servos for point control I cannot have delay() in my code as is used in most stepper motor examples.
Normally I would get round this using millis() but because of the very short periods of time involved I needed to use micros() instead. This in itself is not a problem but because the value for microseconds since the Arduino resstarted in saved in an unsigned long, the value overruns and returns to ) after about 70 minutes unlike millis() that overruns after about 50 days.
As such the timing code needs to allow for the overrun.
Circuit Diagram
For information on setting up a Nema 17 stepper motro follow the link below.
Controlling a Stepper Motor with Arduino UNO
Example 1: Nema17_Stepper_Test_v1.ino Conventional stepper code using delay()
Click to Download code: Nema17_Stepper_Test_v1.ino
This sketch moves the stepper motor backwards and forwards, however because it uses delay() it block the Arduino from doing anything else.
This is not a problem if the only task being performed is to control the stepper motor but if you need yourArduino to perform other tasks at the same time you will have problems.
/*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 dirPin 9
#define stepPin 8
#define stepsPerRevolution 200
void setup() {
// Declare pins as output:
Serial.begin(9600);
Serial.println("a4988 stepper test");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Set the spinning direction clockwise:
Serial.println("clock");
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(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
// Set the spinning direction counterclockwise:
digitalWrite(dirPin, LOW);
Serial.println("anticlock");
// 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(300);
digitalWrite(stepPin, LOW);
delayMicroseconds(100);
}
delay(1000);
}
Example 2: Nema17MicrosV1.ino Stepper code using micros()
Click to Download code: Nema17MicrosV1.ino
This version uses micros() in a way that is not effected by the 70 minute rollover for an explanation of why try this link
Arduino Tutorial: Avoiding the Overflow Issue When Using millis() and micros()
/* Nema17MicrosV1
* How to control a Nema 17 stepper motor without using the delay() function
*
*/
const int stepPin = 8;
const int dirPin = 9;
unsigned long stepperTimer;
int currentStepperSpeedDelay = 500;//changing this value changes the stepper motor speed.
byte stepInState = 0;//high or low state for next step
int stepCounter;
const int clockwise = 0;
const int anticlockwise = 1;
int stepperDirection;//stores the current direction of rotation
void setup() {
Serial.begin(9600);
Serial.println("Nema17MicrosV1...");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, clockwise); // LOW for anticlockwise
stepperDirection = clockwise;
}
void loop() {
unsigned long currentMicros;
currentMicros = micros();
if ((currentMicros - stepperTimer) >= currentStepperSpeedDelay) {
stepperTimer = currentMicros;
if (stepInState > 0) {
stepInState = 0;
} else {
stepInState = 1;
}
digitalWrite(stepPin, stepInState);
stepCounter++;
}
//after 200 steps change direction
if(stepCounter > 200){
stepCounter = 0;//reset the counter
if(stepperDirection == clockwise){
stepperDirection == anticlockwise;
digitalWrite(dirPin, clockwise); //change the dirPin
}else{
stepperDirection == clockwise;
digitalWrite(dirPin, clockwise); //change the dirPin
}
}
}
Additional Resource Links
Controlling a Stepper Motor with Arduino UNO
Arduino Tutorial: Avoiding the Overflow Issue When Using millis() and micros()
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Arduino and Nema 17 Stepper motor using micros() as a reference.