Embedded Systems & Firmware
Which is the most suitable way to remove switch bounce in software?
- ARead the pin more often and treat any single 1 as a press
- BRead the pin every few milliseconds and accept a change only after the same value repeats several times
- CReboot the microcontroller whenever the switch is pressed
Show answer and explanation
Answer: B. Read the pin every few milliseconds and accept a change only after the same value repeats several times
Bounce settles within a few milliseconds, so reading the pin, say, every 5 ms and declaring a change only after three identical readings ignores the transitions during bouncing. Reading periodically from a timer interrupt avoids blocking other work. Making the delay too long makes the response feel sluggish, so it is tuned to the switch's characteristics.
- A. That catches every change during the bounce.
- B. Correct. Changes during bouncing are ignored.
- C. Rebooting is unnecessary.