Arduino Nano - Limit Switch
This tutorial instructs you how to use Arduino Nano with the limit switch. In detail, we will learn:
- How to connect the Arduino Nano to the limit switch.
- How to program the Arduino Nano to read the state of the limit switch.
- How to program the Arduino Nano 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 referred to as Limit Switch due to its primary purpose of detecting when a moving object has reached a limit.
The Limit Switch Pinout
There exist various types of limit switches, yet among the most favored are the KW12-3 and V-156-1C25. Each of these types features three pins:
- C: This is the common pin and is used for 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, usually only two of them are used in a normal application: the C pin and one of the other two. Therefore, there are four possible ways to wire the limit switch. The following table shows the wiring and the reading state on Arduino Nano for all four methods:
C pin | NO pin | NC pin | Arduino Nano Input Pin's State | |
---|---|---|---|---|
1 | GND | Arduino Nano Input Pin (with pull-up) | not connected | HIGH when untouched, LOW when touched |
2 | GND | not connected | Arduino Nano Input Pin (with pull-up) | LOW when untouched, HIGH when touched |
3 | VCC | Arduino Nano Input Pin (with pull-down) | not connected | LOW when untouched, HIGH when touched |
4 | VCC | not connected | Arduino Nano Input Pin (with pull-down) | HIGH when untouched, LOW when touched |
For every method, we can exchange the GND pin and the Arduino Nano Input Pin. Consequently, there are 8 ways to link the Arduino Nano to a limit switch.
We must select one of the four options. The remainder of the tutorial will utilize 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 added safety.
Arduino Nano Code - Limit Switch
Similar to a button, a limit switch also requires debouncing (see more in Why needs debounce for the button/limit switch?). This can make coding complex. Fortunately, the ezButton library offers a debouncing function. Additionally, it utilizes an internal pull-up register, which simplifies programming the button.
※ NOTE THAT:
Two common applications exist with limit switch:
- The first: If the switch is in the TOUCHED position, perform one action. If the input is UNTOUCHED, do the opposite.
- The second: If the switch is changed from UNTOUCHED to TOUCHED (or TOUCHED to UNTOUCHED), take some action.
Detailed Instructions
- Do the wiring as indicated in the wiring diagram.
- Connect the Arduino Nano to a PC using a USB cable.
- Open the Arduino IDE.
- Install the ezButton library. Refer to the instructions at How To.
- Choose the appropriate board and port.
- Click the Upload button on the Arduino IDE to upload the code to the Arduino Nano.
- Press and then release the Limit Switch.
- Check out the result on the Serial Monitor.