Homing stepper motors using the AccelStepper library!

//Homing stepper motors using the AccelStepper library!

Setting Stepper Motors HOME position using the AccelStepper Library


Following in the foot steps of our last tutorial: How to set the home position of stepper motors at startup, we will now see how to achieve the same results using the popular “AccelStepper” library.

Instead of using a joystick to control the stepper motor movements after Homing, we will be using the Serial Monitor window.


This time we are using the Arduino NANO instead of the UNO.  Since the NANO is basically a smaller version of the UNO (give or take a few differences), the connections would be similar, but since we are using the AccelStepper library we need even less connections.

The Easy Driver:

Pin 2 of the NANO is connected to STEPS
Pin 3 to DIR
and GND is connected to a GND pin on the Arduino

*The Voltage and GND (at the top of the Easy Driver)  are connected to a 12V 1A power supply.
The NEMA 17 motor we are using has a max amperage draw of around 0.45A.

Stepper Motor Connections:

The 4 leads of the NEMA stepper (2 per coils), are connected directly to the Easy Driver A and B group pins.

A quick way to identify which wires are part of the same coil is to connect two of those four wires together and if you feel resistance when trying to turn the stepper motor shaft, that means that those 2 wires are part of the same coil.

The limit switch NO (Normally Open) pin is connected to pin 9 of the Arduino

The COM (common) pin of the switch is connected to a GND pin on the Arduino


As in our last tutorial, we want to start homing the stepper right away at startup.

The way the AccelStepper library works, is that at startup the position value is always zero, then you tell the library to move to a position relative to the zero position.

For example if you set the position to “move to” to 300, and then issue the “run” command, the stepper will move 300 steps away clockwise from the zero position.

If after this, you set the position to 350, the stepper will move clockwise 50 steps, since it his already at the position 300.

So the position to move to is relative to the current position.

A negative number will move the stepper counter clockwise.

In this tutorial we are using the Serial Monitor window to control the movements of the stepper once the Homing is complete.

As always for more information about the tutorial and explanation of the code please watch our tutorial video.

/*  Motor Homing code using AccelStepper and the Serial Monitor
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!


#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperX(1, 2, 3);   // 1 = Easy Driver interface
                                  // NANO Pin 2 connected to STEP pin of Easy Driver
                                  // NANO Pin 3 connected to DIR pin of Easy Driver

// Define the Pins used
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)

// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup

void setup() {
   Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
   pinMode(home_switch, INPUT_PULLUP);
   delay(5);  // Wait for EasyDriver wake up

   //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper

// Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepperX.run();  // Start moving the stepper

  stepperX.setCurrentPosition(0);  // Set the current position as zero for now
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
  Serial.println("Homing Completed");
  stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper

// Print out Instructions on the Serial Monitor at Start
  Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");

void loop() {

 while (Serial.available()>0)  { // Check if values are available in the Serial Buffer

  move_finished=0;  // Set variable for checking move of the Stepper
  TravelX= Serial.parseInt();  // Put numeric value from buffer in TravelX variable
  if (TravelX < 0 || TravelX > 1350) {  // Make sure the position entered is not beyond the HOME or MAX position
    Serial.println("Please enter a value greater than zero and smaller or equal to 1350.....");
  } else {
    Serial.print("Moving stepper into position: ");
  stepperX.moveTo(TravelX);  // Set new moveto position of Stepper
  delay(1000);  // Wait 1 seconds before moving the Stepper

  if (TravelX >= 0 && TravelX <= 1350) {

// Check if the Stepper has reached desired position
  if ((stepperX.distanceToGo() != 0)) {
    stepperX.run();  // Move Stepper into position

// If move is completed display message on Serial Monitor
  if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
     Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
    move_finished=1;  // Reset move variable

Copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.

Download the AccelStepper library created by Mike McCauley here

By | 2018-02-23T16:47:57+00:00 July 16th, 2017|Tutorials|

One Comment

  1. Julian May 30, 2018 at 12:28 am - Reply

    Great post. Really well explained and loved the video! However… I have a slight problem. For some reason, my stepper motor will not change direction. When I input a new position negative to the last position (say 100 after having put a 500 in) it goes in the same direction. So the stepper always goes clockwise. I don’t understand why? To trouble shoot:
    1. I added an LED into the circuit between the arduino pin and the DIR in on the Easy Driver and it lights up/goes out as it should.
    2. To check that both coils inside the stepper were working, I swapped the wires around and it does the same thing as before but counter clockwise. Again, the DIR circuit looks to be firing as it should.

    Is this a problem with the easy driver somehow? Perhaps on the other side of the DIR pin there is a fault or something?

    Any help would be greatly appreciated!



Leave A Comment