Projects / Button debounce comparison

Button debounce comparison

Beginner2.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNO

    Wire the button

    Node N through the button to GND, 10 kilohm pull-up to 5V.

  2. UNO

    Wire D2

    Node N straight to D2.

  3. UNO

    Build the RC stage

    10 kilohm from N to D3, 100 nanofarad from D3 to GND.

  4. UNO74HC14

    Build the second RC stage

    Another 10 kilohm and 100 nanofarad feeding the 74HC14 pin 1.

  5. UNO74HC14

    Wire the Schmitt gates

    Pin 2 to pin 3, pin 4 to D4, power on 14 and 7, unused inputs to GND.

  6. UNO

    Upload the sketch

    Counters start at zero.

  7. UNO

    Press the button 20 times

    Note each counter.

  8. UNO

    Compare

    Raw is high, the others are close to 20.

In short

How do you build a button debounce comparison?

Compare software debouncing, an RC filter and a Schmitt trigger side by side on the same button. You need Arduino Uno R3, Tactile push button 6 mm, Resistor 10 kΩ × 3, Ceramic Capacitor 100 nF × 2, 74HC14 hex Schmitt-trigger inverter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 150 minutes.

Level
Beginner
Time
About 150 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ButtonBetween node N and GNDThe test button
10 kilohmFrom 5V to node NPull-up
Node NUno D2Raw reading, also used for the software method
Node N through 10 kilohmUno D3 and a 100 nanofarad capacitor to GNDRC filtered reading
Node N through 10 kilohm and 100 nanofarad74HC14 input pin 1Filtered node for the Schmitt gate
74HC14 pin 2Pin 3First inverter output into second inverter
74HC14 pin 4Uno D4Clean output, same polarity as the button
74HC14 pins 14 and 75V and GNDPower, tie unused inputs 5, 9, 11 and 13 to GND

Code

A mechanical button does not close cleanly: the contacts bounce for a millisecond or several, so a fast microcontroller sees a burst of presses. This lab feeds one button to four counters so you can see the result. The raw count counts every edge and usually reads two to ten times the real number of presses. The software method ignores changes for 30 milliseconds after each edge and gives the right answer cheaply. The RC filter, a 10 kilohm resistor and 100 nanofarad capacitor, smooths the bounce into a slow ramp, but a slow ramp hovering near the input threshold can still make a standard input chatter. The 74HC14 Schmitt trigger has two thresholds, so once the ramp crosses them the output switches cleanly and stays switched. Two inverters in series give back the original polarity. The Schmitt output is the cleanest, and needs no code at all, which is helpful when the processor is busy or sleeping. Make 20 presses and compare the four numbers.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int RAW = 2;      // button node, no filter
const int RC = 3;       // after the RC filter
const int SCH = 4;      // after RC and the Schmitt trigger

unsigned long rawCount = 0, softCount = 0, rcCount = 0, schCount = 0;
bool lastRaw = HIGH, lastSoft = HIGH, lastRc = HIGH, lastSch = HIGH;
unsigned long lastSoftEdge = 0;
unsigned long lastPrint = 0;

void setup() {
  pinMode(RAW, INPUT);   // external 10 kilohm pull-up
  pinMode(RC, INPUT);
  pinMode(SCH, INPUT);
  Serial.begin(9600);
  Serial.println("Press the button a few times. Each press should count once.");
}

void loop() {
  unsigned long now = millis();

  bool r = digitalRead(RAW);
  if (r != lastRaw) {                   // method 1: count every falling edge, bounce included
    if (r == LOW) rawCount++;
    lastRaw = r;
  }

  bool s = digitalRead(RAW);            // method 2: software debounce on the same pin
  if (s != lastSoft && now - lastSoftEdge > 30) {
    lastSoftEdge = now;
    if (s == LOW) softCount++;
    lastSoft = s;
  }

  bool c = digitalRead(RC);             // method 3: RC filter only
  if (c != lastRc) { if (c == LOW) rcCount++; lastRc = c; }

  bool h = digitalRead(SCH);            // method 4: RC then Schmitt trigger
  if (h != lastSch) { if (h == LOW) schCount++; lastSch = h; }

  if (now - lastPrint > 1000) {
    lastPrint = now;
    Serial.print("raw "); Serial.print(rawCount);
    Serial.print("  software "); Serial.print(softCount);
    Serial.print("  RC "); Serial.print(rcCount);
    Serial.print("  Schmitt "); Serial.println(schCount);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Press the button a few times. Each press should count once.
raw 1  software 1  RC 1  Schmitt 1

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
20 presses, rawOften 25 to 60
20 presses, software20
20 presses, RC onlyClose to 20, maybe a few extra
20 presses, Schmitt20
Hold the buttonNo extra counts

If it does not work

Counters all stay zero

Check the pull-up and the wiring to the pins.

Schmitt count is zero

Check 74HC14 power and the unused inputs.

Software count is high

Raise the 30 millisecond lock-out.

RC count too low

Capacitor too large, try 22 nanofarad.

Counts jump while idle

A floating input, check every wire.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the button debounce comparison?

Arduino Uno R3, Tactile push button 6 mm, Resistor 10 kΩ × 3, Ceramic Capacitor 100 nF × 2, 74HC14 hex Schmitt-trigger inverter, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the button debounce comparison take to build?

About 150 minutes for a beginner maker, in 8 steps.

How do I know the button debounce comparison works?

20 presses, raw: Often 25 to 60. 20 presses, software: 20. 20 presses, RC only: Close to 20, maybe a few extra.

Counters all stay zero: what should I check?

Check the pull-up and the wiring to the pins.

Schmitt count is zero: what should I check?

Check 74HC14 power and the unused inputs.

Software count is high: what should I check?

Raise the 30 millisecond lock-out.

RC count too low: what should I check?

Capacitor too large, try 22 nanofarad.