switchCharacterize Sketch Documentation
Copy the.txt file to a new folder named switchCharacterize in your normal sketch folder. Rename the .txt to .ino (This WordPress site does not allow .ino files for some reason).
I run it on an Arduino Due as that board runs at 84 MHz and gets more samples of the bouncing behavior. Connect a switch between pin 2 and Vcc (+5 or +3.3). Connect a pulldown resistor between pin 2 and ground. Something around 10k is a good size for the resistor.
Open the serial port and wait to see:
You can then press and release the switch to see how many edges (bounces = (N-1)/2) were detected and how long it took to settle.
0 | 1 - 23 : 2432 usec 339 samples ==> 1 | 0 - 1 : 0 usec 212 samples
The first column (0) is a counter of the switch presses. The 1 is the level (1 is HIGH). The 23 is the count of the edges seen, so this is 23 edges. We expected 1, so there were (23-1)/2=11 bounces. The bouncing occurred over a period of 2432 microseconds. The program was able to capture 339 samples during this interval (this was run on an Uno at 20 MHz). The next column (1) is the switch press counter again (this time a release). The 0 is the level (0 is LOW). There was 1 edge (so no bouncing) and the bounce duration was 0 microseconds. The program captured 212 samples before deciding that the switch was not going to bounce.
If you press and release the button 25 times, you will get a summary line with the largest number of edges seen for pressing and releasing.
. . .
46 | 1 - 23 : 2528 usec 344 samples ==> 47 | 0 - 1 : 0 usec 212 samples
48 | 1 - 19 : 2532 usec 345 samples ==> 49 | 0 - 1 : 0 usec 212 samples
Maxup 35 Maxdown 3
So, this switch had a worst case of 17 bounces on press and 1 bounce on release.