Move stepper motor to defined angle

Hi,

I am using the stepper library to control my 28byj-48 stepper motor. It's ok an works fine. But I have a question: everytime I start my arduino the motor holds the position from the last time I played with my arduino and if I tell it to spin 20 degree it will spin 20 degrees from the position it started. So I would like that my arduino firstly would turn the stepper motor to a predefined position and after that start runing my code.

So how do I tell the stepper motor to first fo to the 0 degree position instead of start at the last used position?

The only way to "know" where a stepper is positioned is to use a HOME or ZERO switch and drive it to that position when the Arduino starts.

A second best alternative is to save the current position into EEPROM memory before the Arduino is switched off. But that has many problems. The EEPROM memory has a limited life and there would be no means to know if someone rotated the motor while the Arduino is off.

...R
Stepper Motor Basics

Thank you so much! I couldnd believe that it's not possible to zero the stepper... many 2D printer use stepper motor, so you are saying it's not possible to set the initial position to a known position without saving the last position in the EPPROM? If I use a HOME switch, as you said, how would it work? Using the stepper library how would I make the motor spin to a defined position before I start my routine?

Have it move in one direction until a limit switch is pressed - then you know where it is.

@CrossRoads thanks but I didnt understand your idea. You are saying that I need to make a code that spins the motor as soon as the arduni is turned on and when it reaches a certain position I press a button and it holds there? But it's not precise... I mean, I will use this motor in a plotclock so a precise angle is important, so how can I make sure both motors have the same initial angle?

batata004:
You are saying that I need to make a code that spins the motor as soon as the arduni is turned on and when it reaches a certain position I press a button and it holds there?

Yes. But YOU don't need to press any button. When the Arduino detects that the limit switch has been triggered it will know where the motor is. Your code should be something like this

read switch
while switch not pressed
   move one step towards switch
   read switch

...R

Nice really nice! :slight_smile: But I still dont get what is the "switch". Is it a button that I should press? Cause look, this stepper motor gives me a good precision of about 0.08 degree and I would like to sync 2 stepper motor so they spin at the same precise position. I have still no idea how to make sure both motors start at the same exact angle with you "switch" method.

Sorry but I think I am not getting something right you are telling me... could you please explain me just a little more?

batata004:
But I still dont get what is the "switch". Is it a button that I should press?

It is a switch that is activated by the movement of the machine when it gets to the ZERO position. Think of the switch inside your fridge that turns off the light when you close the door.

You could use a mechanical micro-switch or an optical detector. You can easily position the motor to the accuracy of a single step.

You now say you have two motors. What does each of them do? You need to tell us more about your project if you want useful advice.

...R

This is a method that is used in your desktop printer. If you turn your printer off and then physically move the print head to a position away from where it is, then turn the printer on, it will move back to where it was when you turned it off. It moves back until it activates some type of 'switch', which could be an electro-mechanical device, an opto interrupter or other. Now the print head is 'home', a reference point from which how far the print head must move to a desired position is determined. - Scotty

tHANK YOU now I understood! I will use an infrared sensor to detect an led and mark the home position. I am building a plotclock and it's really necessary that both motors are very well aligned.