ESP32 S3 - Limit Switch

This tutorial instructs you how to use the Limit Switch with ESP32 S3. The ESP32 S3 reads the state of the limit switch and reports whether it is touched or untouched via the Serial Monitor.

ESP32 S3 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:

What you'll build:

  1. Connect a Limit Switch to the ESP32 S3 using a breadboard
  2. Read the switch state (TOUCHED / UNTOUCHED) in real time
  3. Detect state transitions (UNTOUCHED → TOUCHED and TOUCHED → UNTOUCHED)
  4. Display live readings in the Arduino IDE Serial Monitor

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Limit Switch (KW12-3)
1×Limit Switch Module
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×Wires
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

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

A Limit Switch gets its name from its primary purpose: detecting when a moving object has reached a defined limit or boundary. Unlike a standard pushbutton, a limit switch is typically built for repeated mechanical actuation and is often used in industrial and automation contexts where precise position detection is required.

Key Specifications

There exist various types of limit switches, but among the most commonly used are the KW12-3 and V-156-1C25. 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 uses only two pins — the C pin and one of the remaining two pins. This results in four different ways to connect a Limit Switch. The table below illustrates the wiring and the resulting input state on the ESP32 S3 for each of the four methods.

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

By swapping the GND pin and ESP32 S3 Input Pin for each of the four ways, there are a total of eight different ways to connect an ESP32 S3 to a Limit Switch. Out of the eight ways, this tutorial focuses on the first method as the working example.

Wiring Diagram

Connect the Limit Switch to the ESP32 S3 using either a breadboard or a screw terminal breakout board, following the diagrams below. 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.

Safety Notes

Always ensure the limit switch wiring matches the intended pull-up or pull-down configuration before powering the board to avoid unintended readings.

The wiring diagram between ESP32 S3 limit switch

This image is created using Fritzing. Click to enlarge image

ESP32 S3 Code - Limit Switch

Similar to a button, a Limit Switch also requires debouncing (more information can be found at Why needs debounce for the button/limit switch?). The ezButton library offers built-in debouncing functionality and uses internal pull-up registers, which simplifies the code considerably. The following code reads the limit switch state on every loop iteration and prints any state changes to the Serial Monitor.

※ 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 ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-limit-switch */ #include <ezButton.h> ezButton limitSwitch(7); // create ezButton object that attach to ESP32 S3 pin GPIO7 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Follow the wiring diagram provided above to connect your Limit Switch to ESP32 S3.
  3. Connect your ESP32 S3 to your PC using a USB cable.
  4. Open the Arduino IDE software.
  5. Install the ezButton library. Refer to the instructions
  6. Select the appropriate board and port in the Arduino IDE.
  7. Compile and upload the code to the ESP32 S3 board by clicking the Upload button in the Arduino IDE.
  8. Test the Limit Switch by touching and releasing it.
  9. Observe the output on the Serial Monitor in the Arduino IDE.
  10. Pro Tip: Try switching between the NO and NC pins to observe how the default state (TOUCHED vs UNTOUCHED) is inverted without changing a single line of code.

Serial Monitor

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-01-15 09:23:11] The limit switch: UNTOUCHED [2026-01-15 09:23:11] The limit switch: UNTOUCHED [2026-01-15 09:23:12] The limit switch: UNTOUCHED [2026-01-15 09:23:12] The limit switch: UNTOUCHED -> TOUCHED [2026-01-15 09:23:13] The limit switch: TOUCHED [2026-01-15 09:23:13] The limit switch: TOUCHED [2026-01-15 09:23:13] The limit switch: TOUCHED [2026-01-15 09:23:14] The limit switch: TOUCHED [2026-01-15 09:23:14] The limit switch: TOUCHED [2026-01-15 09:23:15] The limit switch: TOUCHED [2026-01-15 09:23:15] The limit switch: TOUCHED [2026-01-15 09:23:16] The limit switch: TOUCHED -> UNTOUCHED [2026-01-15 09:23:16] The limit switch: UNTOUCHED [2026-01-15 09:23:17] The limit switch: UNTOUCHED [2026-01-15 09:23:17] The limit switch: UNTOUCHED
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications

Limit switches are used in a wide range of real-world automation and safety scenarios where precise position or end-of-travel detection is needed. Here are some common applications:

  1. CNC Machine End Stop: Detect when a tool carriage reaches the end of its travel range and halt motion to prevent damage.
  2. 3D Printer Homing: Automatically home each axis by triggering when the print head reaches its minimum position.
  3. Conveyor Belt Control: Stop a conveyor belt when a product reaches a specific position on the line.
  4. Door or Gate Position Sensing: Detect whether a door or gate is fully open or fully closed for access control systems.
  5. Elevator Floor Detection: Signal the controller when an elevator car arrives at a designated floor level.
  6. Robotic Arm Safety: Prevent a robotic arm joint from exceeding its safe range of motion by triggering an emergency stop.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenges

Limit switches are simple but powerful components — pushing their usage beyond basic state detection opens up many interesting projects. Here are some challenges to deepen your understanding:

  1. Beginner: Modify the code to turn on the ESP32 S3's built-in LED whenever the limit switch is in the TOUCHED state.
  2. Intermediate: Use two limit switches to represent the "home" and "end" positions of a simulated conveyor, and print a message each time the virtual object travels between them.
  3. Advanced: Implement a debounce counter that tracks how many times the limit switch has been triggered in total and displays a running count on the Serial Monitor, resetting back to zero after 100 presses.

※ 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!