CS 362 - Computer Design

Brooklyn Debounce

The following is a discussion of the Brooklyn Debounce solution as originally presented in https://www.ladyada.net/learn/arduino/lesson5.html

Problem: Switch Bounce

As a switch closes the contact can occur multiple times before settling into the closed state. See Contact Bounce at https://en.wikipedia.org/wiki/Switch#Contact_bounce

The following is picture of an oscilloscope showing this contact bounce as a switch closes.


There are a number of solutions to solve switch/contact bounce. The following is a software solution that can be used with an Arduino that has been referred to as the Brooklyn Debounce.

The assumption is that a digital input pin on the Arduino is connected to a pull-down resistor as shown below:


This schematic is from the Arduino Button Tutorial: https://www.arduino.cc/en/Tutorial/Button

The solution for the Brooklyn Debounce is to read an input value two times separated by a short delay. The code used a delay of 10 milliseconds. The thought is that any switch bounce should settle during that time. If the values read in are not the same, the assumption is the switch is still "bouncing" and we should ignore those two input values and re-read them.

The oscilloscope picture above shows that the switch bounce has in about 0.5 milliseconds. So the delay of 10 milliseconds should be longer that the time it takes for any switch/contact bounce to settle.

The Brooklyn Debounce code also keep track of a "Current State" value of the button in a global variable. So the two read values are just verifying that no bouncing has occurred. The checking to see if the state of the button has changed comes next. The Brooklyn Debounce code will first make sure the two reads have the same value and then checks if this value matches the current state of the button. If the value does not match, then we know the button state has truly changed and hat we are not in the midst of a switch/contact bounce. Only then can the perform whatever task is needed to reflect the new state of the button.

Code for Brooklyn Debounce:   BrooklynDebounceTemplate.ino

// Global variables
int switchPin = 2;              // switch is connected to pin 2
int buttonState;                // variable to hold the button state

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  buttonState = digitalRead(switchPin);   // read the initial state

  // Other code needed for setup() 

void loop(){
  int val;                        // variable for reading the pin status
  int val2;                       // variable for reading the delayed/debounced status

  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      // perform needed task for change of button state

    buttonState = val;                 // save the new state in our variable

Topic attachments
I Attachment Action Size Date Who Comment
Unknown file formatino BrooklynDebounceTemplate.ino manage 1.1 K 2019-09-03 - 19:44 UnknownUser  
PNGpng button_schem_pull_down_.png manage 18.7 K 2019-09-03 - 18:47 UnknownUser  
JPEGjpg switchbounce.jpg manage 78.3 K 2019-09-03 - 18:42 UnknownUser  
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF