Sub topic 11
Hardware Debouncing: RC Filters and Schmitt Triggers
This chapter assumes you have read the earlier chapters on contact bounce, INPUT_PULLUP and inverted logic, and software debouncing with millis(). You should also be comfortable with basic circuit ideas: voltage, resistance, and what a capacitor does. Hardware debouncing is the alternative to the timestamp code you have already written. It removes the bounce before the signal reaches the microcontroller pin.
Why consider hardware at all
A software debounce costs a few variables and a comparison. It is usually the right default. Hardware debouncing is worth considering when:
- The input must be clean even if the firmware is busy, stalled, or still booting.
- The signal drives an interrupt or a counter, where a spurious edge cannot be filtered after the fact.
- Many inputs share a small microcontroller and you want to save CPU time and code.
- You are debouncing a signal that is not a simple button, such as a relay contact or a rotary encoder.
The cost is extra parts, board space, and added latency. The sections below cover the two building blocks, an RC filter and a Schmitt trigger, and how they work together.
The RC low-pass filter
A resistor and capacitor wired together form an RC filter. A capacitor stores charge, so the voltage across it cannot change instantly. When the input voltage jumps, the capacitor voltage follows along an exponential curve. Fast glitches barely move it, while a change that lasts long enough moves it all the way.
The speed is set by the time constant:
tau = R * CAfter one time constant, a charging capacitor has covered about 63% of the distance to its final voltage. After about five time constants it has effectively arrived. The charging voltage is:
V(t) = Vcc * (1 - e^(-t / tau))Typical bounce lasts from under 1 ms to about 10 ms, depending on the switch. A filter with a time constant of a few milliseconds smooths the bounce into one slow ramp.
The basic circuit
Put a capacitor in parallel with the button, between the input node and ground. The pull-up resistor charges it, and the closed button discharges it.
- Button released: the pull-up resistor charges the capacitor toward Vcc. The pin reads HIGH once the voltage crosses the input's HIGH threshold.
- Button pressed: the closed contact shorts the capacitor to ground. The pin reads LOW almost immediately.
The two directions are not symmetric. Release is slowed by the RC time constant, while the press edge is fast. That still helps with press bounce. The first closure discharges the capacitor fully. If the contacts then open briefly, the resistor has only microseconds to recharge it, and the voltage barely rises before the contacts close again. The pin never sees a HIGH level.
Choosing values
Take a 10 kΩ external pull-up. The time to rise to 60% of Vcc, a typical HIGH threshold for 5 V AVR boards, is about 0.92 × tau.
| Capacitor | tau (10 kΩ) | Time to reach 0.6 Vcc | Typical use |
|---|---|---|---|
| 10 nF | 0.1 ms | about 0.09 ms | Too fast for most switches |
| 100 nF | 1 ms | about 0.9 ms | Good quality tactile switches |
| 1 µF | 10 ms | about 9 ms | Noisy or worn switches |
Start with 100 nF and 10 kΩ, test with a real button, and increase the capacitor only if you still see double events. Larger values add latency on release, and a long press-to-register delay makes the interface feel sluggish.
The series resistor
A bare capacitor discharged through a switch produces a brief current spike, limited only by the contact resistance. Over thousands of presses this can pit the contacts. Add a small resistor, 100 Ω to 1 kΩ, in series between the capacitor and the switch node. It limits the peak current to Vcc divided by that resistance, and it slows the discharge to a time constant of R_series × C. That is still very short.
A second resistor between the switch node and the capacitor also lets you filter both edges. The capacitor then charges through the pull-up plus the series resistor, and discharges through the series resistor alone.
The problem with slow ramps
An RC filter produces a slowly changing voltage, and microcontroller inputs are designed for fast edges. The input has two thresholds:
- A voltage below the LOW threshold, VIL, reads as LOW.
- A voltage above the HIGH threshold, VIH, reads as HIGH.
- A voltage between them is undefined.
On an ATmega328P at 5 V, the datasheet gives VIL as 0.3 × Vcc (1.5 V) and VIH as 0.6 × Vcc (3 V). A slow ramp spends a long time in the 1.5 V to 3 V band. Small amounts of electrical noise can then push the pin across its internal switching point several times, so the filter that removed bounce can create chatter of its own. Slow edges can also increase current draw in the input buffer, because both of its transistors conduct partly while the voltage is in the undefined zone.
The Schmitt trigger
A Schmitt trigger is a comparator with hysteresis. Hysteresis means it uses two different switching thresholds depending on the current output:
- The output goes HIGH only when the input rises above the upper threshold, VT+.
- The output goes LOW only when the input falls below the lower threshold, VT-.
- Between VT- and VT+, the output keeps its previous value.
A noisy ramp must now swing across the whole gap to cause a second transition. If the gap is wider than the noise, the output changes exactly once, and it changes quickly even when the input ramps slowly.
Using a 74HC14
The 74HC14 is a common hex inverting Schmitt trigger: six independent gates in one 14-pin package. The 74HC132 is a similar part with NAND gates. Both are inexpensive and work at Vcc from about 2 V to 6 V, so they suit both 5 V and 3.3 V boards. Their hysteresis is typically around 1 V, but the exact thresholds vary with supply voltage and between manufacturers, so check the datasheet for your part.
A complete circuit for one button:
- Connect the button between the filter node and ground.
- Connect a 10 kΩ resistor from the filter node to Vcc.
- Connect a 100 nF capacitor from the filter node to ground.
- Connect the filter node to a gate input of the 74HC14.
- Connect the gate output to the Arduino input pin.
- Add a 100 nF decoupling capacitor between Vcc and ground close to the chip.
Because the 74HC14 inverts, the logic flips back. An idle button gives a LOW at the Arduino pin, and a pressed button gives a HIGH. This is the opposite of INPUT_PULLUP wiring, so name the polarity in your code instead of scattering LOW and HIGH checks.
Reading the clean signal
With a clean signal, the sketch needs no debounce timer. Only edge detection remains:
const uint8_t BUTTON_PIN = 2;
const bool PRESSED_LEVEL = HIGH; // 74HC14 inverts: pressed reads HIGH
int lastReading = !PRESSED_LEVEL;
bool ledState = LOW;
void setup() {
pinMode(BUTTON_PIN, INPUT); // the external circuit drives the pin
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastReading = reading;
if (reading == PRESSED_LEVEL) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
}
}
}The pin uses plain INPUT because the Schmitt gate drives it actively. An internal pull-up would only add a weak extra load. Compare this with the earlier software debounce sketch. The millis() bookkeeping is gone, and the sketch reacts once per press.
Using the microcontroller's internal pull-up
You can build the RC filter with INPUT_PULLUP and no external resistor. The catch is that the AVR internal pull-up is specified loosely, from about 20 kΩ to 50 kΩ. With 100 nF, tau then ranges from 2 ms to 5 ms between chips. That range is acceptable for a hobby project, but a fixed external resistor gives repeatable timing. Without a Schmitt trigger, the slow-ramp chatter described above also still applies.
Some microcontrollers have Schmitt-trigger input buffers built in, but the hysteresis is often small and not specified as a guaranteed limit. Do not rely on it to clean up a slow RC ramp unless the datasheet guarantees enough hysteresis.
Dedicated debounce ICs
If you want a single part instead of discrete components, debounce chips such as the MC14490 (six channels) or the MAX6816 family (one to a few channels) integrate the timing and the switching logic. They give a repeatable debounce time without choosing R and C. They cost more than a resistor, a capacitor, and a shared 74HC14 gate, so they appear mainly in products where tolerances matter.
Choosing between approaches
| Approach | Strengths | Weaknesses |
|---|---|---|
| Software with `millis()` | No parts, tunable in code | Needs CPU time and polling; useless for noisy interrupt lines |
| RC filter only | Two cheap parts | Slow edge can chatter at the threshold; asymmetric timing |
| RC plus Schmitt trigger | Clean single edge; works with interrupts | Extra IC, board space, inverted logic |
| Debounce IC | Fixed, documented timing | Highest cost per channel |
Hardware debouncing does not make software debouncing wrong. Many designs use both: a small RC to remove high-frequency noise, and a short software check for the rest.
Common errors
- Forgetting the inversion. A 74HC14 output is the opposite of its input, so your pressed level changes from LOW to HIGH.
- Making tau too long. A 1 µF capacitor with a 100 kΩ resistor takes about 92 ms to register a release, and fast double-presses disappear.
- Placing the capacitor far from the input. Long wires add noise pickup. Keep the RC network close to the Schmitt trigger input.
- Skipping decoupling. The 74HC14 switching can cause supply glitches that look like extra bounce.
- Mixing voltages. A 5 V Schmitt output driving a 3.3 V pin can overstress it. Power the gate from the same supply as the microcontroller.
Exercise
A button is wired with a 4.7 kΩ pull-up resistor and a 220 nF capacitor in parallel with it. (1) What is the time constant? (2) About how long after release does the node voltage reach 0.6 × Vcc? (3) If the 4.7 kΩ pull-up is replaced by a 74HC14 gate in the signal path, what does the Arduino read when the button is pressed?
Show answer
(1) tau = R × C = 4700 Ω × 220 nF = 4700 × 0.00000022 s ≈ 1.03 ms. (2) The time to reach 60% of Vcc is tau × -ln(1 - 0.6), about 0.92 × 1.03 ms ≈ 0.95 ms. (3) The 74HC14 inverts, so a pressed button (node pulled LOW) gives a HIGH at the Arduino pin, and the sketch must treat HIGH as pressed.
Exercise
Why does an RC filter without a Schmitt trigger sometimes still produce multiple events, even though the capacitor smooths the bounce?
Show answer
The filtered voltage rises slowly and spends a long time between VIL and VIH, where the pin's input buffer has no guaranteed reading. Noise can push the voltage across the buffer's switching point several times. A Schmitt trigger uses two separated thresholds, so noise smaller than the hysteresis gap cannot cause extra transitions.