Why is my limit switch constantly flickering between HIGH and LOW?

Hi everyone, I am stumped with this one!

Problem: Arduino is reading Digital Pin 10 as HIGH LOW HIGH LOW HIGH LOW HIGH LOW forever while the limit switch is open (which is bad)

When I push it (limit switch closed), it reads LOW (which is good).

Things I have tried so far that didn't solve the problem:

  • Tried 3 different arduino boards
  • 3 different limit switches
  • Reduced wire length from 6 feet to about 3 feet
  • Tried 10k external pullup resistor, tried 1k external pullup resistor
  • Tried capacitor and pullup resistor combo as shown here (though I skipped the LED and 1k resistor)
  • Tried pins other than pin 10 and tried different GND pin on Arduino
  • Tried different USB cables and different USB port on USB hub and PC
  • Ditched the limit switch (part# here) all together and just used the raw cables and opened and closed them (e.g. didn't touch the leads together, then touched the leads together)

PCB Diagram and Serial Monitor:


Code:

//End Stop Test
#define SIGNAL_PIN 10

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  pinMode(SIGNAL_PIN, INPUT_PULLUP); 
  pinMode(LED_BUILTIN,OUTPUT);

}

void loop() {

  if(digitalRead(SIGNAL_PIN) == HIGH)
  {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("HIGH");
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else if(digitalRead(SIGNAL_PIN) == LOW);
  {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("LOW");   
    digitalWrite(LED_BUILTIN, LOW);
  }


}

Any thoughts? Thank you!

You mention that you push the limit switch. Please identify this limit switch. Most limit switches are micro-switches. Do you have screw or soldered connections for the wires? Are you using a breadboard some where in the circuit?

Let’s see a good image of your wiring.

Thank you so much for the replies everyone! Sorry for not posting an image of my actual setup (doh!)

Below are pictures and a video. I taped down the wires just now so it's easier to see everything in the picture.

To answer a few questions:

  • Limit switch I'm using is here .
    This one has 3 pins, and the way it work is: S and V are connected while the switch is in its default state. (S and V are normally closed).
    When the switch is pushed, S and V are disconnected and instead S and G pins become connected.

  • I am using jumper wires, not soldered connections.

  • No breadboard (I tried simplifying my circuit because this same issue was occurring in a more complex setup), pictures and video below.

Video demonstration is here



What does the white pin go to? It should go to 5 volts shouldn't it? Otherwise the Arduino is reading a floating value.

lol I'm going to smack myself if it's that simple...is it though?... :sweat_smile: .. I can try it. But I thought the whole point of the INPUT_PULLUP in the code is to activate the Arduino's internal pullup resistor, which already connects it to the internal 5V supply line.

I thought that's why the left most diagram on this page works:
File:Reprap endstops schem mech.png - RepRap File:Reprap endstops schem mech.png - RepRap

It's at least something to try! :wink:

It worked when I had this issue without input-pullup, and a custom built module...

Haha aw man, so connecting the white pin to the 5V pin on the Arduino didn't do anything (using the exact same code).

Then I thought, what if I call it pinMode(SIGNAL_PIN, INPUT) instead of INPUT_PULLUP, and I connected pin 10 directly to the 5V line with a jumper cable (completely eliminated the limit switch from the circuit). It still has the same fluttering/flickering between HIGH and LOW.

This seems very weird...

Is this your setup?

You could try that anyway.

WAIT...
On closer inspection, it looks like your little button module doesn't actually have resistors soldered to it...
Is that accurate?

#$#$#$#$#$#$*$ oh my lord it was not the hardware...

It was a dumb SEMICOLON in the wrong spot of the code AHHHHHHHHHHHHHHHHHHHH!!!!!

dumb mistake

I wish the compiler would throw a flag but I guess it just let's that kind of stuff fly...so it seems like the "else if" line was completely ignored and it just kept following the instructions directly below it regardless of the condition of the limit switch....

!!!!!!

I really appreciate everyone's help in trying to diagnose the issue!

1 Like

NO! You still have problems using that type of switch as a limit switch. There is no magical ZERO time for the switch to make it's move from one position to the other. During that very short interval, your code may be testing the switch several times and what will it see? NOTHING connected at either position of the switch.

A momentary pushbutton would be much better.

I doesn't matter.

1 Like

Thank you Paul_KD7HB, I think that makes sense.
But what is the solution then?

My understanding is that these mechanical limit switches are fairly common in CNC/robotic type applications and that the limitation you mentioned is acceptable? Or maybe I misunderstood.

Either way, it seems like you have a better way, I would love to better understand it!

That's the spirit!

1 Like

Any SPST type switch will work when one side of the switch will ALWAYS be connected to the system ground. Look for a microswitch to fit your project.

1 Like

Thank you! I will look into it!

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