top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

  • Writer's pictureBrainy-Bits

How to set the HOME position of a Stepper 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.



EasyDriver Stepper Driver

Joystick Module

Stepper Motor NEMA 17

These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!



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.

18,870 views0 comments

Recent Posts

See All


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page