Excellent question! This is a fundamental technique in electronics called an RC (Resistor-Capacitor) filter, and its placement is key to making it work.
The Short Answer (TL;DR)
You place the 100nF capacitor directly between the microcontroller's input pin and ground (for the most common setup).
It works in parallel with the switch, but from the perspective of the microcontroller pin.
The Detailed Explanation (with Diagrams)
To debounce a button, you need both a resistor and a capacitor. The placement depends on whether you are using a pull-up or pull-down resistor configuration. The pull-up configuration is far more common.
Scenario 1: Using a Pull-Up Resistor (Most Common)
In this setup, the input pin is "pulled up" to a HIGH state by a resistor. Pressing the button connects the pin to Ground, pulling it LOW.
- Resistor (R1): Connects the MCU input pin to VCC (e.g., 5V or 3.3V). A typical value is 10kΩ.
- Button: Connects the MCU input pin to Ground (GND).
- Capacitor (C1): Connects the MCU input pin to Ground (GND). It sits in parallel with the button.
Circuit Diagram (ASCII):
VCC (+5V)
|
R1 (10kΩ)
|
+------> To MCU Input Pin
|
--- C1 (100nF)
---
|
+----o o---- Button
| |
GND GND
How it works:
- Button Open (Not Pressed): The capacitor is charged up to VCC through the resistor R1. The MCU pin reads HIGH.
- Button Pressed: The button contacts bounce.
- On the first contact, the button shorts the capacitor to ground, rapidly discharging it. The MCU pin reads LOW.
- As the contacts bounce open for a millisecond, the pin doesn't immediately fly back to HIGH. The capacitor needs to recharge through R1, which is slow.
- Before the capacitor's voltage can rise high enough to be read as HIGH again, the button contact closes again, keeping it at LOW.
- Result: The capacitor effectively "smooths out" the rapid voltage spikes from the bouncing, presenting a clean HIGH-to-LOW transition to the MCU.
Scenario 2: Using a Pull-Down Resistor (Less Common)
In this setup, the input pin is "pulled down" to a LOW state by a resistor. Pressing the button connects the pin to VCC, pulling it HIGH.
- Resistor (R1): Connects the MCU input pin to Ground (GND). A typical value is 10kΩ.
- Button: Connects the MCU input pin to VCC.
- Capacitor (C1): Connects the MCU input pin to VCC.
Circuit Diagram (ASCII):
VCC (+5V)
|
+----o o---- Button
|
+------> To MCU Input Pin
|
--- C1 (100nF)
---
|
VCC
|
R1 (10kΩ)
|
GND
Note: This diagram is a bit tricky in ASCII. The capacitor is in parallel with the resistor, going from the MCU pin to ground.
Let's try a better diagram for the pull-down case:
VCC (+5V)
|
o
| Button
o
|
+------> To MCU Input Pin
|
+--+--+
| |
--- R1 (10kΩ)
C1 --- |
100nF | |
| |
GND GND
How it works: The principle is the same, but the logic is inverted. The capacitor holds the pin LOW and must be charged through the button press. This configuration is less common because many microcontrollers (like Arduino, ESP32) have convenient built-in pull-up resistors you can enable in software, simplifying your hardware.
Important Considerations
- Component Values: The combination of
R1=10kΩ and C1=100nF is a classic and very effective recipe. It creates a time constant (τ = R * C) of 1ms, which is usually perfect for debouncing human button presses.
- Physical Placement: For best results, place the capacitor and resistor as close to the microcontroller's input pin as possible on your PCB or breadboard. This minimizes the length of the "antenna" that could pick up electrical noise.
- Using Internal Pull-ups: If your microcontroller has internal pull-up resistors (most do), you can enable them in your code and simplify the external circuit. You would only need the button and the capacitor.
Circuit with Internal Pull-up:
(Internal Pull-up in MCU)
|
+------> MCU Input Pin
|
--- C1 (100nF)
---
|
+----o o---- Button
| |
GND GND
This is the simplest and often the best hardware solution. You just need a capacitor from the input pin to ground, and a switch from the input pin to ground.