Limit switch with Nema 17 and A4988

Hi , I have a trouble with 2 limit switch for nema 17 operation ( A4988 module ). I need motor change direction when it touch to each limit switch but it not working , anyone could help me about it , thank you so much

Note :

On limit switch1 ,

  • ON pin connect to digital 2
  • COM pin connect to GRN

On limit switch2 ,

  • ON pin connect to digital 3
  • COM pin connect to GRN
const int dirPin = 5;
const int stepPin = 4;
const int lmswitch1 = 2;
const int lmswitch2 = 3;
const int enable = 8;
const int stepsPerRevolution = 1000;

void setup()
{
  // Declare pins as Outputs
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(lmswitch1 , INPUT);
  pinMode(lmswitch2 , INPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable,LOW);
  digitalWrite(dirPin, LOW);
}
void loop()
{
  int ltm1 = digitalRead(lmswitch1);
  int ltm2 = digitalRead(lmswitch2);

  if ((ltm1 == HIGH) && (ltm2 == LOW)) {
    motorStep(1000);
  } else if ((ltm1 == LOW) && (ltm2 == LOW)) {
    digitalWrite(dirPin, HIGH);
    delay(2000);
  } else if ((ltm1 == HIGH) && (ltm2 == HIGH)) {
    motorStep(1000);
  } else if ((ltm1 == LOW) && (ltm2 == HIGH)) {
    digitalWrite(dirPin, LOW);
    delay(2000);
  }
  if ( (digitalRead(dirPin) == HIGH) ) {
    motorStep(1000);
    digitalWrite(dirPin, HIGH);

  }
  else if ( (digitalRead(dirPin) == LOW) ) {
    motorStep(1000);
    digitalWrite(dirPin, LOW);

  }
}

void motorStep( int MAX) {
  for (int x = 0; x < MAX; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(200);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(200);
  }
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

Grrrrrr

This makes no sense to me - firstly I've no clue what sense the limit switches are (active high or active low) so its unclear what's what.

Secondly you are conflating the two limit switches with each other which is odd - you are either at one limit or at the other, you can't be at both!! I'd expect two clauses, one for limit1, one for limit2. If neither is active nothing should happen, yet this code always does something no matter what.

1 Like

I think i don't understand about limit switch.
On limit switch ,

  • ON pin connect to digital 2
  • COM pin connect to GRN

and i try this code to test limit switch but i don't understand why it always show "1" on Serial Monitor although i press into limit switch. So this limit switch cannot change state? , anyone could show me why ?

const int lmswitch1 = 2;
void setup()
{
pinMode(lmswitch1 , INPUT);
}
void loop()
{
  if(digitalRead(lmswitch1==LOW)){
    Serial.println("1");
    delay(1000);
  }else{
    Serial.println("0");
    delay(1000);
  }
}

if(digitalRead(lmswitch1==LOW))

I think you meant

if(digitalRead(lmswitch1) ==LOW)

Hi,
If your limit swtiches are connected between the input pins and gnd, do you have pullup resistors for each of the switches?
This is to pull the respective input high when the switch is open.
OR.
You can turn the internal pullups by declaring your pinMode this way.

pinMode(lmswitch1 , INPUT_PULLUP);
pinMode(lmswitch2 , INPUT_PULLUP);

Can you post a copy of your circuit please?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

const int dirPin1 = 5;
const int stepPin1 = 4;
const int dirPin2 = 7;
const int stepPin2 = 6;
const int lmswitch = 2;
const int spr = 10000;


void setup()
{
  // Declare pins as Outputs
  Serial.begin(9600);
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(lmswitch , INPUT);
  digitalWrite(dirPin1, HIGH);

}
void loop()
{
  if (digitalRead(lmswitch) != LOW){
  StepRight();
  StepLeft();
  }

}

void StepRight()
{
  while (digitalRead(lmswitch) != LOW)     
  {
    digitalWrite(dirPin1, HIGH); //pin 8, CW

    for (int x = 1; x < 10000; x++)     
    {
      digitalWrite(stepPin1, HIGH);        
      delayMicroseconds(2000);     
      digitalWrite(stepPin1, LOW);         
      delayMicroseconds(2000);     
    }
  }
}

void StepLeft()
{
  while (digitalRead(lmswitch) != LOW)   
  {
    digitalWrite(dirPin1, LOW); 

    for (int x = 1; x < 10000; x++)     
    {
      digitalWrite(stepPin1, HIGH);       
      delayMicroseconds(2000);     
      digitalWrite(stepPin1, LOW);         
      delayMicroseconds(2000);    
    }
  }
}

it still not working , it always run never change direction when i push to limit switch

Hi,
Thanks for the image, BUT we need a circuit diagram, not a Fritzy image.
Please post a hand drawn diagram with what you are using for a power supply, not a 9V smokedetector battery.

Why have you got BOTH limit switches wired to the SAME pin?

Wire each switch to its own input pin like you had before.
CHANGE;

pinMode(lmswitch1 , INPUT);
pinMode(lmswitch2 , INPUT);

TO:

pinMode(lmswitch1 , INPUT_PULLUP);
pinMode(lmswitch2 , INPUT_PULLUP);

Tom... :smiley: :+1: :coffee: :australia:

Thank so much , i do done with this code

const int dirPin1 = 5;
const int stepPin1 = 4;
const int lmswitch1 = 2;
const int lmswitch2 = 3;
const int enPin = 9;


void setup()
{
  // Declare pins as Outputs
  Serial.begin(9600);
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(lmswitch1 , INPUT_PULLUP);
  pinMode(lmswitch2 , INPUT_PULLUP);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  digitalWrite(dirPin1, HIGH);
}
void loop()
{
  int fwdend = digitalRead(lmswitch1);
  int backend = digitalRead(lmswitch2);
  if (fwdend == HIGH ) {
    digitalWrite(stepPin1 , HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1 , LOW);
    delayMicroseconds(2000);
  } else if (fwdend == LOW ) {
    digitalWrite(dirPin1, LOW);
  }
  if ((backend == HIGH ) && (digitalRead(dirPin1) == LOW)) {
    digitalWrite(stepPin1 , HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1 , LOW);
    delayMicroseconds(2000);
  } else if ((backend == LOW ) && (digitalRead(dirPin1) == LOW)) {
    digitalWrite(dirPin1, HIGH);
  }
}


If the return value of a digitalRead isn't HIGH, there's no need to test it again to see if it is LOW :wink:

1 Like

Anyone know how to stop stepper motor when it running ? , use module A4988

Thank you

Stop writing to the step pin

you mean :slight_smile:

        for (int x = 0; x < 2 ; x++) {
          digitalWrite(stepPin2, HIGH);
          delayMicroseconds(6000);
          digitalWrite(stepPin2, LOW);
          delayMicroseconds(6000);
        }

Why have you introduced a for loop when there was not one in your previous sketch ? In any case, the code snippet is still writing to the step pin

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