Raspberry Pi - Limit Switch
This tutorial instructs you how to use Raspberry Pi with the limit switch. In detail, we will learn:
- How to connect the Raspberry Pi to the limit switch.
- How to program the Raspberry Pi to read the state of the limit switch.
- How to program the Raspberry Pi to check if the limit switch is touched or not.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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
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
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:
- Create a Python script file limit_switch.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Press and then release the Limit Switch.
- View the result in the Terminal.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.