Stepper engine position control after turning on power

Hello. I’m working on stepper engine with driver A4988 and Arduino Nano. After turning off the power, the stepper stops in some position. After turning on the power, the stepper working from the position, where he stops, but I need him to return to some “zero” position and start stepping from there after turning on. Can you advise me how to set a “zero” position for him to start from, when he turns on the power?

You need a limit switch that is triggered by the motor when it gets to the ZERO position. Then in your setup() function you can use code something like this pseudo code

zeroSwitchState = digitalRead(zeroSwitchPin);
while (zeroSwitchState == HIGH) { // assumes LOW when switch is triggered
   // code to move one step towards switch
   zeroSwitchState = digitalRead(zeroSwitchPin);
}
motorPosition = 0;

...R
Stepper Motor Basics
Simple Stepper Code