Stepper motor whith limit switch

Hello everyone, I'm trying to get a stepper motor to spin until the moment the limit switch is pressed.
The goal is that once the limit switch is pressed the engine will not return to work.

Thanks to everyone for the help


#include <Stepper.h>
const int limit_s_pin = 7;
const int stepsPerRevolution = 200;
int i = 1 ;  
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() 
{
  pinMode(limit_s_pin,INPUT);
  myStepper.setSpeed(60);
}

void loop() {
  
 while(digitalRead(limit_s_pin)== HIGH && i == 1)
  {
   myStepper.step(3);
  }
    i = 2;
}

Your topic was MOVED to its current forum category as it is more suitable than the original

How is the limit switch wired ?

switches are typically connected between a pin and ground, the pin configured as INPUT_PULLUP so that it is pulled HIGH and when the switch is active, it pulls the pin LOW

why not simply

void loop() {
    if (HIGH == digitalRead(limit_s_pin))
        myStepper.step(3);
}

but since the starting position of a stepper motor is typically unknown at startup, why not use the switch as a known calibration point at one extreme and rely on the # of steps to keep the motor within limits.

positioning the limit switch at one extreme allows the motor to always be moved in one direction at startup to locate calibrate

normaly close ==> gnd
com==>pin 7

I want the engine to stop as soon as the limit switch is pressed and not run again

Then you have to use INPUT_PULLUP or use an external pull up resistor. As you have been told. Wiring to the NC contact on the switch will make the input HIGH when the limit is exceeded. If you want to run it just once, put it in setup():

#include <Stepper.h>
const int limit_s_pin = 7;
const int stepsPerRevolution = 200;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
  pinMode(limit_s_pin, INPUT);
  myStepper.setSpeed(60);

  while (digitalRead(limit_s_pin) == LOW)
  {
    myStepper.step(3);
  }
}

void loop() {
}
1 Like

switches are typically wired normally open

for your case, just change the HIGH to LOW

    if (LOW== digitalRead(limit_s_pin))

If it has to happen in loop(), the "run once" logic is usually:

#include <Stepper.h>
const int limit_s_pin = 7;
const int stepsPerRevolution = 200;
bool running = true ;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);

void setup()
{
  pinMode(limit_s_pin, INPUT_PULLUP);
  myStepper.setSpeed(60);
}

void loop() {
  if (running == true)
  {
    if (digitalRead(limit_s_pin) == LOW)
    {
      myStepper.step(3);
    }
    else
    {
      running = false;
    }
  }
}

Notice the use of boolean variable instead of an anonymously named int, to denote the state.

why?
simply not step when the limit switch is active

Because he said, "and not run again". If it's a homing setup, that's a good idea. Subsequent code should run in the loop() function without the homing happening every time the limit switch is inactive. I'm trying to anticipate an actual use case where the stepper performs some kind of work besides just homing. Also demonstrate the basic framework of "run once in loop()".

To really drill it in, I should have called the variable "homing" instead of "running".

??

i try to answer the question directly. i think it's less confusing to anticipate fewer issues without explanation

i by "homing" you mean a calibration point as i suggested, wouldn't that be a process in setup() that occurs once and resets a step-count variable? for such a thing, why would there need to be a "run" variable?

My thought, you might actually wish to home the device without power cycling the system. Perhaps the question would not have arisen if the OP had presented an overview of the project right in the first post, revealing the desired operational states.

We don't really know if this is a real project, or today's lab handout or homework.

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