Go Down

Topic: CNC shield not reading the value of limit switch (Read 176 times) previous topic - next topic

jinmax100

Jun 23, 2019, 08:04 am Last Edit: Jun 23, 2019, 11:33 am by jinmax100
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");
   
   }
 
 
 }

groundFungus

#1
Jun 23, 2019, 12:24 pm Last Edit: Jun 23, 2019, 05:10 pm by groundFungus
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 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");
   }
}

Go Up