Arduino Nano - Limit Switch

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

Arduino Nano with Limit Switch

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×Wires
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
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
Limit Switch pinout
image source: diyables.io

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

The wiring diagram between Arduino Nano 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 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.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-limit-switch */ #include <ezButton.h> ezButton limitSwitch(2); // create ezButton object for pin 2 void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { limitSwitch.loop(); // MUST call the loop() function first if(limitSwitch.isPressed()) Serial.println("The limit switch: UNTOUCHED -> TOUCHED"); if(limitSwitch.isReleased()) Serial.println("The limit switch: TOUCHED -> UNTOUCHED"); int state = limitSwitch.getState(); if(state == HIGH) Serial.println("The limit switch: UNTOUCHED"); else Serial.println("The limit switch: TOUCHED"); }

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.
COM6
Send
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 The limit switch: TOUCHED -> UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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!