Microswitch weird behaviour

Hello,
I want to use a limit switch with my Arduino uno. I connected it through an cnc shield at pin 12 to the Arduino. I use the C an NC pin at the limit switch. Now the problem: The switch only works if I touch or come close the the isolated cable which is used for the signal. I added a restistor (10kΩ) but that doesn´t work. Here is my code:

#include <Arduino.h>

#define LIMIT_SWITCH_PIN 11
 
void setup() {
  Serial.begin(9600);
  pinMode(LIMIT_SWITCH_PIN, INPUT);
}
 
void loop() {
  if (digitalRead(LIMIT_SWITCH_PIN) == HIGH)
  {
    Serial.println("Activated!");
  }
  else{
    Serial.println("Not activated.");
  }
  delay(100);
}

Why do you not using pulldown? Without it your switch pin is floating and it's state will depend on all electromagnetic noise around

You need to connect it to ground also. Otherwise the input pin is floating and will be affected by nearby objects.

How/where did you add the resistor?

Uno pins don't have built in pull-down resistors, only pull-up.

pin 12 ?

1 Like

Just a reminder of what others have said about limit switches: You want to use a pull-up resistor on the input pin and connect the switch using the C (common) and the NC (normally closed ) terminals to pull down. That way, when you poll the input pin, it should normally read LOW. If it reads HIGH there is a fault condition. Either the switch is activated or one of its wires has been severed.

You might want to reply to @finn433 rather than me!

You are correct. I thought I did that.

  1. I forgot to say that the other pin is indeed connected to ground.
  2. I meant pin 11
  3. The resistor is connected between the switch and pin 11
  4. I used pinmode(endstop, INPUT_PULLUP); but now the switch is constantly on HIGH and it´s doesn't affect by any presses.

Assuming you're using the NC terminal for input, then:

Assuming the switch common is connected to GND, you could keep the 10K series resistor as it will provide some input protection against any voltage spikes that may occur from the cable. I suggest adding a 1K pullup resistor to 5V at the switch end of the series resistor. This should provide enough wetting current for the microswitch. In this scenario, there will be about 5mA continuous current drain until the microswitch opens.

Assuming the switch common is connected to 5V, you could keep the 10K series resistor as it will provide some input protection against any voltage spikes that may occur from the cable. I suggest adding a 1K pulldown resistor to GND at the switch end of the series resistor. This should provide enough wetting current for the microswitch. In this scenario, there will also be about 5mA continuous current drain until the microswitch opens.

P.S. Should I have asked for a circuit diagram?

Thanks that worked for me. I could have attached a circuit diagram but it also worked without one :slight_smile:

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