C2 · Lesson 26 · Sandbox
Choosing time constants for real jobs
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?
Predict
A debounce filter must ignore bounce lasting 2 ms but pass a real press within 30 ms. Which time constant fits?
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.
Name it
Designing with τ
A timer. A capacitor charging from 0 toward crosses a threshold at
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 , the output must stay below the logic threshold, which roughly needs ;
- 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 in series speeds the release and raises the spike to about . Choose the largest whose spike the transistor can survive.
Worked example
A timer charges 1 µF toward 5 V and must cross 2.5 V after 10 ms. What resistor?
Show the solution
- Time to cross 2.5 V.
- Solve for R.
- 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.
Worked 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?
Show the solution
- Time constant.
- After a clean press. It crosses 2.5 V after
- The worst bounce. Even if the contact stayed closed through all 2 ms of bounce, the output would reach only 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
Choose and so the capacitor reaches 3 V exactly 10 ms after the 5 V step.
Type values the way you would write them on a schematic: 4.7k, 100n, 10m. Then try them: the circuit runs with your values and is measured.
Check
Choose and so that 2 ms of contact bounce (modeled as a 1 kHz square wave) never lifts the output above 1 V.
Type values the way you would write them on a schematic: 4.7k, 100n, 10m. Then try them: the circuit runs with your values and is measured.
Check
The same filter must also let a steady press through: choose and so the output passes 4 V within 30 ms of a clean 5 V step.
Type values the way you would write them on a schematic: 4.7k, 100n, 10m. Then try them: the circuit runs with your values and is measured.
Check
A transistor switches a 100 mH, 100 Ω relay coil from 12 V. Choose the resistor in series with the flyback diode so the drain never goes above 30 V.
Type values the way you would write them on a schematic: 4.7k, 100n, 10m. Then try them: the circuit runs with your values and is measured.
Explain why
Why does a debounce filter's time constant have both a lower and an upper limit?
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
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.