Stepper Motor with Limit Switches

I have the code below which is my attempt to control a stepper motor direction that moves a carriage to the right and left. In addition there are limits at each end of the travel that when activated stops movement in the respected direction. My problem is that the motor works just fine in the left and right directions but I can only get one of the limit switches to work. I'd appreciate any suggestions.

``````int PUL = 7; //define Pulse pin
int DIR = 6; //define Direction pin
int ENA = 5; //define Enable Pin
int RGT = 3;
int LFT = 2;
int potPin = A5;
int count = 0;
int interval = 1000;
int val = 0;
int Spd = 500;

#define Limit01 8  // Pin 2 connected to Limit switch out
#define Limit02 9  // Pin 3 connected to Limit switch out

void setup() {
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
Serial.begin(9600);
}

void loop() {

if (digitalRead(LFT) == LOW) {  //  If joystick is moved Left
if (digitalRead(Limit01) == HIGH) {} // check if limit switch is activated

else {  //  if limit switch is not activated, move motor clockwise

digitalWrite(DIR, LOW);
digitalWrite(ENA, HIGH);
digitalWrite(PUL, HIGH);
delayMicroseconds(500);
digitalWrite(PUL, LOW);
delayMicroseconds(500);
}
}

if (digitalRead(RGT) == LOW) { // If joystick is moved right

if (digitalRead(Limit02) == HIGH) {} // check if limit switch is activated

else {  //  if limit switch is not activated, move motor counter clockwise

digitalWrite(DIR, HIGH);
digitalWrite(ENA, HIGH);
digitalWrite(PUL, HIGH);
delayMicroseconds(500);
digitalWrite(PUL, LOW);
delayMicroseconds(500);

}
}
}
``````
• You did not initialize the inputs. It looks like the switch has a pull-down, so no internal pull-up, but anyway initialize them at the beginning. Then.. Are you sure the switch is working? Did you try to use a sketch to read the switches state to check if they work? Commented Jun 6, 2017 at 9:54
• Can you supply a schematic of how the switches are wired? Have you measured the voltages on the limit switch inputs with the switch in the open and closed positions? Commented Jun 8, 2017 at 17:35

What I understood from your question is you want only one limit switch to control the stepper motor in both directions.So here is the code that might be helpful for you in that case:

``````int dirpin_T1 = 2;
int dirH_T1 = 3;
int steppin_T1 = 4;
int limit = 10; // ASSUME LIMIT-SWITCH IS PLACED AT RIGHT CORNER
void setup() {
pinMode(limit,INPUT);

pinMode(dirpin_T1, OUTPUT);
pinMode(dirH_T1, OUTPUT);
pinMode(steppin_T1, OUTPUT);

}

void loop() { //STEPPER TRAVELLING LEFT TO RIGHT

digitalWrite(dirpin_T1, LOW );
digitalWrite(dirH_T1, LOW);
do
{
digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(50);

}(digitalRead(limit)==HIGH); //AS LIMIT-SWITCH PRESSED MOTOR STOPS
delay(500);
//WHEN LIMIT-SWITCH IS LOW CHANGE THE DIRECTION OF MOTOR ROTATION
{
digitalWrite(dirpin_T1, LOW ); //CHANGE MOTOR DIRECTION (LOW TO HIGH)
digitalWrite(dirH_T1, HIGH);
for(int i=0; i<36000;i++)
{ //please try changing value of i as it depends on how much rotations you want your motor to step forward

digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(50);
}

}
}
``````

The simple logic would be to move the motor in one direction until limit-switch is HIGH. And to move it to the other direction when its LOW i.e already pressed.Correct me if I am wrong and try modifying the code as per this logic.However you need to change your code depending upon what type of driver you are using. Right now I am using TB6560 driver.You can use if loop instead of do while loop to make it easier to understand.

• What is `if(digitalRead(S01)==HIGH){}` supposed to do?
– user31481
Commented Nov 6, 2017 at 11:04
• its a limit switch Commented Nov 6, 2017 at 11:05
• Well, it looks like a statement that do nothing and has no side effects.
– user31481
Commented Nov 6, 2017 at 11:24
• thats a blank loop that will cause motor to stop when digitalRead(S01)==HIGH, otherwise motor will keep rotating Commented Nov 7, 2017 at 5:37
• No, I'm asking your the ìf` part. I've been programming since 1975, and I know what a comment is. I have great experience writing beautiful, nice comments. Everybody loves them. They are so beautiful people keeps saying, "please, stop writing so nicely. I can't take my eyes of yours wonderful comments."
– user31481
Commented Nov 7, 2017 at 9:36

Use while ( condition) { code to execute } making sure the code itself is "short" not to overrun the condition

If you use while construct instead of do / while you are checking the condition first before code execution. while( not at limit switch) move stepper one step Also the limit switch active state should be ground, not +V. Safety issue. If you accidentally ground the signal it will be valid, if the signal path is grounded already , by accident, putting +V to it won't be detected.