Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SmartSwitchDebounce Banner

Arduino ESP32 License: MIT Version

SmartSwitchDebounce

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.

Features

  • Uses INPUT_PULLUP mode
  • 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

Why This Library?

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.

Wiring

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.

Installation

Option 1: Manual Installation

  1. Download or clone this repository.
  2. Copy the full SmartSwitchDebounce folder into your Arduino libraries folder.
  3. Restart Arduino IDE.
  4. 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

Option 2: Install Using Arduino IDE ZIP Import

  1. Download this repository as a ZIP file.
  2. Open Arduino IDE.
  3. Go to:
Sketch → Include Library → Add .ZIP Library...
  1. Select the downloaded ZIP file.
  2. Restart Arduino IDE if needed.

Basic Usage

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

Toggle Relay Example

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

API Reference

SmartSwitchDebounce(int pin, unsigned long debounceDelay = 75)

Creates a SmartSwitchDebounce object.

Parameters:

  • pin - GPIO pin connected to the switch
  • debounceDelay - debounce delay in milliseconds

Example:

SmartSwitchDebounce mySwitch(14);

Or with a custom debounce delay:

SmartSwitchDebounce mySwitch(14, 100);

begin()

Initializes the switch pin using INPUT_PULLUP mode and stores the initial switch state.

Example:

mySwitch.begin();

changed()

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

currentState()

Returns the latest stable switch state.

Return values:

HIGH → Switch open
LOW  → Switch closed

Example:

if (mySwitch.currentState() == LOW) {
  Serial.println("Switch is closed");
}

isPressed()

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

Included Examples

BasicSwitchTest

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.

ToggleRelay

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.

Supported Boards

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()

Example Use Cases

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

Safety Note

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.

Notes

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.

License

This project is licensed under the MIT License — see the LICENSE file for details.

Author

Manujaya Gunathilaka

GitHub: manujayagunathilaka

Contributions are welcome! Feel free to open an issue or submit a pull request.

About

Arduino library for debouncing switches and detecting stable switch state changes.

Topics

Resources

Security policy

Stars

11 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages