ESP32 UNO - Limit Switch

Need your machine to know when a slider hits the end of its rail? Put an ESP32 Uno limit switch at that spot and your sketch gets a clean signal each time the lever is pushed. Makers also call this part a travel switch or an end stop. On the ESP32 Uno form board it needs only two wires and the free ezButton library, and the Serial Monitor shows every touch and release.

By the end, you can:

  1. Name the C, NO and NC terminals of a travel switch
  2. Pick one of the four ways to wire a limit switch
  3. Connect the switch to D7 and GND with no extra resistor
  4. Read the debounced state with the ezButton library in Arduino IDE
  5. React the moment the lever is pushed or let go
ESP32 Uno limit switch

Components You Need

1×ESP32 Uno-form Board
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×Optionally, Heat Shrink Tubing
1×Optionally, Soldering Iron

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 .

Meet the Limit Switch

After this part, you know what the switch does and which terminal to use.

The name says it all: the part reports when a moving object reaches its limit. 3D printers, CNC machines, garage doors and robot arms use it as an end stop. Another common name is travel switch.

Pinout

You will find the KW12-3 and V-156-1C25 in many kits, and both carry three metal tabs.

Terminal Full Name Joined to C When
C Common Always used, in both modes
NO Normally Open The lever is pushed down
NC Normally Closed The lever is resting
Limit Switch Pinout
image source: diyables.io

How It Works

Here you choose a wiring method that matches the logic level your code expects.

A real project rarely needs all three tabs. Use C plus either NO or NC. That gives four basic setups, listed below with the level the ESP32 Uno input pin reads.

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

Swap the two wires in any row and the result stays the same, so there are 8 options in total. One is enough. This page uses row 1: C on GND, NO on the input pin, and the internal pull-up turned on by the code.

Good to know: Row 3 and row 4 need 3.3V on the C tab. ESP32 Uno pins run at 3.3V logic and are NOT 5V tolerant, so never put 5V there.

ESP32 Uno Pinout

On the ESP32 Uno form board, the label printed next to a header (such as D7) is not the number your sketch uses. Find the GPIO number for that label in the diagram before you add a wire.

ESP32 Uno pinout diagram

How to Connect

When this part is done, the switch sits between D7 and GND.

The wiring diagram between ESP32 Uno limit switch

This image is created using Fritzing. Click to enlarge image

Limit Switch Pin ESP32 Uno Pin
C GND
NO D7 (GPIO14)
NC Leave free

Good to know: D7 (GPIO14) is a plain I/O pin. It is not a boot strapping pin and not an input-only pin, so the switch never blocks booting or uploads.

ESP32 Uno Code - Read a Limit Switch

This sketch prints the switch state on every loop, plus a line each time the lever goes down or comes back up.

A mechanical contact chatters for a few milliseconds when it closes. Your board can see one press as many. Filtering that out is called debouncing (Why debouncing is needed for the button/limit switch?). The ezButton library handles it and also switches on the pin's internal pull-up.

※ NOTE THAT:

You can use the switch signal in two styles:

  • Level style: run one action while the lever is TOUCHED and the reverse action while it is UNTOUCHED.
  • Edge style: run an action only at the instant the state flips, from UNTOUCHED to TOUCHED or back.
/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-limit-switch */ #include <ezButton.h> ezButton limitSwitch(14); // limit switch NO pin on D7 (GPIO14), C pin on GND void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // ignore contact bounce shorter than 50 ms } void loop() { limitSwitch.loop(); // update the switch first, before any check below 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"); }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Add ezButton: open the Library Manager and install it, or follow the ezButton Library Guide.
  6. Paste the sketch into a new window and click Upload.
  7. Start the Serial Monitor and choose 9600 baud from the list.
  8. Push the lever down, hold it for about one second, then let it spring back.
  9. Pro tip: Mount the switch so the moving part presses the lever only part of the way. Hitting it hard against its stop bends the lever and makes the click point drift over time.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
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 -> UNTOUCHED 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 -> UNTOUCHED The limit switch: UNTOUCHED
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Video Tutorial

See the ESP32 Uno build in action in the video below.

Code Explanation

After this part, you can change the sketch with confidence.

Code Part What It Does
ezButton limitSwitch(14) Links the object to D7 (GPIO14) and enables the internal pull-up
setDebounceTime(50) Ignores contact noise that lasts less than 50 ms
limitSwitch.loop() Reads the pin and updates the state; call it first in loop()
isPressed() / isReleased() True once, at the moment the lever goes down or comes up
getState() Returns HIGH (UNTOUCHED) or LOW (TOUCHED) right now

The result: two event lines appear only at each change, while the state line repeats all the time.

ESP32 Uno vs UNO R4 for This Project

Moving this end stop project from the UNO R4 to the ESP32 Uno only changes the pin number and the board setup.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Switch input pin D7 written as 7 D7 written as 14 (GPIO14)
Library ezButton ezButton (no change)
Pull-down methods use 5V on C 3.3V on C
Arduino IDE board Arduino UNO R4 WiFi or Minima ESP32 Dev Module
WiFi and Bluetooth WiFi only on the R4 WiFi WiFi plus Bluetooth Classic and BLE

The same wiring image works as-is, because the switch only joins D7 to GND. Watch out only if you change to row 3 or 4: then the C tab must go to 3.3V.

Go Wireless: Get an End Stop Alert on Your Phone

Here you turn the switch into a remote alert.

The ESP32 Uno has WiFi and Bluetooth on board. Send "TOUCHED" to a phone page each time isPressed() fires, and you can watch a door, a gate or a machine axis from across the room. You keep the same D7 wiring and simply add one print line to the web or Bluetooth monitor inside the event check.

Try These Next

  • Count how many times the lever is pushed and print the total.
  • Wire a second switch to D6 (GPIO27) and make a two-ended rail with a stop at each side.
  • Light an LED on D13 (GPIO18) only while the switch is TOUCHED.
  • Remove the repeating state lines so only the edge events print.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE CH340 driver missing or a charge-only cable Install the CH340 driver and use a USB cable that carries data
Upload stops at Waiting for download Board did not enter download mode Hold the BOOT button while the upload starts, then let go
Output stays UNTOUCHED when you push the lever Wire on NC, or C is not on GND Put the signal wire on NO and the C tab on a GND pin
Output stays TOUCHED with the lever free Signal wire on NC or a short to ground Move the wire to NO and look for bare metal touching GND
Each push prints more than one TOUCHED event Contact bounce longer than 50 ms Raise setDebounceTime() to 80 or 100
Nothing reacts at all Pin number 7 left in the sketch Use 14 in the ezButton line, which is the GPIO behind D7
Compile error about ezButton.h Library not installed Install ezButton from the Library Manager
Random letters on the Serial Monitor Wrong baud rate Pick 9600 baud to match Serial.begin()

FAQ

Does a limit switch need an external resistor on the ESP32 Uno?

Not with this wiring. ezButton turns on the internal pull-up of GPIO14, so the pin rests HIGH and goes LOW when the lever closes the contact. Only the pull-down rows (3 and 4) need an external 10kΩ resistor to GND.

What is the difference between the NO and NC terminals?

NO stays open until the lever is pushed. NC stays closed until the lever is pushed. Use NO for a "touched" signal and NC when you want a broken wire to look like a hit, which is safer for machines.

Can I power the limit switch C tab from 5V on the ESP32 Uno?

No. The ESP32 Uno input pins accept 3.3V at most. Put the C tab on GND (as in this guide) or on 3.3V if you use a pull-down method.

Can I use A2 to A5 for a limit switch?

You can, but A2 to A5 (GPIO35, 34, 36, 39) are input only and have no internal pull-up. ezButton cannot pull them HIGH, so add an external 10kΩ resistor from the pin to 3.3V. A normal digital pin like D7 is easier.

Why does the sketch say 14 and not 7?

The ESP32 Uno code always uses GPIO numbers. The header labeled D7 is wired to GPIO14 on this board, so you write 14. The UNO R4 code uses 7 for the same header.

Which pins should I avoid for switches on this board?

Skip D0 and D1 (USB serial), D8 (GPIO12), D10 (GPIO5) and A0 (GPIO2). The last three are boot strapping pins. A switch held closed at power-on can stop the board from starting.

Do I need a driver to upload code to the ESP32 Uno?

The board uses a CH340 USB-to-serial chip. Windows often installs it by itself. If no COM port appears, install the CH340 driver once.

Can the end stop cut power to a motor right away?

Check isPressed() or getState() == LOW in loop() and switch the motor driver off at that moment. Keep loop() free of long delay() calls so the stop happens at once.

Can I read more than one limit switch?

Yes. Create one ezButton object per switch, each with its own GPIO, and call loop() on every object at the top of loop(). An ezButton array is handy for four or more switches.

Can I see the switch state on my phone?

Yes. The ESP32 Uno has WiFi and Bluetooth. Send the state to the DIYables Web Monitor or Bluetooth Monitor app, as shown in the Go Wireless section.

What if my machine needs more switch inputs than the Uno headers give?

The ESP32 Uno has extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work with WiFi on) and GPIO15 (I/O, PWM, analog on ADC2). Add pin headers with a soldering iron, then type the GPIO number in your sketch. For extra limit switches, use GPIO32 and GPIO33 first. Keep two risks in mind. (1) GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC spot) decide the boot mode at power-on, so a switch that pulls them the wrong way can block booting or code uploads. (2) Never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They go to the flash chip and using them crashes the board.

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