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

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

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.

 


SCHEMATIC

Home Switch Arduino Stepper Motor

 

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

 


THE CODE

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 / 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!
Thx!

*/

// 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);
    delay(5);   
}

  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);
    delay(10);
  }

  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);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      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);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;  // Increase the number of steps taken
      }
    }
}


TUTORIAL VIDEO



DOWNLOAD

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. 

2 Comments

Vishnu Das V - May 11, 2017

I need to make an inverted pendulum setup with stepper motor. It’s a cart on a linear track.
So limit switches will be placed at both ends. Now every time when I start the run the code the cart should move to the middle of linear track. How can I know the counts/pulses to be provided to stepper for the cart to be in middle?

abderrahim - May 11, 2017

hello ,
i’m trying to use a switch to home the stepper Motor and to change the direction in the same time , with the same switch ,
my motor need to do like 1000 steps tell hi touch the switch and then go back to to start point
thank u so much for the help

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up