Help: Stepper motor to start from a fixed position

Hello guys,
I am currently working on a project that requires stepper motor and relay control from arduino. I am new to this field and have only a little knowledge about coding
I used a code from to move my stepper motor with BED and it works perfectly with the stepper motor. But the problem is I want my stepper motor to always start from a fixed or 0 position. With this the motor starts always where it was previously disconnected. Is there any way that I can control the motor to start at fixed position and move to certain mentioned degrees and back again to that position?

Here is a code I modified according to my requirements with relay controls. I am not getting it where and how can I put the coding for the stepper motor to start from a fixed position…

#define DIR_PIN 8
#define STEP_PIN 9
#define RELAY1  2                        
#define RELAY2  3                        
#define RELAY3  4 

void setup() {
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT);

void loop() {
   rotateDeg(360, .2); 
   rotateDeg(-360, .09);                //reverse
   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
   rotateDeg(90, .2); 
   rotateDeg(-90, .09);                //reverse
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
   rotateDeg(90, .2); 
   rotateDeg(-90, .09);                //reverse
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
   rotateDeg(90, .2); 
   rotateDeg(-90, .09);                //reverse
   digitalWrite(RELAY3,HIGH);          // Turns Relay Off 

void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;

  int steps = abs(deg)*(2/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 

    digitalWrite(STEP_PIN, LOW); 

I’d be obliged if anyonecan guide me with this code to help my stepper motor to always start from it’s zero position no matter at which position it is switched off.


To ensure it starts at a known position, a shaft encoder could be used. Then your software can tell which position the motor shaft is in, and then your motor can be commanded (via software) to take the least number of steps to get back to the chosen starting position.

Otherwise, with no encoder, I guess that you could first turn off the arduino, then manually move the shaft to some desired starting position. Then, in your "void setup" routine of your code, initialise coils A:A' with HIGH:LOW; and B:B' with HIGH:LOW. In other words apply HIGH to terminal A and HIGH to terminal B.

Then, when you start stepping, you'll need to set up your own 'look-up' table for forwards and reverse stepping.

For forward stepping, since you have already initialised A and B terminals with HIGH and HIGH.

The first forward step command for terminals A and B will be LOW:HIGH The next forward step command after that for A and B would be LOW:LOW Followed by HIGH:LOW. Then HIGH:HIGH

The above would represent 4 steps in 1 direction.

If you wanted to go in the other direction, then you would apply:


So once the software routine looks ok, just turn on the arduino again. Then see if everything works. The software just needs to keep track of the most-recently applied coil voltages (polarity), so that it knows what's the next polarity to apply in order to make the next step in the forward or reverse direction.

If no encoder is used, then we would be hoping that our stepper always makes the correct number of steps. And we also rely on shutting down the system only when the shaft is back in its original starting position - where the shaft may just stay in that position until the system gets powered up again later.


Thank you for your detailed explanation. I'll try to implement these strategies in the project. Thanks again..

It has to be an absolute shaft encoder or an incremental encoder with an index pulse (that requires you
move the motor until you see the index pulse though). A homing switch can also do this.

The cheapest solution I’ve seen for absolute encoder is the magnetic encoder chips such as AS5040
which require a special magnet to be mounted on the end of the shaft. It is of course surface mount.

A relatively cheap incremental encoder option is maybe a YUMO E6B2-CWZ3E (or related), which could be bolted onto a stepper that has a shaft protruding out the back-end (as well as the front end). For a NEMA 17 stepper, 50 millimetre length metal 'standoff' posts with M3 size screws can be used to connect to the NEMA 17, while a custom plate can be placed on the other side of the posts - where a cheap custom plate with a (large through-hole) can be used for mounting the YUMO. And screw holes for the YUMO can also be drilled into the plate for fixing the YUMO in place. The YUMO shaft and NEMO 17 shaft can then be coupled together using a shaft coupler (eg a 5 mm shaft to 6 mm shaft adapter). Not all NEMA 17's come with a shaft that pokes out at both ends of the motor. But ones that do can connect to a YUMO - after a little effort in customisation.

Although, an absolute encoder is best I reckon - as we immediately know which direction to go - in order to have the least steps to get back home. Absolute encoders can be pricey unfortunately.

The usual (and simple) way to locate a stepper motor is with a limit switch. At startup, move the motor step by step until it trips the switch. At that point the Arduino knows that the motor is at the HOME or ZERO position.


Here is a picture of my setup, with YUMO E62B 10 bit incremental encoder and NEMA 17 motor. The plastic screw heads have their sides clipped with a toe-nail clipper so that the YUMO encoder can fit into the slots between the posts. This is the same clipping method I use to fit nylon screws next to Arduino header boards (because some header boards get in the way of ‘complete’ screw heads).

The coupler connects the shafts together. The only thing needed is to simply add a sensor switch or any device that allows the stepper to find the home position through the basic stepping and testing process.

I’m currently putting together a plate to fit an absolute encoder TRD-NA1024NW-2302 (to replace the incremental one). TRD-NA1024NW-2302 can be obtained relatively cheaply from ebay.


ajayr14: @Southpark Thank you for your detailed explanation. I'll try to implement these strategies in the project. Thanks again..

Most welcome ajay. A bunch of great advice from all the members here. All the best with your project.

@MarkT and Robin2

Thanks a lot