Arduino – Does this mechanical switch really have no bounce

arduinodebounceswitches

I'm prototyping with a bunch of no name mechanical button mini-switches and SAM D21 microcontroller (Adafruit Feather M0). The switch is connected directly between the ground and the input pin #19 (PB02 of ATSAMD21G18) without any debouncing circuitry #19. This Arduino test program:

constexpr int BUTTON = 19;

void setup()
{
  pinMode(BUTTON,  INPUT_PULLUP);
}

void loop()
{
  Serial << digitalRead(BUTTON) << ", " << micros() << "\n";
}

produces a log of clean transitions without a single bounce, e.g.

1, 8891998
1, 8892101
1, 8892197
1, 8892362
0, 8892468
0, 8892569
0, 8892668
0, 8892764
....
0, 9063951
0, 9064048
0, 9064145
0, 9064305
1, 9064401
1, 9064507
1, 9064610
1, 9064706

Pin PB02 is read more often than every 0.1ms, which seems sufficient to catch mechanical switch bounce. Is there some hardware filtering involved here? Unfortunately, I don't have a scope handy to capture the signal.

Here is a picture of the switch:

enter image description here

EDIT

There are several answers blaming serial port latency for missing the bounce. This is incorrect. I'm logging timestamps of each measurement, which are better than 4μs accurate. See https://www.arduino.cc/en/pmwiki.php%3Fn%3DReference/Micros for description of micros() function, specifically: "On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds".

EDIT'

Picture of the test setup, as requested (tiny switch on the left):

enter image description here

Best Answer

Not every electromechanical contact exhibits bounces, and if it does, not always every single time you activate it. Moreover, some switches bounce only on one "edge" of the signal, i.e. only when closed or open. And other switches' bounces are so quick that they are not detectable by common MCUs. You need a scope to capture that behavior.

The variations could be astounding.

An interesting article about debouncing from embedded guru Jack Ganssle shows you some empirical data.

Excerpts (emphasis mine):

Many of the switches exhibited quite wild and unexpected behavior. Bounces of under 100 nsec were common (more on this later). No reasonable micro could reliably capture these sorts of transitions, so I abandoned that plan and instead used the scope, connecting both analog and digital channels to the switch. This let me see what was going on in the analog domain, and how a computer would interpret the data. A 5 volt supply and 1k pull-up completed the test jig.

If a sub-100 nsec transition won't be captured by a computer why worry about it? Unfortunately, even a very short signal will toggle the logic once in a while. Tie it to an interrupt and the likelihood increases. Those transitions, though very short, will occasionally pervert the debounce routine. For the sake of the experiment we need to see them.

[...]

So how long do switches bounce for? The short answer: sometimes a lot, sometimes not at all.

Only two switches exhibited bounces exceeding 6200 µsec. Switch E, what seemed like a nice red pushbutton, had a worst case bounce when it opened of 157 msec - almost a 1/6 of a second! Yuk. Yet it never exceeded a 20 µsec bounce when closed. Go figure.

Another switch took 11.3 msec to completely close one time; other actuations were all under 10 msec.

Toss out those two samples and the other 16 switches exhibited an average 1557 µsec of bouncing, with, as I said, a max of 6200 µsec. Not bad at all.

Seven of the switches consistently bounced much longer when closed than when opened. I was amazed to find that for most of the switches many bounces on opening lasted for less than 1 µsec - that's right, less than a millionth of a second. Yet the very next experiment on the same switch could yield a reading in the hundreds of microseconds.

Identical switches were not particularly identical. Two matching pairs were tested; each twin differed from its brother by a factor of two.

[...]

Use a grain of salt when playing with these numbers. Civil engineers don't really know the exact strength of a concrete beam poured by indolent laborers, so they beef things up a bit. They add margin. Do the same here. Assume things are worse than shown.

Bottom line: always assume switches will bounce and implement a conservative debouncing strategy (HW, SW or even both for maximum reliability).

If you really need to optimize things, you have to characterize your switch with sound statistical methods (never trust the manufacturer, unless you have a legal guarantee), so that you can choose the optimum debouncing strategy/parameters.