# Using EEPROM to set a home position for a stepper motor

Hey guys, i’m fairly new to Arduino projects and I was trying to set a home position for a stepper using EEPROM. My idea was to have a ‘for’ loop and to have the stepper move 45 steps one way, then 45 the other. Below is the code for that so you know what I mean.

void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 45; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin,LOW);
for(int x = 0; x < 45; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}

Hey guys, i'm fairly new to Arduino projects and I was trying to set a home position for a stepper using EEPROM. My idea was to have a 'for' loop and to have the stepper move 45 steps one way, then 45 the other. I use the value 'x' for the steps. I was wondering if I could store the number of steps left in 'x' in EEPROM before I cut the power so that on start up I can just get the number of steps the motor had to move before its task was complete then just make it move that many steps in the direction it needs. My question is - Is this possible and if not, is there any other way to set a home position without using hardware?

Hazmat:
I was wondering if I could store the number of steps left in 'x' in EEPROM before I cut the power so that on start up I can just get the number of steps the motor had to move before its task was complete then just make it move that many steps in the direction it needs. My question is - Is this possible and if not,

How often will this happen? EEPROM has many, but limited, lifetime writes.

And what if power is lost before this is written? you still need a way to re-center.

hawk

I found an example of the basics for a solution

And there is more info if you know what to search for; e.g. Saving to EEPROM on power-out - General Electronics - Arduino Forum

You can never be sure that the motor has not been moved while the Arduino was powered off.

If you have a hard stop in your system, just drive 90 steps towards that stop on every startup.

If you have a "home switch" in the middle then drive 45 steps left and 90 steps right, stopping as soon as the switch is triggered. If the switch is not triggered by that sequence then step the motor back and forth like a sick animal stuck in a trap and squealing for help.