ESP32 S3 UNO - Limit Switch
This ESP32 S3 Uno limit switch tutorial shows you how to wire a limit switch to the ESP32 S3 Uno form board and read it with Arduino code. A limit switch, also called a travel switch, tells your board when a moving part reaches the end of its path. You will see the switch state and every touch or release on the Serial Monitor.
In this tutorial, you will:
- Learn the C, NO and NC pins of a limit switch
- Wire a limit switch to the ESP32 S3 Uno
- Read the state of the travel switch with the ezButton library
- Detect when the switch is touched or released

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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 main job: it detects when a moving object reaches a limit. Machines use it to stop a motor, a door or a slider at the right place. You may also see it called a travel switch.
Pinout
Two models are very common: the KW12-3 and the V-156-1C25. Both have three pins, and each pin has its own role.
C pin
This is the common pin. You use it in both the normally open mode and the normally closed mode.
NO pin
NO means Normally Open. You use this pin in the normally open mode. It connects to C only while the lever is pressed.
NC pin
NC means Normally Closed. You use this pin in the normally closed mode. It connects to C while the lever is free.

How It Works
Knowing how the pins connect lets you pick the wiring that fits your code. In most projects you only use two of the three pins: the C pin and either the NO or the NC pin. This gives four basic ways to connect the switch. The table below shows each way and what the ESP32 S3 Uno input pin reads.
| C pin | NO pin | NC pin | ESP32 S3 Uno Input Pin's State | |
|---|---|---|---|---|
| 1 | GND | ESP32 S3 Uno Input Pin (with pull-up) | NOT connected | HIGH when untouched, LOW when touched |
| 2 | GND | NOT connected | ESP32 S3 Uno Input Pin (with pull-up) | LOW when untouched, HIGH when touched |
| 3 | 3.3V | ESP32 S3 Uno Input Pin (with pull-down) | NOT connected | LOW when untouched, HIGH when touched |
| 4 | 3.3V | NOT connected | ESP32 S3 Uno Input Pin (with pull-down) | HIGH when untouched, LOW when touched |
For each method, you can also swap the two wires (GND or 3.3V on one side, the input pin on the other), so there are 8 ways in total. You only need one of them. This tutorial uses the first method: C to GND, NO to the input pin, with the internal pull-up resistor.
WARNING
If you use method 3 or 4, connect the C pin to 3.3V, never to 5V. The ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant.
ESP32 S3 Uno Pinout
The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D2, A0, SDA…) and their GPIO numbers when you wire the circuit.

Wiring Diagram
Connect the C pin of the limit switch to GND and the NO pin to pin D7 of the ESP32 S3 Uno. No external resistor is needed, because the code turns on the internal pull-up.

This image is created using Fritzing. Click to enlarge image
| Limit Switch Pin | ESP32 S3 Uno Pin |
|---|---|
| C | GND |
| NO | D7 (GPIO14) |
| NC | Not connected |
ESP32 S3 Uno Code - Limit Switch
A limit switch bounces like a push button: one press can create several quick ON/OFF signals. You must filter this out, which is called debouncing (read more at Why debouncing is needed for the button/limit switch?). The ezButton library does the debouncing for you and also enables the internal pull-up resistor, so your code stays short.
※ NOTE THAT:
Most projects use the switch in one of two ways:
- Check the state: do one thing while the switch is TOUCHED, and the opposite while it is UNTOUCHED.
- Check the change: do something at the moment the switch goes from UNTOUCHED to TOUCHED, or from TOUCHED to UNTOUCHED.
The sketch below shows both ways. It prints a message each time the switch is touched or released, and it also prints the current state on every loop. The switch is on D7, so the code uses GPIO number 14.
Detailed Instructions
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Install the library: add the ezButton library with this guide: ezButton Library Guide.
- Upload the code: copy the code above, paste it into the Arduino IDE and click the Upload button.
- Test the switch: open the Serial Monitor at 9600 baud, then press and release the limit switch lever.
- Check the output on the Serial Monitor. It should look like the lines below.
- Tip: the state message prints on every loop, so the Serial Monitor scrolls fast. Remove those lines if you only need the touch and release events.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
FAQ
Do I need a resistor to connect a limit switch to the ESP32 S3 Uno?
No. With the wiring in this tutorial, the ezButton library turns on the internal pull-up resistor of the input pin. The pin reads HIGH when the switch is free and LOW when it is touched. You only need an external resistor if you use a pull-down method.
Can I connect the limit switch to 5V on the ESP32 S3 Uno?
Do not do it. The ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant, so 5V on an input pin can damage the board. Use GND with the pull-up method, or 3.3V with a pull-down method.
Which ESP32 S3 Uno pin should I use for the limit switch?
Any normal digital pin works. This tutorial uses D7 (GPIO14), so the code uses 14. Avoid D6 (GPIO3) and D9 (GPIO46) because they are boot strapping pins, and avoid D0/D1 because they are used for USB serial.
Is the code different from the UNO R4 code?
Only the pin number changes. On the UNO R4 you write 7 for pin D7, but on the ESP32 S3 Uno you write the GPIO number, which is 14. The ezButton library works the same on both boards.
How do I stop a motor when the limit switch is touched?
Use the isPressed() event or check getState() for LOW in your loop. When the switch is touched, turn off the motor driver pin or send a stop command. Check the switch often so the motor stops right away.
What if my project needs more pins than the Uno headers give?
The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For extra limit switches, GPIO35 to GPIO42 or GPIO15/GPIO16 are good choices, because they work as inputs. Do not use GPIO47/GPIO48 for switches, because they are output only. Two warnings: (1) GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. (2) GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, so a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port appears in Arduino IDE | USB driver missing or board not in upload mode | Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET |
| State is always UNTOUCHED | Wrong pins used on the switch or loose wire | Check that C goes to GND and NO goes to D7 (GPIO14), and check each wire |
| State is always TOUCHED | NC pin used instead of NO, or the NO wire touches GND | Move the wire from NC to NO, and check for a short to GND |
| One touch shows several events | Switch bounce | Keep limitSwitch.loop() at the top of loop() and raise the debounce time, for example to 100 ms |
| Nothing changes in the code | Wrong pin number in code | Write 14 (the GPIO number) in the code, not 7 or D7 |
| Compile error ezButton.h not found | Library not installed | Install the ezButton library from the Library Manager |
| Serial Monitor shows strange characters | Baud rate mismatch | Set the Serial Monitor to 9600 baud |