Go Down

Topic: Homing stepper with a swtich (Read 845 times) previous topic - next topic

I'm worink on a project where I'm gonna build a box that is sliding on a linear rail (very much like a CNC) using a stepper motor. I'm using an meny to determine the actions of the stepper motor, but i wont the stepper to "home" itself before it move to its first stop.

So my question is: Is it posible to make the stepper move until it triggers a switch before starting its actions?

Sorry if the question is bad formulated. It is to prevent the motor to start from a wrong point incase of the power shuts down in the middle of a previous action.

I'm using the library "accelstepper" to control the stepper. 


Sure, limit switches are used all the time for this. Look at "micro switches".

Simply move the motor a little bit (In case it is already at the limit) and then slowly move it back while checking the state of the switch.


Rig up a limit switch and at startup, move the stepper until it triggers it. Look at the reprap project - there are limit switches used there for the same thing.


You could use 2x micro switches to speed up reference point homing. Return home at full speed until first micro switch is closed then slow until second one closes.

Nov 27, 2012, 06:25 pm Last Edit: Nov 27, 2012, 06:27 pm by ekarlsson25 Reason: 1
Oh many answers!  :)
But how am I going to prgoram it? Is there a tutorial on those RepRap machines?


Oh many answers!  :)
But how am I going to prgoram it? Is there a tutorial on those RepRap machines?

In setup...
Is limit switch active?
    yes: step out x number of steps
    is limit switch active?
        yes: Error, switch broken. stop.
while limit switch not active
    step in 1 step
your home.

I can't work it out..  :~
I'm pretty new to programing.


I can't work it out..  smiley-confuse
I'm pretty new to programing.

What have you tried? Try as I might, I can't find your code anywhere in this thread.

I haven't got the rail atm, but it seems like it should work anyways, like if the button is not active the motor moves backwards. But when the button gets pressed the code from the menu starts.

I've just tried to modify the AccelStepper's example "blocker" with a funktion that reads the buttonstate. But i don't really know how i should proceed, or if I'm doing it right from the start?


But i don't really know how i should proceed, or if I'm doing it right from the start?

Move over a little. Your blocking our view of your code.

A little more. More. Careful you don't fall off of the chair. A little more.

Hey, I said be careful. Now, get up and post your code.

Nov 28, 2012, 11:47 am Last Edit: Nov 28, 2012, 12:36 pm by ekarlsson25 Reason: 1
Here is the code.  I've gotten it so far that it needs a press of the button to start activate the program. But i cant figure out how to make it move backwards until the button in pressed.

Code: [Select]
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 49, 47);

int switchPin = 33;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);

boolean debounce(boolean last)
  boolean current = digitalRead(switchPin);
  if (last != current)
    current = digitalRead(switchPin);
  return current;

void loop()
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
    ledOn = !ledOn;
  lastButton = currentButton;

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131