piezo sensor/Reading it/07. A tap is a hump
Reading it · 07 of 9

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:

ReadingFirst tapSecond tap
124
2369 (1.80 V)259 (1.27 V)
3231 (1.13 V)289 (1.41 V)
482 (0.40 V)104 (0.51 V)
518 (0.09 V)27 (0.13 V)
601

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

A tap is a hump
A reading every100 ms
Highest reading
59 % of the top
Readings over 50 mV
4
Falls to a third in
about 120 ms
Every 100 ms, the first reading after the tap lands 63 ms later, on the way down: 59 % of the top. And 4 readings stay above 50 mV, so a sketch that counts readings over a level counts 4 taps. The old sketch read every 100 ms; the owner's Uno log shows one tap as 4 readings, 369, 231, 82, 18.

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

The same tap gives very different numbers each time.

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.

One tap is counted three or four times.

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.

Can I use an interrupt instead of reading in a loop?

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.

Alongside this page
Where this goes next

A light that each tap turns on or off, counted once.

A tap switch →

Edit this page — content/books/piezo-sensor/a-tap-is-a-hump.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

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.

Browse Modules and blocks on the forum →