Debouncing

Inside a tactile switch are metal contacts that go to the legs, and then a domed disc that sits on top, when pressed, the dome collapses down and connects the two contacts.

Below you can see the switch body, silver metal disc, black switch cap and the metal square that holds the switch together

The metal disc is springy/bouncy. When you press it down, it wont make immediate contact, it will shift around a little bit!

If you are relying on a switch activation for something exact, you may find that the mechanical switch may make contact on and off for a few milliseconds before settling down to the state you want. This is called switch bounce and is considered unwanted noise at best, a failure in some very fast circuits.

The bounces don't occur when the button is held down or not pressed. They only occur during the press or release of a button.

Here are some tricks to use code to debounce a switch (so you don't have to add more hardware).

MakeCode

Internally, MakeCode handles debouncing, so you don't have to do anything more for a noisy switch!

CircuitPython

CircuitPython is interpreted so it doesn't run incredibly fast even in a short polling for a switch press loop. This is to our advantage with debouncing, as the timing on reading an input and any follow-up code should be long enough to not detect any switch noise. If you don't poll the switch less than 10 milliseconds apart (hard to do!) you're fine.

If you somehow get a really fast processor and you are trying to catch presses very fast, you might do something like:

Download: file
if switch.value:
    time.sleep(0.05)  // wait 50 ms see if switch is still on
    if switch.value:
        # ok, it's really pressed

but again, this is rarely necessary, if ever.

Arduino

Arduino code does run fast enough where you might poll a switch multiple times within a few milliseconds. So the code might catch the highs and lows of switching noise.

The software debounce can be done a number of ways but there is an example in the standard IDE installation which is listed below. I've modified it for a pullup inside the microcontroller and the pin to A1 like other examples.

Download: file
/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin A1 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = A1;   // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;  // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

A Quick Note on Hardware Debouncing

Hardware costs money so many manufacturers do not want to spend extra money for hardware to debounce switches. A 0.1 uF capacitor across the switch contacts is often used in simple circuits with better hardware used depending on the circuit requirements. Hardware debouncing is its own art and you'll find other guides on the Internet and in books discussing it.

It is worth it to put your homework into hardware switch debouncing if you really require a near-perfect switch transition detection.

0.1uF ceramic capacitors - 10 pack

PRODUCT ID: 753
As any engineer will tell you, 0.1uF ceramic capacitors are the magic fairy dust of electronics - sprinkle them over your circuit schematics for good luck! But seriously, a 0.1uF...
$1.95
IN STOCK
This guide was first published on Aug 15, 2018. It was last updated on Aug 15, 2018.
This page (Debouncing) was last updated on Aug 08, 2020.