How to set the HOME position of a Stepper at Startup!
- 11 May, 2017
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.
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.
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.