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:

  1. Learn the C, NO and NC pins of a limit switch
  2. Wire a limit switch to the ESP32 S3 Uno
  3. Read the state of the travel switch with the ezButton library
  4. Detect when the switch is touched or released
ESP32 S3 Uno limit switch

Hardware Preparation

1×ESP32 S3 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 .

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.

Limit Switch Pinout
image source: diyables.io

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.

ESP32 S3 Uno pinout diagram

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.

The wiring diagram between ESP32 S3 Uno limit switch

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.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-limit-switch */ #include <ezButton.h> ezButton limitSwitch(14); // create ezButton object that attach to the ESP32 S3 Uno pin D7 (GPIO14) 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 the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Install the library: add the ezButton library with this guide: ezButton Library Guide.
  6. Upload the code: copy the code above, paste it into the Arduino IDE and click the Upload button.
  7. Test the switch: open the Serial Monitor at 9600 baud, then press and release the limit switch lever.
  8. Check the output on the Serial Monitor. It should look like the lines below.
  9. 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.
∞
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
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 -> UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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

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