ESP32 C3 Super Mini - Limit Switch

A limit switch tells your ESP32 C3 Super Mini when a moving part has reached the end of its travel. It is a cheap and very reliable way to protect motors, doors, sliders and 3D printer axes. This ESP32 C3 Super Mini limit switch tutorial shows you the wiring and the code in simple steps.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Limit Switch

This tutorial is only about the Limit Switch. Please do not confuse it with:

Hardware Preparation

1×ESP32 C3 Super Mini
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 is a mechanical switch that is pressed by a moving object when the object reaches a limit.

Key Features:

  • Mechanical: A metal lever or roller pushes the contact.
  • No power needed: The switch itself needs no supply voltage.
  • Two modes: Works as normally open (NO) or normally closed (NC).
  • Long lever: Easy to hit, even with a slow moving part.
  • Very reliable: Made for machines, doors and moving rails.

Technical Specifications:

  • Common models: KW12-3 and V-156-1C25.
  • Pins: 3 pins (C, NO, NC).
  • Contact type: SPDT (one common pin, two outputs).
  • Signal for ESP32 C3 Super Mini: 3.3V logic only. Wire the switch to GND or 3.3V, never to 5V.
  • Output: A simple open or closed contact, read by any digital pin.

Pinout

There are many types of limit switches. The KW12-3 and V-156-1C25 are the most popular. Both have three pins.

  • C pin: The common pin. It is used in both normally open and normally closed modes.
  • NO pin: The normally open pin. It is used in the normally open mode.
  • NC pin: The normally closed pin. It is used in the normally closed mode.
Limit Switch Pinout
image source: diyables.io

How It Works

A limit switch normally uses only two pins: the C pin and one of the other two pins.

  • Untouched: C is connected to NC. C is NOT connected to NO.
  • Touched: C is connected to NO. C is NOT connected to NC.

This gives four ways to use a limit switch. The table below shows the wiring and the state read by the ESP32 C3 Super Mini for each way.

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

By swapping the GND pin and the ESP32 C3 Super Mini input pin in each of the four ways, there are eight ways in total to connect an ESP32 C3 Super Mini to a limit switch.

This tutorial uses only the first way, because it needs no extra resistor. The ESP32 C3 Super Mini has an internal pull-up resistor built into the chip.

WARNING

The ESP32 C3 Super Mini works at 3.3V logic only. Never connect the C pin to a 5V supply. 5V on a GPIO pin can damage the board. Use GND or the 3.3V pin only.

Wiring Diagram

Connect the C pin to GND and the NO pin to a free GPIO pin on the ESP32 C3 Super Mini.

The wiring diagram between ESP32 C3 Super Mini Limit Switch

This image is created using Fritzing. Click to enlarge image

Limit Switch Pin ESP32 C3 Super Mini Pin
C GND
NO GPIO7
NC not connected
  • Note: GPIO7 is a free pin. Do not use GPIO8 (built-in LED), GPIO9 (boot pin), GPIO20 or GPIO21 (Serial Monitor UART).
  • Note: No external resistor is needed. The code turns on the internal pull-up resistor.
  • Note: For a stable and secure connection, use a soldering iron to solder the wires and the limit switch pins together, then cover the joint with heat shrink tubes.
  • Note: Keep the switch wires short. Long wires pick up noise and cause false readings.

ESP32 C3 Super Mini Code - Limit Switch

Like a button, a limit switch needs debouncing, and the ezButton library does that work for you.

What This Code Does:

  • Sets GPIO7 as an input with the internal pull-up resistor.
  • Debounces the limit switch with a 50 ms filter.
  • Prints TOUCHED or UNTOUCHED every loop.
  • Prints a message when the state changes from UNTOUCHED to TOUCHED.
  • Prints a message when the state changes from TOUCHED to UNTOUCHED.

※ 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 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"); }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire the parts: Follow the wiring diagram above to connect the limit switch to the ESP32 C3 Super Mini.
  • Connect the board: Plug the ESP32 C3 Super Mini into your PC with a USB Type-C cable.
  • Open the IDE: Start the Arduino IDE software.
  • Install the library: Install the ezButton library. See the instructions.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Pick the COM port of your board in Tools > Port.
  • Upload the code: Click the Upload button in the Arduino IDE.
  • Open Serial Monitor: Set the baud rate to 115200.
  • Test it: Press and release the limit switch lever a few times.
  • Check the output: Watch the messages on the Serial Monitor.
  • Pro Tip: If the state never changes, swap the NO pin and the NC pin. Many cheap switches have the labels printed the other way round.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 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 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
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Line-by-line Code Explanation

The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!

Application/Project Ideas

A limit switch is used any time a moving part must stop at a safe point.

  • 3D Printer Endstop: Stop an axis before it hits the frame.
  • Automatic Door: Detect when a sliding door is fully open or fully closed.
  • Drawer Alarm: Send an alert when a drawer or cabinet is opened.
  • Motor Protection: Cut power to a linear actuator at the end of its travel.
  • Object Counter: Count boxes that pass on a small conveyor belt.
  • Safety Cover: Stop a machine when its lid is lifted.

Challenge Yourself

Try these small projects to practice with the limit switch and the ESP32 C3 Super Mini.

  • Easy: Turn the built-in LED on GPIO8 ON while the limit switch is touched.
  • Easy: Count how many times the switch is touched and print the total.
  • Medium: Use two limit switches on GPIO6 and GPIO7 to detect both ends of a rail.
  • Medium: Stop a servo motor as soon as the limit switch is touched.
  • Advanced: Use the NC mode instead of the NO mode, so a broken wire is also reported as an alarm.

Language References

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