Raspberry Pi - Limit Switch

This tutorial instructs you how to use Raspberry Pi with the limit switch. In detail, we will learn:

Raspberry Pi with Limit Switch

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×Wires
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Limit Switch

It is called Limit Switch because its primary purpose is to detect when a moving object has reached a limit.

The Limit Switch Pinout

There exist various types of limit switches, yet among the most widely favored are the KW12-3 and V-156-1C25 models, each featuring three pins:

  • C: This is the common pin and is used in both the normally open and normally closed modes
  • NO: This is the normally open pin and is used in the normally open mode
  • NC: This is the normally closed pin and is used in the normally closed mode
Limit Switch pinout
image source: diyables.io

How It Works

Although the limit switch has three pins, typically only two pins are used: the C pin and one of the other two. Therefore, there are four possible configurations. The following table shows the wiring of the limit switch and the corresponding readings on Raspberry Pi for all four scenarios:

C pin NO pin NC pin Raspberry Pi Input Pin's State
1 GND Raspberry Pi Input Pin (with pull-up) not connected HIGH when untouched, LOW when touched
2 GND not connected Raspberry Pi Input Pin (with pull-up) LOW when untouched, HIGH when touched
3 VCC Raspberry Pi Input Pin (with pull-down) not connected LOW when untouched, HIGH when touched
4 VCC not connected Raspberry Pi Input Pin (with pull-down) HIGH when untouched, LOW when touched

For each method, we can interchange the GND pin and the Raspberry Pi input pin. This gives us a total of 8 possibilities to connect a Raspberry Pi to a limit switch.

We must select one of the four options. The remainder of the tutorial will use the first method.

Wiring Diagram

The wiring diagram between Raspberry Pi and Limit Switch

This image is created using Fritzing. Click to enlarge image

For a secure and reliable wiring connection, we suggest using a Soldering Iron to solder the wires and pins of the limit switch together. Afterwards, use Heat Shrink Tube for safety.

Raspberry Pi Code - Limit Switch

Similar to a button, a limit switch also requires debouncing (for more information, see Why needs debounce for the button/limit switch?).

※ NOTE THAT:

Two popular applications exist with limit switch:

  • The first: If the switch is TOUCHED, perform one action. If the switch is UNTOUCHED, carry out a different action in response.
  • The second: If the switch is changed from UNTOUCHED to TOUCHED (or TOUCHED to UNTOUCHED), execute an action.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file limit_switch.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-limit-switch import RPi.GPIO as GPIO # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin for your button SWITCH_PIN = 16 # Define debounce time in milliseconds DEBOUNCE_TIME_MS = 200 # 200 milliseconds # Set the initial state and pull-up resistor for the button GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize the button state and previous state switch_state = GPIO.input(SWITCH_PIN) prev_switch_state = switch_state # Define a function to handle button presses def button_callback(channel): global switch_state switch_state = GPIO.input(SWITCH_PIN) # Add an event listener for the button press GPIO.add_event_detect(SWITCH_PIN, GPIO.BOTH, callback=button_callback, bouncetime=DEBOUNCE_TIME_MS) try: # Main loop while True: # Check if the button state has changed if switch_state != prev_switch_state: if switch_state == GPIO.HIGH: print("The limit switch: TOUCHED -> UNTOUCHED") else: print("The limit switch: UNTOUCHED -> TOUCHED") prev_switch_state = switch_state if switch_state == GPIO.HIGH: print("The limit switch: UNTOUCHED") else: print("The limit switch: TOUCHED") except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 limit_switch.py
  • Press and then release the Limit Switch.
  • View the result in the Terminal.
PuTTY - Raspberry Pi
The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED -> TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED -> UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!