A focused, lightweight Arduino library for reliable switch debouncing using INPUT_PULLUP mode — no delay(), no bloat, just clean stable GPIO input.
SmartSwitchDebounce is a simple Arduino library for debouncing switches and detecting stable switch state changes using INPUT_PULLUP mode.
It is useful for Arduino and ESP32 projects where a push button, toggle switch, or wall switch is connected between a GPIO pin and GND.
- Uses
INPUT_PULLUPmode - Millis-based debounce logic
- No
delay()required - Detects stable switch state changes
- Works with Arduino and ESP32 boards
- Simple and beginner-friendly API
- Useful for push buttons, toggle switches, and wall switches
Mechanical switches do not always produce a clean signal when pressed or flipped. A single physical switch action can create multiple rapid HIGH/LOW changes because of switch bounce.
Without debounce handling, one switch press may be detected multiple times.
This can cause issues such as:
- A relay toggling multiple times
- A counter increasing more than once
- A menu selection jumping unexpectedly
- False switch triggers
SmartSwitchDebounce solves this by waiting until the switch signal becomes stable before reporting a state change.
Connect the switch like this:
GPIO pin ─── Switch ─── GND
This library uses INPUT_PULLUP, so the switch behavior is:
Switch open → HIGH
Switch closed → LOW
No external pull-up resistor is required when using the internal pull-up resistor.
- Download or clone this repository.
- Copy the full
SmartSwitchDebouncefolder into your Arduino libraries folder. - Restart Arduino IDE.
- Open Arduino IDE and go to:
File → Examples → SmartSwitchDebounce
You should see the included examples.
Typical Arduino libraries folder locations:
Windows:
Documents\Arduino\libraries\
macOS:
Documents/Arduino/libraries/
Linux:
~/Arduino/libraries/
Final folder structure should look like this:
Arduino/libraries/SmartSwitchDebounce/
├── library.properties
├── keywords.txt
├── README.md
├── LICENSE
├── src/
│ ├── SmartSwitchDebounce.h
│ └── SmartSwitchDebounce.cpp
└── examples/
├── BasicSwitchTest/
│ └── BasicSwitchTest.ino
└── ToggleRelay/
└── ToggleRelay.ino
- Download this repository as a ZIP file.
- Open Arduino IDE.
- Go to:
Sketch → Include Library → Add .ZIP Library...
- Select the downloaded ZIP file.
- Restart Arduino IDE if needed.
#include <SmartSwitchDebounce.h>
SmartSwitchDebounce wallSwitch(14, 75);
void setup() {
Serial.begin(115200);
wallSwitch.begin();
Serial.println("SmartSwitchDebounce Basic Test Started");
}
void loop() {
if (wallSwitch.changed()) {
Serial.println("Switch state changed");
if (wallSwitch.isPressed()) {
Serial.println("Switch is CLOSED / LOW");
} else {
Serial.println("Switch is OPEN / HIGH");
}
}
}This example toggles a relay every time a stable switch state change is detected.
#include <SmartSwitchDebounce.h>
SmartSwitchDebounce wallSwitch(14);
const int relayPin = 26;
const bool relayActiveLow = true;
bool relayStatus = false;
void setup() {
Serial.begin(115200);
wallSwitch.begin();
pinMode(relayPin, OUTPUT);
turnRelayOff();
Serial.println("Toggle Relay Example Started");
}
void loop() {
if (wallSwitch.changed()) {
relayStatus = !relayStatus;
if (relayStatus == true) {
turnRelayOn();
Serial.println("Relay ON");
} else {
turnRelayOff();
Serial.println("Relay OFF");
}
}
}
void turnRelayOn() {
if (relayActiveLow == true) {
digitalWrite(relayPin, LOW);
} else {
digitalWrite(relayPin, HIGH);
}
}
void turnRelayOff() {
if (relayActiveLow == true) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
}Creates a SmartSwitchDebounce object.
Parameters:
pin- GPIO pin connected to the switchdebounceDelay- debounce delay in milliseconds
Example:
SmartSwitchDebounce mySwitch(14);Or with a custom debounce delay:
SmartSwitchDebounce mySwitch(14, 100);Initializes the switch pin using INPUT_PULLUP mode and stores the initial switch state.
Example:
mySwitch.begin();Checks the switch and returns true only when a stable switch state change is detected.
This method uses millis() internally and does not use delay().
Example:
if (mySwitch.changed()) {
Serial.println("Switch changed");
}Returns the latest stable switch state.
Return values:
HIGH → Switch open
LOW → Switch closed
Example:
if (mySwitch.currentState() == LOW) {
Serial.println("Switch is closed");
}Returns true when the stable switch state is LOW.
This is useful for push buttons or switches connected between GPIO and GND.
Example:
if (mySwitch.isPressed()) {
Serial.println("Switch is pressed");
}A simple example that prints switch state changes to the Serial Monitor.
Use this example to test whether your switch wiring and debounce logic are working correctly.
A practical example that toggles a relay whenever a stable switch state change is detected.
This is useful for smart home, automation, and control projects.
This library should work with boards compatible with the Arduino framework, including:
- Arduino Uno
- Arduino Nano
- Arduino Mega
- ESP32
- ESP8266
- Other Arduino-compatible boards
The library uses standard Arduino functions:
pinMode()digitalRead()millis()
SmartSwitchDebounce can be used in:
- Smart home wall switch projects
- Relay control projects
- Push button input projects
- Menu navigation buttons
- Counter projects
- IoT device control
- ESP32 automation projects
- Arduino beginner projects
SmartSwitchDebounce only handles low-voltage switch input debounce logic.
Do not connect mains voltage or high-voltage signals directly to Arduino, ESP32, or any microcontroller GPIO pins.
When using this library with relays, AC loads, or high-voltage systems, follow proper electrical safety practices. Use suitable isolation, enclosures, fuses, wiring, and protection circuits. If you are unsure, consult a qualified electrician or experienced professional.
This library only handles switch debounce and stable switch state change detection.
It does not handle:
- Relay logic
- Wi-Fi
- Firebase
- Sensors
- MQTT
- Cloud communication
This keeps the library small, reusable, and easy to use in different projects.
This project is licensed under the MIT License — see the LICENSE file for details.
Manujaya Gunathilaka
GitHub: manujayagunathilaka
Contributions are welcome! Feel free to open an issue or submit a pull request.
