Debounce an input using Arduino
This example demonstrates how to debounce an input, which means checking twice in a short period of time to make sure it’s definitely pressed. Without debouncing, pressing the button once can appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
The code below is based on Limor Fried’s version of debounce, but the logic is inverted from her example. In her example, the switch returns LOW when closed, and HIGH when open. Here, the switch returns HIGH when pressed and LOW when not pressed.
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There’s
a minimum delay between toggles to debounce the circuit (i.e. to ignore
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don’t need any extra components for this example.
For more detail: Debounce an input using Arduino
This Post / Project can also be found using search terms:
- Arduino input debounce
- debounce arduino input
- debounce inputs arduino
- How to hook up momentary switch to arduino with debounce