How to set the HOME position of a Stepper at Startup!

//How to set the HOME position of a Stepper at Startup!

How to HOME position of a Stepper Motor at Startup


If you’ve been following our tutorials for a while, you will have noticed that we have used Stepper Motors in multiple projects.

One thing to keep in mind is that when power is removed, the information about the position of the Stepper Motor is lost and the project as no way to know what the position is at next power up.

So in this tutorial we will see how to add a simple switch to any Stepper project that can be used to “Home” the Stepper Motor at startup, and set that position as Zero or Home.

We can then calculate the maximum number of steps we can go forward from that position and use that as the limit travel the stepper can move.  This way we don’t have to use another Limit switch.

In this first part we will not use any Libraries to move the stepper motor, in part 2 we will see how to achieve the same results by using the popular AccelStepper library.


The Easy Driver:

Arduino Pin 6 is connected to DIR on the Easy Driver board
Pin 5 to STEPS
Pin 6 to DIR
Pin 2 to MS1
Pin 3 to MS2
Pin 8 to SLEEP
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 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 Joystick:

Arduino pin A0 is connected to the X-axis pin of the Joystick
GND and 5V are connected to 5V and GND pin on the Arduino

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


We want the “Homing” of the Stepper Motor to happen at startup, so we will put all the code needed in the “SETUP” portion of the Arduino code, which gets executed first before the main “LOOP”.

We are using 2 While loops to achieve the reset or Homing of the stepper.

First one gets executed as long as the Homing Switch is not activated, it rotates the Stepper Motor towards the switch until it activates.

The second While loop gets executed when the switch is activated, it rotates the Stepper Motor away from the switch until it deactivates.

We then set the “steps” variable to zero.

The Stepper Motor is now at Home.

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

/* Simple Stepper Motor Homing code
Created by Yvan /
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!


// Define the Pins used
#define step_pin 5    // Pin 5 connected to Steps pin on EasyDriver
#define dir_pin 6     // Pin 6 connected to Direction pin
#define MS1 2         // Pin 3 connected to MS1 pin
#define MS2 3         // Pin 4 connected to MS2 pin
#define sleep_pin 8   // Pin 8 connected to SLEEP pin

#define x_pin A0      // Pin A0 connected to joystick x axis pin

#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)

int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(sleep_pin, OUTPUT);
   pinMode(x_pin, INPUT);

   pinMode(home_switch, INPUT_PULLUP);
   digitalWrite(sleep_pin, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up

/* Configure type of Steps on EasyDriver:
// MS1 MS2
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //

   digitalWrite(MS1, HIGH);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps

// Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(dir_pin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(dir_pin, LOW); 
    digitalWrite(step_pin, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(step_pin, LOW);

  steps=0;  // Reset position variable to zero

void loop() {

// Enable movement of Stepper Motor using the Joystick

  while (analogRead(x_pin) >= 0 && analogRead(x_pin) <= 100) {
    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pin, HIGH);
      digitalWrite(step_pin, LOW);
      steps--;   // Decrease the number of steps taken
    while (analogRead(x_pin) > 900 && analogRead(x_pin) <= 1024) {
      if (steps < 650) {      // Maximum steps the stepper can move away from the Home Position
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
         digitalWrite(step_pin, LOW);
        steps++;  // Increase the number of steps taken

No library are needed for this tutorial.

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

By | 2018-02-23T16:50:00-05:00 May 12th, 2017|Tutorials|


  1. YDIDUC1 August 13, 2020 at 12:05 pm - Reply

    Is there a way to set the home position without a limit switch. I want be to make sure that the valve connected to the stepper motor is fully closed before I start my program to allow air through the valve. I will be using the easy driver board.

  2. Dan Sitzmann July 15, 2020 at 12:01 pm - Reply

    Great tutorial! Appreciate the effort you put into this. I do have a question though. I have modified this a little as I don’t need the serial monitor and am using a button to move the stepper after home. The project I have is to home the motor, press and hold a button to move away from home and release the button to stop. When the button is released, I need the motor to reverse back to the home switch. Then repeat pushing the button to move. I don’t need this to be super accurate so no need to have the motor come off the home switch. How do I get the motor to automatically return to home switch after the move? The move wont be much, but it could be different every time. I guess email would be easiest to share the sketch I have. Thank you for your time.

  3. claudio April 8, 2020 at 10:41 pm - Reply

    I would like that when doing homing, the motor does 325 steps so that “that” is the zero point and from there it moves to the right or left (with your maximum of 650),somebody helpme???

  4. Paul Morel March 31, 2019 at 11:49 pm - Reply

    My CNC is powered with an Arduino Mega 2560 with Ramps 1.4. Each axis has two endstops. Each endstop is 3 wire to NC, NO and COM and connected to my Ramps board. When the endstop is triggered, the Arduion shuts down and disconnects from Repetier Host. I would like to use one endstop from each from the X and Y axis as a proximity homing sensor. I watched your video and tutorial and I’m not able to accomplish this. Can you help guide me with this?

    • claudio April 8, 2020 at 10:36 pm - Reply

      tienes un corto en tu final de carrera….o lo has conectado mal,al hacer corto te apaga el arduino

  5. JOSE ROSA RODRIGUES September 23, 2018 at 7:47 pm - Reply

    Excellent tutorial.. congrats!!!

  6. Paul April 7, 2018 at 7:20 pm - Reply

    Thanks for the great tutorial. I modified the code to use a second stepper motor (x and y control). I would like to add a third stepper, that upon button press in the joystick, it will rotate a set number of revolutions or steps and then rotate back to the original position. Do you have any suggestions? Thanks again.

Leave A Comment