Sub topic 11
Hardware Debouncing: RC Filters and Schmitt Triggers
Assume you already know how to wire a button, configure an Arduino pin, and use digitalRead(). This chapter focuses on circuits that reduce contact bounce before the signal reaches your sketch.
The RC debounce circuit
An RC circuit uses a resistor and a capacitor. Together, they form a low-pass filter. A low-pass filter changes quickly varying signals more than slowly varying signals.
For an active-low button, wire the circuit like this:
- Connect a resistor from 5 V to the input pin.
- Connect the capacitor from the input pin to GND.
- Connect the button from the input pin to GND.
- Configure the pin as an input.
When the button is released, the resistor charges the capacitor toward 5 V. When the button is pressed, the button discharges the capacitor toward GND. Brief voltage changes from contact bounce are slowed by the capacitor.
The key value is the time constant:
\[ \tau = R \times C \]
The time constant, written as tau, is the approximate time scale of the voltage change. A 10 kΩ resistor and a 100 nF capacitor produce a time constant of about 1 ms. A 47 kΩ resistor and a 100 nF capacitor produce about 4.7 ms.
A larger time constant filters more bounce, but it also makes the button respond more slowly. The resistor and capacitor must be chosen together.
What the Arduino pin sees
An RC circuit produces a gradual voltage ramp instead of a sharp digital transition. The Arduino input changes from LOW to HIGH when that voltage crosses its input threshold. The exact threshold depends on the board and microcontroller.
This creates two important failure modes:
- A capacitor that is too small may allow bounce through.
- A capacitor that is too large may keep the input near its threshold for too long.
A slowly changing input can also be sensitive to electrical noise. While the voltage is between LOW and HIGH thresholds, the input's interpretation may be uncertain.
The Arduino's internal pull-up resistor can sometimes be used:
const uint8_t BUTTON_PIN = 2;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
Serial.println(pressed ? "pressed" : "released");
}With INPUT_PULLUP, the internal resistor and an external capacitor form the RC circuit. This saves a component, but the internal resistor has a wide tolerance. Use an external resistor when the timing must be predictable.
Adding a Schmitt trigger
A Schmitt trigger is a digital input with hysteresis. Hysteresis means it uses one voltage threshold when the signal rises and a different threshold when the signal falls.
This prevents small voltage changes near a threshold from rapidly changing the output. An RC filter slows the signal, and the Schmitt trigger converts the slow, filtered signal back into a clean digital edge.
Some microcontroller inputs include hysteresis, but you should not assume every board has the same behavior. An external Schmitt-trigger buffer, such as a logic device designed for the circuit's supply voltage, makes the behavior more explicit.
A Schmitt trigger does not replace the RC filter. It rejects threshold noise, but a long sequence of real voltage changes can still produce several output transitions. The RC values must still match the button and the required response time.
Choosing hardware or software
| Approach | Strength | Limitation |
|---|---|---|
| RC filter | Simple and inexpensive | Adds response delay and component tolerances |
| RC plus Schmitt trigger | Produces cleaner digital edges | Needs more parts and correct supply voltage |
| Software debounce | Easy to change and test | The raw pin can still see noisy transitions |
Use software when the input is local and timing is not critical. Use hardware when the signal travels through a noisy environment, must be clean during startup, or is shared by several systems. A hybrid design is often useful: hardware limits electrical noise, while software defines what counts as a press.
Exercise
An active-low button uses a 10 kΩ pull-up resistor and a 100 nF capacitor. What is the approximate RC time constant, and what happens if the capacitor is made much larger?
Show answer
The time constant is about 1 ms because 10,000 Ω × 0.0000001 F = 0.001 s. A much larger capacitor filters more bounce but makes the input change more slowly and can delay button response.