---
title: "Sandbox: debounce a switch, or design a timer"
heading: Choosing time constants for real jobs
eyebrow: C2 · Lesson 26 · Sandbox
xp: 60
---

::: hook
A mechanical switch doesn't close cleanly. Its contacts bounce for a millisecond or so, and a microcontroller watching it would count a single press as a dozen. An RC filter smooths the bounces away, but only if its time constant is long enough to ignore them and short enough that a real press still gets through promptly. Picking that τ, and picking parts for a timer or for protecting a transistor from a coil's kick, are the design problems in this sandbox. Each one is graded by simulating what you built.

::: play
- A bouncing switch is modeled as a fast square wave for the first few milliseconds. The RC filter's output should stay low during the bounce, then rise once the switch settles.
- Slide R and C. Find values where the bounce barely moves the output. How long does the output then take to rise?
:::
:::

```sim circuit
V1 in 0 square 0 5 1k Hz    @ 0,0 down
R1 in out 1k                @ 0,0 right
C1 out 0 1u                 @ 2,0 down
slider: R1 100..100k log
slider: C1 10n..10u log
scope: v(in), v(out)
time: 0..5m s
meter: v(out)
```

::: predict
## A debounce filter must ignore bounce lasting 2 ms but pass a real press within 30 ms. Which time constant fits?

- [ ] 0.1 ms
- [ ] 1 ms
- [x] 10 ms
- [ ] 100 ms

::: reveal
About 10 ms. Short time constants follow the bounce. During 2 ms of bounce, a 10 ms filter barely starts to move, then the steady press brings it to 95% within 3τ = 30 ms. With 100 ms, a press would take far too long to register.
:::
:::

::: nameit
## Designing with τ

**A timer.** A capacitor charging from 0 toward $V_s$ crosses a threshold $V_t$ at

$$t = RC\ln\frac{V_s}{V_s - V_t}$$

Pick C first (from the values you can buy, and large enough that stray capacitance doesn't matter), then solve for R. Keep R in the kilohms to megohms: too small wastes current, too large makes leakage currents matter.

**A debounce filter** has two requirements pulling in opposite directions:

- **ignore the bounce:** during bounce lasting $t_b$, the output must stay below the logic threshold, which roughly needs $\tau \gg t_b$;
- **respond promptly:** after a real press, the output must cross the threshold within the allowed time.

Microcontrollers usually add a **Schmitt trigger** input (two thresholds, C6) so the slowly rising output switches cleanly once.

**Protecting a switch from a coil.** A flyback diode clamps the kick near the supply but makes the coil release slowly. A resistor $R_s$ in series speeds the release and raises the spike to about $V_s + 0.7 + I_0R_s$. Choose the largest $R_s$ whose spike the transistor can survive.
:::

::: example
## A timer charges 1 µF toward 5 V and must cross 2.5 V after 10 ms. What resistor?

::: solution
1. **Time to cross 2.5 V.**
   $$\begin{aligned} t &= RC\ln\frac{5}{5 - 2.5} \\ &= RC\ln 2 \end{aligned}$$
2. **Solve for R.**
   $$\begin{aligned} R &= \frac{0.01}{10^{-6} \times 0.693}\ \Omega \\ &= 14.4\ \text{k}\Omega \end{aligned}$$
3. **Nearest standard part.** 15 kΩ from E24 gives 10.4 ms.

**Answer:** 14.4 kΩ; the E24 value of 15 kΩ gives 10.4 ms.
:::
:::

::: example
## A debouncer of 100 kΩ and 0.1 µF from 5 V: how long after a clean press does it reach 2.5 V, and how high can 2 ms of bounce push it?

::: solution
1. **Time constant.**
   $$\begin{aligned} \tau &= 100\ \text{k}\Omega \times 0.1\ \mu\text{F} \\ &= 10\ \text{ms} \end{aligned}$$
2. **After a clean press.** It crosses 2.5 V after
   $$\begin{aligned} t &= 10 \times \ln 2\ \text{ms} \\ &= 6.9\ \text{ms} \end{aligned}$$
3. **The worst bounce.** Even if the contact stayed closed through all 2 ms of bounce, the output would reach only
   $$\begin{aligned} v &= 5(1 - e^{-0.2})\ \text{V} \\ &= 0.9\ \text{V} \end{aligned}$$
   well below the threshold.

**Answer:** it reaches 2.5 V 6.9 ms after a clean press, and 2 ms of bounce can push it to only 0.9 V.
:::
:::

::: check
kind: design
given: Vt = 2 | 3 | 4 V
choose: R1, C1
goal: final(v(out)) = Vt ± 2%
solution: R1 = 10k; C1 = 0.01/ln(5/(5 - Vt))/10000

## Choose $R_1$ and $C_1$ so the capacitor reaches {Vt} exactly 10 ms after the 5 V step.

```sim circuit
V1 in 0 step 5      @ 0,0 down
R1 in out 1k        @ 0,0 right
C1 out 0 1u         @ 2,0 down
scope: v(in), v(out)
time: 0..10m s
meter: v(out)
```

::: right
Right. $RC\ln\frac{5}{5 - V_t}$ = 10 ms.
:::
:::

::: check
kind: design
choose: R1, C1
goal: peak(v(out)) < 1
solution: R1 = 10k; C1 = 1u

## Choose $R_1$ and $C_1$ so that 2 ms of contact bounce (modeled as a 1 kHz square wave) never lifts the output above 1 V.

```sim circuit
V1 in 0 square 0 5 1k Hz    @ 0,0 down
R1 in out 1k                @ 0,0 right
C1 out 0 100n               @ 2,0 down
scope: v(in), v(out)
time: 0..2m s
meter: v(out)
```

::: right
Right. The capacitor barely moves during the bounce; its time constant is long compared with the bounces.
:::
:::

::: check
kind: design
choose: R1, C1
goal: final(v(out)) > 4
solution: R1 = 10k; C1 = 1u

## The same filter must also let a steady press through: choose $R_1$ and $C_1$ so the output passes 4 V within 30 ms of a clean 5 V step.

```sim circuit
V1 in 0 step 5      @ 0,0 down
R1 in out 100k      @ 0,0 right
C1 out 0 1u         @ 2,0 down
scope: v(in), v(out)
time: 0..30m s
meter: v(out)
```

::: right
Right. A τ between about 4.3 and 18.6 ms satisfies both this and the bounce requirement.
:::
:::

::: check
kind: design
choose: RD
goal: peak(v(d)) < 30
solution: RD = 100

## A transistor switches a 100 mH, 100 Ω relay coil from 12 V. Choose the resistor $R_D$ in series with the flyback diode so the drain never goes above 30 V.

```sim circuit
VS vs 0 12                  @ 0,0 down 6
L1 vs x 100m                @ 3,0 down
RW x d 100                  @ 3,2 down
D1 d dd                     @ 5,4 up
RD dd vs 1M                 @ 5,2 up
DZ 0 d BV=60                @ 7,6 up 2
RLEAK d 0 100k              @ 10,4 down 2
M1 d g 0 nmos VTO=2 KP=50m  @ 2,5
VG g 0 square 0 10 100 Hz   @ 2,5 down 2
scope: v(d), v(g)
time: 0..10m s
meter: v(d)
```

::: right
Right. The spike is about $12 + 0.8 + 0.116 \times R_D$ volts (the diode drops nearly 0.8 V at this current), so anything up to about 150 Ω keeps it under 30 V and still releases faster than a bare diode.
:::
:::

::: explain
## Why does a debounce filter's time constant have both a lower and an upper limit?

::: model
If the time constant is too short, the capacitor charges and discharges quickly enough to follow the contact bounce, so the bounces still reach the input as multiple transitions. If it's too long, a genuine press takes too long to bring the output past the logic threshold, making the button feel sluggish or missing short presses. The useful range lies between the bounce duration and the acceptable response time.
:::
:::

::: practice

::: check
given: R = 10 | 47 | 100; C = 0.1 | 1 | 10; f = 0.5 | 0.8
answer: R*C*ln(1/(1 - f))
unit: ms
tolerance: 0.5%

## An RC timer has {R} kΩ and {C} µF. When does it reach {f} of the supply?

::: misses
- [R*C*f] The rise is exponential.
- [any] $RC\ln\frac{1}{1 - f}$.
:::
:::

::: check
given: t = 10 | 50 | 100; C = 1 | 10
answer: t/(C*ln(2))
unit: kΩ
tolerance: 0.5%

## A timer must reach half the supply at {t} ms using {C} µF. What R?

::: misses
- [t/C] Halfway takes $RC\ln 2$.
- [any] $R = t/(C\ln 2)$.
:::
:::

::: check
given: Vs = 12 | 24; I = 0.1 | 0.2; Rs = 50 | 100
answer: Vs + 0.7 + I*Rs
unit: V
tolerance: 1%

## A {Vs} V coil carrying {I} A turns off into a diode with {Rs} Ω in series. The switch sees?

::: misses
- [Vs + 0.7] Add the resistor's drop.
- [any] $V_s + 0.7 + IR_s$.
:::
:::
:::
