Arduino - Limit Switch
The limit switch has been using in many application such as motor control, automation. In this tutorial, we will learn how to use the Limit Switch with Arduino.
Please do not confuse with the following:
|1||×||Arduino UNO or Genuino UNO|
|1||×||USB 2.0 cable type A/B|
|1||×||(Optional) Heat Shrink Tubing|
|1||×||(Optional) Soldering Iron|
|1||×||(Optional) 9V Power Adapter for Arduino|
|1||×||(Optional) Screw Terminal Block Shield for Arduino Uno|
Or you can buy the following sensor kit:
|1||×||DIYables Sensor Kit 30 types, 69 units|
About Limit Switch
It is called Limit Switch because its main functionality is used to detect the moving object reaching a limit.
The Limit Switch has 3 pins:
- C pin: is the common pin. It is used in both normally open mode and normally closed mode
- NO pin: is normally open pin. It is used in the normally open mode
- NC pin: is normally closed pin. It is used in the normally closed mode
How It Works
Although The Limit Switch has 3 pins, a normal application usually uses only two pins: C pin and one of two remaining pins. Accordingly, there are four ways to use limit switch. The below is the wiring table for limit switch and the reading state on Arduino in all four ways:
|C pin||NO pin||NC pin||Arduino Input Pin's State|
|1||GND||Arduino Input Pin (with pull-up)||not connected||HIGH when untouched, LOW when touched|
|2||GND||not connected||Arduino Input Pin (with pull-up)||LOW when untouched, HIGH when touched|
|3||VCC||Arduino Input Pin (with pull-down)||not connected||LOW when untouched, HIGH when touched|
|4||VCC||not connected||Arduino Input Pin (with pull-down)||HIGH when untouched, LOW when touched|
For each way, we can swap GND pin and Arduino Input Pin. Therefore, we have 8 way to connect Arduino to a limit switch.
We only need to choose one of the above four ways. The rest of tutorial will use the first way.
This image is created using Fritzing. Click to enlarge image
Arduino Code - Limit Switch
Just like a button, a limit switch also needs to be debounced (See more at Why needs debounce for the button/limit switch?). Debouncing make the code complicated. Fortunately, the ezButton library supports the debouncing functionm, The library also uses internal pull-up register. These make easy to us to program
※ NOTE THAT:
There are two wide-used use cases:
- The first: If the switch's state is TOUCHED, do something. If the input state is UNTOUCHED, do another thing in reverse.
- The second: If the switch's state is changed from UNTOUCHED to TOUCHED (or TOUCHED to UNTOUCHED), do something.
- Do wiring as above wiring diagram
- Connect Arduino to PC via USB cable
- Open Arduino IDE
- Install ezButton library. See How To
- Select the right board and port
- Click Upload button on Arduino IDE to upload code to Arduino
- Touch the Limit Switch and release.
- See the result on Serial Monitor.
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.