# Stepper motor defining start position and limit switches

<hello I have a isue with my stepper motor program . I am using a bounce program to drive my stepper motor with . But sometimes it doesnt do the same distance at the beginning how could I define a starting point from where it would go down 35000 steps and then would go up . So that when I take off the power I could just move it back to its initial position . Else could i maybe use 2 limit switches that would define the 2 points where it has to go back the other way

// Bounce.pde
// -- mode: C++ --
//
// Make a single stepper bounce from one limit to another
//

#include <AccelStepper.h>

// Define a stepper and the pins it will use
// AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper(1,7,6);

void setup()
{
// Change these to suit your stepper if you want
stepper.setMaxSpeed(1000);
stepper.setAcceleration(10);
stepper.moveTo(1000);
}

void loop()
{
// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());

``````stepper.run();
``````

}

Having a "homing" procedure to really know where things are is a typical process. You have at least one limit switch and in the setup you run towards the limit switch until you hit it. Then you know where you are and can start from there.

How could I program this / add this to my program ?

Depending on the stepper motor you can drive the motor to a mechanical stop , stalling the motor briefly to set your reference point .
Works with car instrument type steppers for example .

That maybe a problem. If you do so, the reference point is in the middle of your Movement. If you start the program, you do not know in which direction to go to the reference point. It is better to place the reference point at one end of the traverse path and bounce between 0 and the other end.

I must say I have no idea how I would change this to a position end point I never used arduino before

That's a fairly simple version of homing ( and it sets the reference point to the middle of your movement ). The limit switch must be positioned at the negative end.

``````// Bounce.ino
// -- mode: C++ --
//
// Make a single stepper bounce from one limit to another
//

#include <AccelStepper.h>

// Define a stepper and the pins it will use
// AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper(1,7,6);

const byte limitPin = A2;   // input pin for limit switch, change to your needs
// Switch must connect to GND if stepper reaches the switch.
void setup()
{
// Change these to suit your stepper if you want
pinMode( limitPin, INPUT_PULLUP );
stepper.setMaxSpeed(100);       // slow speed for homing
stepper.setAcceleration(100);
// start homing procedure
stepper.moveTo( -2000 );                // should be max possible movement towards the limit switch
while( digitalRead( limitPin ) == HIGH ) stepper.run(); // move until limit switch is reached
stepper.setCurrentPosition( -1000 );    // set current position as negative limit
// start normal move
stepper.setMaxSpeed(1000);
stepper.setAcceleration(10);
stepper.moveTo(1000);
}

void loop()
{
// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());

stepper.run();

}``````

how could we change the program to have a limit switch at the other side too as security and that it would go back to the first limit switch ?

May be you could give it a try? You have some example in the code you were given.

There seems to be a problem with the programming were it doesnt do the first part and just instantly goes into the loop / bounce version

How did you wire up the limit switch? The code's expecting to see LOW as an indication that the limit switch was pressed.

How did you connect your linit switch?
The limit switch must be connected between the pin and Gnd, and it mus be normally open. It must close, when the stepper reaches the switch.

We maybe found a solution we'll keep you up thx anyway

IT works thx

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.