Embedded Systems & Firmware

Which is the most suitable way to remove switch bounce in software?

  1. ARead the pin more often and treat any single 1 as a press
  2. BRead the pin every few milliseconds and accept a change only after the same value repeats several times
  3. 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.
Practice this topic Back to topic

More Embedded Systems & Firmware questions

  1. A timer counts a 16 MHz clock divided by 16 with a prescaler and raises an interrupt every 1000 counts. What is the interrupt interval?
  2. A PWM signal has a period of 1000 counts, with the output high (1) for 250 counts of each period. What is the duty cycle?
  3. Which method is commonly used by microcontrollers to adjust LED brightness or motor speed?
  4. What is the input capture function of a microcontroller timer used for?
  5. What should be avoided inside an interrupt handler?