Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: jinmax100 on Jun 23, 2019, 08:04 am

Title: CNC shield not reading the value of limit switch
Post by: jinmax100 on Jun 23, 2019, 08:04 am
Hello guys, I am new to the forum. I am trying to connect NIMA 17 stepper motor with Drv8825 motor driver. I am trying to connect limit switches to end stops of CNC shield. Currently in X+ end stop (I just want one motor to rotate when limit switch is pressed). I want to do this without using GRBL and just from Arduino IDE.

The problem I am currently facing is that X+ pin in CNC shield isn't reading the value of the switch. My CNC shield is mounted over Arduino UNO and as per schematic limit X(pin 9) in arduino UNO corresponds to X+ and X- pins in CNC shield. In my code I have illustrated that when the switch goes low only then the motor starts rotating but when the switch goes high motor should do nothing. My limit switch has its connection in NO terminal(connected to X+ in CNC shield) and common terminal(connected to GND in CNC shield). But the motor is constantly rotating even when switch goes high. Where have I gone wrong. Please help me.

Is it not possible to digitalRead() in end stop pins? Logically it should, right? Afterall endstops are connected to pin 9, 10 and 11. I have posted my code below.


#CODE


#define EN          8 
#define X_DIR     5
#define X_STP     2
#define limitX      9   //Limit pins: X->9 , Y->10 , Z->11

void setup() {

   Serial.begin(9600);

   pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
   
   pinMode(limitX, INPUT_PULLUP);
   
   pinMode(EN, OUTPUT);

}

void loop() {
 
 if(digitalRead(limitX==LOW))
 {
     digitalWrite(X_STP,HIGH);

     delayMicroseconds(500);

     digitalWrite(X_STP,LOW);

     delayMicroseconds(500);

 }

 else
 {
     Serial.println("Switch has been pressed");
   
   }
 
 
 }
Title: Re: CNC shield not reading the value of limit switch
Post by: groundFungus on Jun 23, 2019, 12:24 pm
Code: [Select]
if(digitalRead(limitX==LOW))
Should be
Code: [Select]
if(digitalRead(limitX)==LOW)

You want to read the state of X+ pin not the pin number whose result is the comparison (limitX==LOW).

Read the how to use this forum-please read sticky (https://forum.arduino.cc/index.php/topic,148850.0.html) to see how to, properly, post code.  Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Code: [Select]
#define EN          8
#define X_DIR     5
#define X_STP     2
#define limitX      9   //Limit pins: X->9 , Y->10 , Z->11

void setup()
{
   Serial.begin(9600);
   pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
   pinMode(limitX, INPUT_PULLUP);
   pinMode(EN, OUTPUT);
}

void loop()
{
   if (digitalRead(limitX) == LOW)
   {
      digitalWrite(X_STP, HIGH);
      delayMicroseconds(500);
      digitalWrite(X_STP, LOW);
      delayMicroseconds(500);
   }
   else
   {
      Serial.println("Switch has been pressed");
   }
}