Need Stepper motor to stop and rotate with single limit switch.

My setup has Stepper 23, limit switch at a specific position. I want to motor start and stop at the limit switch in loop. Clockwise every time. is it possible, with a single limit switch?

#define STEP 9
#define DIR 8
int home_switch = 12;
int pd = 400;
void setup() {
  
  // Sets the two pins as Outputs
  pinMode(STEP,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(home_switch, INPUT_PULLUP);

} 

void loop() {
  if(!digitalRead(home_switch) == LOW)
    {
   digitalWrite(DIR, HIGH);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(pd);
    
  } else {
    digitalWrite(STEP, LOW);
  }
}

I think this is what you want but please correct me if I am wrong ...

You want the stepper motor always to run in the same direction and every time it completes a revolution and triggers the limit switch you want it to stop. You have not told us what will cause it to restart.

One simple way to implement that is to check the switch after every step and if the switch is triggered set a variable (let's call it motorMayRun) to false. Something like this pseudo code

if (motorMayRun == true) 
  // move one step
  limitSwitchStatus = digitalRead(limitSwicthPin);
  if (limitSwitchStatus == LOW) // assumes LOW when triggered
      motorMayRun = false

When restarting the motor you might need to make it move a few steps while ignoring the limit switch until such time as the switch is released. You could do that by comparing the previous state of the switch with the current state

...R

You want the stepper motor always to run in the same direction and every time it completes a revolution and triggers the limit switch you want it to stop. You have not told us what will cause it to restart.

This is the single switch to start and stop the motor.
Please help me out

virencq:
This is the single switch to start and stop the motor.
Please help me out

Have you carefully considered the code I suggested?

Assuming that it stops the motor correctly, what will decide whether, or when, the motor should re-start moving?

...R

Can we implement another limit switch to achieve this?

virencq:
Can we implement another limit switch to achieve this?

Not until you explain what you mean by "this".

This is the 3rd time I have asked you to explain what are the circumstances in which the motor should restart. I won't ask a 4th time.

...R

Setup has

  1. One stepper
  2. 2 relays
  3. 1 Limit switch (Attached to motor shaft in such a way, after each revolution it activates the limit switch and stops.

What I want is motor rotates its one revolution and stops at home position. Relay 1 and relay 2 activates. and in loop again and again.

As you suggested if motor forwards few step after home position by the program not manually. then it will suffice my requirements.

alternately if you suggest another limit switch to add for motor start in loop. please guide.

#define STEP 9
#define DIR 8
#define REL1 4
#define REL2 5
int home_switch = 12;
int pd = 400;
void setup() {
 
  // Sets the two pins as Outputs
  pinMode(STEP,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(REL1,OUTPUT);
  pinMode(REL2,OUTPUT);

  pinMode(home_switch, INPUT_PULLUP);

}

void loop() {
  if(!digitalRead(home_switch) == LOW)
    {
   digitalWrite(DIR, HIGH);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(pd);
   
  } else {
    digitalWrite(STEP, LOW);
  }

{

digitalWrite(REL1,HIGH);
  delay(200);
  digitalWrite(REL1,LOW);
  delay(200);}
  {digitalWrite(REL2,HIGH);
  delay(600);
  digitalWrite(REL2,LOW);
  delay(400);}



}

virencq:
What I want is motor rotates its one revolution and stops at home position. Relay 1 and relay 2 activates. and in loop again and again.

How long is the motor supposed to stop? Is it the passage of time that causes it to restart?

Something must happen to cause the motor to restart because if it restarts the instant after it stops it won’t have stopped. The whole movement of a stepper motor is a series of stops and starts. That’s why it is called a stepper motor.

On the other hand if you don’t actually want the motor stop at the switch but you just want the triggering of the switch to cause the relays to operate that is also perfectly possible.

Reply #6 is the first place where you mention relays. Please tell us what the whole project is. It will be much easier to give useful advice when we understand the context of your questions. At the moment this is a typical XY Problem

…R