Logo

Interfacing Limit Switch with Arduino

Limit Switch Module

A Limit Switch is a mechanical device used to detect the presence or absence of an object, or its position. Commonly used in CNC machines, elevators, and automation systems, it helps control movement by acting as a physical trigger.

Working Principle of Limit Switch

The Limit Switch works through a mechanical actuator. When the actuator is pressed or released, it either opens or closes an internal electrical connection, sending a HIGH or LOW signal to the microcontroller.

Types of Limit Switches

Normally Open (NO)

  • No current flows in default state.
  • When pressed, circuit closes and allows current flow.
  • Arduino reads a LOW to HIGH transition.

Normally Closed (NC)

  • Current flows in default state.
  • When pressed, circuit opens and breaks the connection.
  • Arduino reads a HIGH to LOW transition.

Requirements

1. Arduino

2. Limit Switch Module

3. 10K ohm resistor (optional, for pull-down)

4. Jumper wires

Pin Configuration of Limit Switch

Limit Switch Terminals

  • COM (Common): Connect to ground or signal input.
  • NO (Normally Open): Connect to Arduino digital pin.
  • NC (Normally Closed): Optional use, connects to Arduino if needed for specific logic.

Wiring the Limit Switch to Arduino

To wire a Limit Switch to Arduino, connect the COM terminal to GND and the NO terminal to a digital input pin (e.g., D2). Optionally, use a pull-up resistor or enable Arduino's internal pull-up resistor in code to ensure stable readings.

Algorithm

  1. Initialize Components

    • Connect the COM terminal of the Limit Switch to GND on the Arduino.
    • Connect the NO terminal to a digital input pin, like D2.
  2. Write the Code

    • Set the input pin as INPUT_PULLUP in the setup() function.
    • Use digitalRead() in the loop() function to check the switch state.
    • Trigger actions based on whether the switch is pressed or released.
  3. Respond to Input

    • Print switch status to the serial monitor.
    • Use the switch input to stop motors, activate alarms, or log events.
  4. Test the Project

    • Upload the code to Arduino.
    • Manually press and release the switch.
    • Observe the response in the serial monitor or connected device.

Arduino Code

1//  Define pin connections
2const int switchPin = 2;   // Limit Switch connected to digital pin D2
3const int ledPin = 13;     // On-board LED used for status indication
4
5void setup() {
6  //  Set the Limit Switch pin as input with pull-up enabled
7  pinMode(switchPin, INPUT_PULLUP);  // So the pin reads HIGH when switch is unpressed
8
9  //  Set the LED pin as output
10  pinMode(ledPin, OUTPUT);
11
12  //  Initialize Serial Monitor for debugging
13  Serial.begin(9600);
14  Serial.println("Limit Switch Ready...");
15}
16
17void loop() {
18  // Read the state of the switch
19  int switchState = digitalRead(switchPin);
20
21  // Check if the switch is pressed
22  if (switchState == LOW) {  
23    // The switch is pressed (COM is connected to GND, pulling the pin LOW)
24    Serial.println("Limit Switch is PRESSED!");
25    digitalWrite(ledPin, HIGH);  // Turn ON LED to indicate switch press
26  } else {
27    // The switch is not pressed
28    Serial.println("Limit Switch is RELEASED.");
29    digitalWrite(ledPin, LOW);   // Turn OFF LED
30  }
31
32  delay(300);  // Small delay for stability
33}
34

Applications of Limit Switches

  • End-stop detection in CNC and 3D printers
  • Door position sensing
  • Elevator floor level triggers
  • Safety interlock systems
  • Robotic arm position detection
  • Industrial automation equipment

Conclusion

Interfacing a Limit Switch with Arduino is a simple yet powerful way to add mechanical input to your projects. Whether you’re automating machinery or adding safety features, limit switches offer reliable physical detection for smarter systems.