Position validation after power lose for a stepper motor

Hello everyone,
I have an idea for a device that basically uses a stepper motor and a belt to pivot another device back and forth 180 degrees. So a simple back and forth motion for the final part. Now I can make the stepper do what I want, however, if the power is lost upon power up the program just start from whatever position it happens to be in at and then executes the instructions. How can I guard against this action?
As an example, if you were looking at a clock hand, and the stepper make the clock hand go from 9 to 3 clockwise, then back to 9 counter clock wise. All is well till there is a power interruption, lets say that the power was lost and the hand was at 12noon, upon start up the program would then move the hand from 12 to 6 clockwise and then back to 12 counter clock wise.
I would like the Arduino to be able to always be sweeping the hand between 9 and 3 and then back. I was thinking of a hall effect switches as this is space confined and is a not the nicest of environments for mechanical switches. If this is an option how would I tell the program to basically "move the stepper in a clockwise (or counter clock wise) till you find the hall effect switch, then at that point I would have a reference start point for the program.

Thanks in advance for any guidance, ideas

Any ideas on how to accomplish this?

All systems that use a stepping motor must have at least one and preferably two physical limit switches.
These can be of many different types. A popular one is an opto slot there are lots of them.
You have something that breaks the beam on the bit that is moving.
Other switches are reflective opto, hall effect or mechanical.

Implementation is simple, just pulse the stepper in the right direction until you see the switch triggered.

Thank you very much Mike,
Might you be able to point me in the direction of a code example of this process? I take it this "bumping in one direction" would be placed in the setup portion of the program then the loop can take over. But I would be appreciative of an example if you wouldn't mind.

But I would be appreciative of an example if you wouldn't mind.

It is almost trivial. Assume you have a function step() that gives one step from the motor then:-

while(digitalRead(limitPin) != HIGH) step();

Assuming that reading a limit pin returns a high when it is triggered.

Using limit switches assumes it's OK for the hand (to use your example) to go back to 9 o'clock every time the Arduino resets/restarts.

If, after a power interruption, you want the process to take up where it left off (at 1 o'clock, say) you will need a very different approach - possibly by saving the current stepper position in non-volatile memory or having some independent system that the Arduino can use to determine the position of the hand.

A simpler solution may be a backup battery.


Another possibility is an absolute position shaft encoder. There are several types, for example optical encoders using a Gray coded disk, or magnetic. Typical resolution is 256 or 1024 parts in 360 degrees. They can be expensive, but surplus electronics outlets sometimes have bargains. Here is an example of a magnetic encoder: http://www.usdigital.com/products/encoders/absolute/rotary/shaft/ma3

Here is an example of a magnetic encoder

Or just get the chip a AS5040