Skip to content

Arduino button debounce library for various switch types, port expanders and other 8-bit data sources. Fast and robust debounce algorithm.

License

Notifications You must be signed in to change notification settings

Dlloydev/Toggle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toggle arduino-library-badge PlatformIO Registry

Arduino library for deglitching and debouncing signals, buttons and switches. One or two inputs can be monitored to decode transistions (depicting direction) for SPST, SPDT or SP3T contacts. Simple to use and requires very little memory. Captures one-shot transitions (depicting direction) and current status.

image

Features

  • Performs both debouncing and deglitching.
  • Works with signals, stored data, buttons and switches.
  • External pull-up resistors not required.
  • Very simple to use.
  • Ultra low memory use.

Using Toggle

Simple to use because pinMode, input pullups, de-glitch period, bounce period, and switch type (1 or 2 inputs) is automatically detected and configured.

Declaring a switch or button using 1 digital pin for SPST or SPDT contacts:

Toggle sw1(6); // GPIO 6

Declaring a switch or button using 2 digital pins for SP3T contacts:

Toggle sw2(7, 8); // GPIO 7 and 8

Each switch is polled in the loop() function:

sw1.poll();
sw2.poll();

The switch functions when using 1 input pin:

bool isOFF();
bool isON();
bool OFFtoON();
bool ONtoOFF();

The switch has 2 positions referred to as OFF (input is high) and ON (input is low). The first 2 functions will continuously return true if the switch is at that current position. The last 2 functions return true (once only) if the switch has just transitioned to the checked position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.

The switch functions when using 2 input pins:

bool isUP();
bool isMID();
bool isDN();
bool UPtoMID();
bool MIDtoDN();
bool DNtoMID();
bool MIDtoUP();

The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 functions will continuously return true if the switch is at that position. The last 4 functions return true (once only) if the switch has just transitioned to that position. This is very handy to execute code based on direction of switched operation or for any one-shot processing of code.

Other functions:

void setInputMode(inMode inputMode);

// options:
sw1.setInputMode(sw1.inMode::input_input);     // high impedance input
sw1.setInputMode(sw1.inMode::input_pullup);    // pullup resistor enabled (default)
sw1.setInputMode(sw1.inMode::input_pulldown);  // not used 
sw1.setInputMode(sw1.inMode::input_logic);     // uses a byte variable for input data

void setSampleUs(uint16_t sampleUs);

// default sample period is 5000μs, range is 0-65535μs (0-65ms)
// using 8 samples, the range for debounce will be (0-524ms)
sw1.setSampleUs(5000); //μs

Example Sketch:

This sketch checks the status of a SP3T and a basic SPST switch or button:

#include <Toggle.h>

Toggle sw1(6,7);
Toggle sw2(8);

void setup() {
  while (!Serial) { }; // Leonardo
  Serial.begin(115200);
}

void loop() {
  sw1.poll();
  sw2.poll();

  if (sw1.UPtoMID) Serial.println(F("sw1: UP⇒MID"));
  if (sw1.MIDtoDN) Serial.println(F("sw1: MID⇒DN"));
  if (sw1.DNtoMID) Serial.println(F("sw1: DN⇒MID"));
  if (sw1.MIDtoUP) Serial.println(F("sw1: MID⇒UP"));

  if (sw2.OFFtoON) Serial.println(F("sw2: OFF⇒ON"));
  if (sw2.ONtoOFF) Serial.println(F("sw2: ON⇒OFF"));
}

Switch Connections

Switching between GND and digital pin is the default solution used by this library. External pull-up resistors can be added if desired, but the internal pullups should be sufficient for most applications. What might be of consideration is providing sufficient wetting current to overcome switch contact oxidation.

A set of connections are shown where 0.1μF capacitors are optionally added to provide the following benefits:

  • contact wetting current
  • hardware signal filtering
  • beneficial for interrupt applications
  • improves noise immunity when using longer cables

Connections shown below are for 2-position switches (SPST, SPDT, DPDT) using 1 input:

image

image

Connections shown below are for 3-position switches (SP3T, DP3T) using 2 inputs:

Toggle Switch (SP3T, On-Off-On):

image

Slide Switch (Single Pole, 3-position):

image

Rotary Switch (Double Pole, 3-position):

image

Rocker Switch (SP3T, On-Off-On):

image

Polling

The switch inputs are polled in the main loop and the history of readings is stored in an 8-bit shift register (byte) where bit0 is the present reading and bit7 is the oldest reading. Data is shifted and updated every 5ms.

Deglitching

Using the input pullups provides a high 20K-50K impedance that makes the signals more susceptible to noise pickup, especially if longer cables are used in a noisy environment. To improve this possible situation, software deglitching is internally set to 1 read sample which represents a minimum period of 5ms that any reading change is ignored.

Debouncing

Debouncing requires the shift register to be completely filled with 1's or 0's to signify a stable state. This occurs 40ms after bouncing stops. Contact closure will be detected after 2 stable samples (readings) are made. This allows single sample anomalies to be ignored (deglitched). Contact release is detected when 8 stable samples (readings) have been made.

Memory Comparison on Leonardo with 2 buttons attached :

Library Version Bytes Bytes Used
Empty sketch -- 149 --
JC_Button.h 2.1.2 186 37
Toggle.h 2.2.0 190 41
Bounce2.h 2.71.0 193 44
AceButton.h 1.9.2 205 56
ezButton.h 1.0.3 331 182

References