Arduino Nano ESP32 - Limit Switch

This tutorial provides instructions on how to use the Limit Switch with Arduino Nano ESP32.

Arduino Nano ESP32 with Limit Switch

It is important to note that this tutorial is specifically about using a Limit Switch, please do not confuse with the following:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×Wires
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
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 called Limit Switch because its main functionality is to detect the moving object reaching a limit.

A Limit Switch is so named because its primary purpose is to detect when a moving object has reached a limit.

Pinout

There exist various types of limit switches, with the KW12-3 and V-156-1C25 being the most favored among them. Each of these types features three pins:

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

How It Works

A typical Limit Switch application typically uses only two pins, the C pin and one of the remaining two pins. This results in four different ways to use a Limit Switch. The table below illustrates the wiring and reading state on an Arduino Nano ESP32 for each of these four methods.

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

By swapping the GND pin and Arduino Nano ESP32 Input Pin for each of the four ways, there are a total of eight different ways to connect an Arduino Nano ESP32 to a Limit Switch.

Out of the eight ways, the tutorial will focus on one method, specifically the first method will be used as an example.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Limit Switch

This image is created using Fritzing. Click to enlarge image

To make the wiring connection stable and firm, we recommend using to solder wires and limit switch's pin together, and then use to make it safe.

For a stable and secure connection, it is recommended to use a soldering iron to solder the wires and Limit Switch pins together, and then use heat shrink tubes for added safety.

Arduino Nano ESP32 Code - Limit Switch

Just like a button, a limit switch also needs to be debounced (See more at ). 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

Similar to a button, a Limit Switch also requires debouncing (more information can be found at Why needs debounce for the button/limit switch?). Debouncing can make the code more complex. However, the ezButton library offers debouncing functionality and utilizes internal pull-up registers, making programming easier.

※ NOTE THAT:

Two common use cases for a Limit Switch are:

  • The first use case: If the switch is TOUCHED, perform a certain action. If the input state is UNTOUCHED, perform the opposite action.
  • The second use case: If the switch's state changes from UNTOUCHED to TOUCHED (or TOUCHED to UNTOUCHED), perform a specific action.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-limit-switch */ #include <ezButton.h> ezButton limitSwitch(D2); // create ezButton object that attach to the Arduino Nano ESP32 pin D2 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

  • Follow the wiring diagram provided above to connect your Limit Switch to Arduino Nano ESP32.
  • Connect your Arduino Nano ESP32 to your PC using a USB cable.
  • Open the Arduino IDE software.
  • Install the ezButton library. Refer to the instructions
  • Select the appropriate board and port in the Arduino IDE.
  • Compile and upload the code to the Arduino Nano ESP32 board by clicking the Upload button in the Arduino IDE.
  • Test the Limit Switch by touching and releasing it.
  • Check out the output on the Serial Monitor in the Arduino IDE.
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!