A tap is a hump
A tap lasts a tenth of a second or more on SIGNAL, so how often you read decides what you see. Read every 100 ms, as the old sketch did, and you catch each tap on its way down and count it three or four times. Read continuously and keep the highest of each 20 ms, and you see each tap once, at nearly its real height.
What a slow sketch saw
The owner tested the block on an Uno with a sketch that read SIGNAL once, printed it, and waited 100 ms. Two taps from that log, as the Uno's counts out of 1023:
| Reading | First tap | Second tap |
|---|---|---|
| 1 | 2 | 4 |
| 2 | 369 (1.80 V) | 259 (1.27 V) |
| 3 | 231 (1.13 V) | 289 (1.41 V) |
| 4 | 82 (0.40 V) | 104 (0.51 V) |
| 5 | 18 (0.09 V) | 27 (0.13 V) |
| 6 | 0 | 1 |
Each tap is four readings above zero, falling to under a quarter in two of them. None of those readings is the top of the tap: the sketch was asleep when it happened. The second column even rises before it falls, which is either a second tap or the first still near its top when it was read; the log cannot say which.
How often is often enough
Drag the interval. The hump is this book's model, not a measurement, and the tap lands at the same point between readings every time. At 100 ms the first reading comes 63 ms after the tap, when the hump has fallen well below its top, and four readings stay over 50 mV. At a few milliseconds the highest reading is within a few percent of the top.
That is why the first sketch has no delay(). It reads as fast as the
board allows, several thousand times a second on an Uno, keeps the highest
reading of each 20 ms, and prints only that. The hump lasts far longer than
20 ms, so the window almost always holds its top, and the plot shows one
spike per tap.
A tap is a rise, not a level
The table also shows why "over 100 counts means a tap" counts wrong: a single tap stayed over 100 for two readings, and over 10 for four. What marks a tap is SIGNAL rising. The next article builds a switch on that.
When it does not work
If the sketch reads with a delay between readings, it catches each tap at a random point on the way down, so equal taps read anywhere from their full height to a fraction of it. Read without a delay and keep the highest reading in a short window, as the first sketch does.
The hump lasts longer than the gap between readings, so several readings in a row are above any small level. Counting readings over a level counts the hump, not the tap. Count a rise, then ignore everything for a moment, as the tap switch does.
Not directly: an interrupt pin needs the signal to cross a logic threshold, 3 V on a 5 V Uno, and light taps never reach it. A comparator in front of the pin could do it. For most uses, reading in a loop with no delay is simpler and catches every tap.
Edit this page — content/books/piezo-sensor/a-tap-is-a-hump.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Piezo-Ceramic Sensor
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.