An ON/OFF switch, also called a toggle switch, flips between two stable states each time you press it and stays there until you press it again - unlike a button, which only reports ON while you hold it down. This tutorial wires a two-pin ON/OFF switch to the ESP32 C3 Super Mini and reads its state in code, a pattern you'll reuse for lamp switches, mode selectors, or any on/off control in a project.
It also shows the debouncing trick that keeps switch-reading code short and reliable.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 C3 Super Mini - 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)
- Wires
- ON/OFF Square Switch
- ON/OFF Round Switch
- On/Off Switch Module
- DC Power Jack
- Breadboard
- Jumper Wires
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect one pin of the switch to GPIO7 and the other pin to GND - the two pins have no polarity, so it doesn't matter which goes where. Stick to 3.3V logic; never feed 5V into any GPIO pin on the ESP32 C3 Super Mini. Avoid GPIO8 and GPIO9, since those are boot strapping pins used during startup. For a sturdier connection, solder the wires to the switch pins and cover the joints with heat shrink tubing.
How it works
The ESP32 C3 Super Mini has an internal pull-up resistor you can enable in software, so you don't need an external resistor - this tutorial relies on that built-in pull-up. With it enabled, the pin reads HIGH when the switch is open and LOW when it's closed. Because the switch is latching, each press flips the state and it holds there until you press it again, unlike a momentary button that only stays active while held.
Code
The sketch uses the ezButton library, which handles debouncing - filtering the brief electrical noise a mechanical contact produces when it opens or closes - and turns on the internal pull-up resistor for you. It reads the switch on GPIO7 with a 50 ms debounce filter, prints OFF -> ON or ON -> OFF whenever the state changes, and prints the current state as ON or OFF on every loop through the Serial Monitor.
esp32-c3-super-mini-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-switch
*/
#include <ezButton.h>
ezButton onOffSwitch(7); // create ezButton object that attach to the ESP32 C3 SuperMini pin GPIO7
void setup() {
Serial.begin(115200);
onOffSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
onOffSwitch.loop(); // MUST call the loop() function first
if (onOffSwitch.isPressed())
Serial.println("The switch: OFF -> ON");
if (onOffSwitch.isReleased())
Serial.println("The switch: ON -> OFF");
int state = onOffSwitch.getState();
if (state == HIGH)
Serial.println("The switch: OFF");
else
Serial.println("The switch: ON");
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-switch

Top comments (0)