How To Wire Limit Switches

Dear Arduino Community,

I want to add limiting switches (like this one) to my project.

The problem is, I don't know how to connect them to my Arduino Mega, and I don't know how to programe everything up.

Here's what I've done for the moment (please see the picture in the attachments for a better understanding): I connected pin 22 to a 10K ohms resistor (which is in serial with the limiting switch, and this one is connected to the GND of arduino), and to the 5V output from Arduino.

What I want to do is to find a way that everytime the Limiting Switch is pressed, one of the digital pins changes its value to LOW or HIGH, so that I can use this to stop the stepper motors.

To see if I could make the limiting switch work, I wrote a code. This code works with a LCD Shield that I bought. In this code, my goal is simple: every time that I press the limiting switch, I want to print something in the LCD Screen, saying "the PIN is HIGH", or "the PIN is LOW".

Here is the code that I wrote for this:

void menuItem8() { // Function executes when you select the 8th item from main menu
  int activeButton = 0;
  int val = 0;
  digitalWrite (22, LOW);
  
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Test");

  while (activeButton == 0) {
    int button;
    readKey = analogRead(0);
    if (readKey < 790) {
      delay(100);
      readKey = analogRead(0);
    }
    val = digitalRead(22);
    
    if (val == HIGH) {
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("HIGH");
      delay (1000);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("STILL");
      delay (1000);
    }
    
    if (val == LOW) {
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("BUTTON");
      delay (1000);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("PRESSED");
      delay (1000);
    }   
    button = evaluateButton(readKey);
    switch (button) {
      case 4:  // This case will execute if the "back" button is pressed
        button = 0;
        activeButton = 1;
        break;
    }
  }
}

Anyone have any idea on how I should properly wire the Limiting Switch and/or Program the Arduino so I can change the value of a digital pin and use it to stop some stepper motors?

Thank you, and if I didn't make myself clear, please let me know and I'll try to explain it better.

Joao

The problem is, I don't know how to connect them to my Arduino Mega

Connect one leg to ground. It doesn't matter which one.

Connect the other leg to a digital pin. It doesn't matter which one, as long as the pin is not being used for something else.

Set the mode of the pin to INPUT_PULLUP.

When you read the switch pin, HIGH will mean not pressed, and LOW will mean pressed.

PaulS:
Connect one leg to ground. It doesn't matter which one.

Connect the other leg to a digital pin. It doesn't matter which one, as long as the pin is not being used for something else.

The limit switches I've seen are usually SPDT, so it does matter which pins you pick. One of them needs to be the common pin.

OP posted a link to a SPDT switch with wires already attached to two (of the three) tabs of the switch. It does not matter which of the two wires goes to which pin on the Arduino. It does, as you say, matter which tabs on the switch the wires go to.

I overlooked the link.

When you read the switch pin, HIGH will mean not pressed, and LOW will mean pressed.

And you overlooked that the wires are connected to the N.C. contact. :stuck_out_tongue:

Hey guys,

I have no idea what you are talking about, but the PaulIS post gave me the anwser I needed.

Thank you both,

Joao

And you overlooked that the wires are connected to the N.C. contact.

All that using the N.C. contact means, instead of the N.O. contact, is that HIGH and LOW change meanings.

Josdc:
Hey guys,

I have no idea what you are talking about, but the PaulIS post gave me the anwser I needed.

Thank you both,

Joao

Partially. Be sure to take note of his correction above. Because you connected to the normally closed contact, unpressed will be LOW and pressed will be HIGH.