A limit switch tells your ESP32 C3 Super Mini when a moving part has reached the end of its travel. It is a cheap, reliable way to protect motors, doors, sliders and 3D printer axes. It is purely mechanical, needs no power itself, and can be read with just one GPIO pin and the chip's internal pull-up resistor.
This post covers wiring it up and reading its state in code using the ezButton library, which handles debouncing for you.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 C3 Super Mini - Limit Switch
What you need
- ESP32 C3 Super Mini
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Limit Switch (KW12-3)
- Limit Switch Module
- Limit Switch (V-153-1C25)
- Limit Switch (V-155-1C25)
- Limit Switch (V-156-1C25)
- Wires
- Breadboard
- Jumper Wires
- DC Power Jack
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A limit switch like the KW12-3 or V-156-1C25 has three pins: C (common), NO (normally open) and NC (normally closed). The simplest setup uses only C and NO: connect C to GND and NO to a free GPIO pin, here GPIO7. Avoid GPIO8 (built-in LED), GPIO9 (boot pin), and GPIO20/GPIO21 (used by the Serial Monitor UART).
No external resistor is needed because the code enables the ESP32's internal pull-up. For a solid connection, solder the wires to the switch pins and cover the joint with heat shrink tubing, and keep the wires short since long leads pick up electrical noise and cause false readings.
How it works
Inside the switch, a metal lever or roller is pushed by whatever is moving. When untouched, C is connected to NC and disconnected from NO. When touched, C connects to NO and disconnects from NC. Wiring C to GND and NO to your GPIO with the internal pull-up means the pin reads HIGH when the switch is untouched and LOW when it's pressed.
Mechanical contacts physically bounce for a few milliseconds when they open or close, so a single press can look like several rapid presses to the microcontroller. This is why debouncing matters, just like with a pushbutton.
Code
The sketch uses the ezButton library, which takes care of both debouncing and enabling the internal pull-up resistor so you don't have to configure pinMode() manually. It creates an ezButton object on GPIO7 and sets a 50 ms debounce filter.
In loop(), it calls limitSwitch.loop() first, then checks isPressed() and isReleased() to detect state transitions, and reads getState() to print whether the switch is currently TOUCHED or UNTOUCHED every cycle.
esp32-c3-super-mini-limit-switch.ino
/*
* This ESP32 C3 Super Mini code was developed by newbiely.com
*
* This ESP32 C3 Super Mini code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-limit-switch
*/
#include <ezButton.h>
ezButton limitSwitch(7); // create ezButton object that attach to the ESP32 C3 SuperMini pin GPIO7
void setup() {
Serial.begin(115200);
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");
}
Things that go wrong
If you see rapid flickering between TOUCHED and UNTOUCHED, suspect bounce or noisy wiring rather than a broken switch; check the debounce time and wire length. If the state never changes, double check you wired C to GND and NO to the GPIO, not NC, since this tutorial assumes the normally open mode. Avoid the reserved pins (GPIO8, GPIO9, GPIO20, GPIO21) or you'll get confusing behavior unrelated to the switch itself.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-limit-switch

Top comments (0)