Learning Goals 5 min
- Wire a piezo disc backwards — as an analog input rather than an output — so it senses vibration instead of making sound.
- Detect a single knock cleanly: read the peak voltage on the analog pin, threshold it, and use a short cooldown so one knock doesn't register as five.
- Build a knock counter and a tap-tempo (count knocks per second) — your first real signal-processing project.
Warm-Up 10 min
You've been using the piezo buzzer as an output since L01-14 — apply a voltage at 880 Hz and it sings A5. But the piezo crystal is a two-way device: when you bend it, it generates a tiny voltage. That's the piezoelectric effect — discovered by the Curie brothers in 1880, and the working principle behind quartz watches, ultrasonic transducers, BBQ lighters, and the strange little disc sitting on your bench.
Try it before you wire it
Even before today's sketch: take a piezo disc, connect a multimeter on its 200 mV range to the two leads. Tap the disc gently with your fingernail. The needle (or digits) jump briefly — you're reading the voltage you generated by deforming the crystal. That's the entire principle of today's sensor.
New Concept · The piezo as a knock sensor 20 min
Wiring — almost the same as the buzzer
| Piezo lead | Connect to |
|---|---|
| Red (+) | A0 (analog input) |
| Black (−) | GND |
| 1 MΩ resistor | Between A0 and GND, in parallel with the piezo |
Two new ideas to unpack:
- The piezo is now an INPUT. No
pinModeneeded (A0 defaults to input). The piezo's tiny voltage shows up at A0 and we read it withanalogRead. - The 1 MΩ "bleed" resistor. A piezo is essentially a tiny capacitor — once you give it a charge it holds it for a while. Without the resistor, every knock leaves A0 stuck at a high reading for seconds. The 1 MΩ resistor slowly drains the charge so each knock is a sharp, short spike. Don't skip it.
What the analog reading looks like
Sitting still: raw hovers around 0–10. Tap the disc with a fingernail: raw briefly spikes to 200–800 for one or two readings, then collapses back to ~0. Hit it harder: spike goes to 900+. The reading is roughly proportional to how hard the disc was struck.
Detecting a knock — the cooldown idea
A naive sketch like:
if (analogRead(A0) > 100) Serial.println("KNOCK");will print KNOCK five times in a row for one tap — because one physical knock produces a spike that lasts a few milliseconds, which crosses your loop() a handful of times. The fix is a cooldown: after detecting a knock, ignore the sensor for the next 100–200 ms.
const int THRESHOLD = 100;
const unsigned long COOLDOWN_MS = 150;
unsigned long lastKnock = 0;
void loop() {
int raw = analogRead(KNOCK);
if (raw > THRESHOLD && millis() - lastKnock > COOLDOWN_MS) {
Serial.print("KNOCK strength: ");
Serial.println(raw);
lastKnock = millis();
}
}Exactly the same pattern you used for the non-blocking beep in L02-12 and the rain-reminder in L02-17. Pattern: a millis() gate on top of a threshold.
Worked Example · Knock counter 20 min
Step 1 — wiring
On the breadboard what you build
On paper what engineers draw
Piezo + on A0, piezo − on GND, 1 MΩ across A0 and GND. Tape the piezo to the breadboard or the desk with masking tape so it's mechanically coupled.
Step 2 — the sketch
Save as knock-counter.ino:
// L02-18: Piezo as a knock sensor with cooldown
const int KNOCK = A0;
const int THRESHOLD = 100;
const unsigned long COOLDOWN_MS = 150;
unsigned long lastKnock = 0;
int knockCount = 0;
void setup() {
Serial.begin(9600);
Serial.println("Tap the piezo to count knocks.");
}
void loop() {
int raw = analogRead(KNOCK);
if (raw > THRESHOLD && millis() - lastKnock > COOLDOWN_MS) {
knockCount++;
lastKnock = millis();
Serial.print("KNOCK #");
Serial.print(knockCount);
Serial.print(" strength: ");
Serial.println(raw);
}
}Step 3 — upload and tap
Open Serial Monitor. Tap the piezo with one fingernail. You should see one line per tap, with a strength number that varies with how hard you tap:
Tap the piezo to count knocks. KNOCK #1 strength: 180 KNOCK #2 strength: 240 KNOCK #3 strength: 540 KNOCK #4 strength: 95 ← too soft, won't appear KNOCK #4 strength: 380
Soft taps below the threshold are silently ignored — that's working as designed. If the count is missing taps you wanted, lower the threshold. If it's catching footsteps you didn't want, raise it.
Step 4 — verify the cooldown
Tap the piezo really hard once. Without the cooldown, the spike would print KNOCK 3–5 times. With it, you should see exactly one KNOCK per tap, even hard ones. If you see doubles, raise COOLDOWN_MS to 200 or 250.
Step 5 — quick tempo check
Tap a steady rhythm — once per second for 10 seconds. Count the knock messages. Should be 9 or 10, not 50. The cooldown is keeping you honest.
Basic 7 min
Goal: Light an LED on D9 for 200 ms every time a knock is detected. Visual feedback so you don't need to watch the Serial Monitor.
Challenge 1 7 min
Goal: Classify each knock by strength: soft (100–250), medium (250–600), hard (> 600). Print the label alongside the strength.
Challenge 2 6 min
Goal: Compute and display the tap tempo — beats per minute (BPM) — based on the gap between the last two knocks. So a tap once per second = 60 BPM, twice per second = 120 BPM.
Challenge 3 · Secret-knock unlock 15 min
Build the classic "shave-and-a-haircut" secret-knock detector. The sketch must:
- Wait for a knock pattern matching 5 knocks with specific gaps (the famous shave-and-a-haircut rhythm: short-short-long-short-short, with the last two knocks slightly faster than the others).
- If the pattern matches within ±30% tolerance on each gap → flash an LED on D9 for 2 seconds (the "door unlocked" signal).
- If the pattern is wrong, do nothing — wait for the next attempt.
- After matching or after 5 seconds without a knock, reset the counter and try again.
Suggested target rhythm (millisecond gaps between knocks): 250, 250, 500, 250. So tap a steady rhythm with one slightly longer pause in the middle.
It's done when:
- The LED unlocks reliably when you tap the correct rhythm.
- Tapping wrong (e.g. all even, or only 4 knocks, or 6) does not unlock.
- After 5 seconds of silence the buffer clears — no stale partial patterns.
- You can see the recorded gaps in the Serial Monitor for debugging.
Recap 5 min
A piezo is two-way: drive it with a voltage and it sings; bend it with mechanical force and it generates a voltage. Today you used it backwards — as a knock sensor on an analog pin, with a 1 MΩ bleed resistor across it to drain the charge between knocks. The detection pattern was the now-familiar threshold-plus-cooldown: register a knock when the analog reading exceeds a level, then ignore the pin for 100–200 ms so a single physical tap doesn't produce a flood of detections. With that pattern + an array of gap timings you can recognise rhythms — the secret-knock unlock is the classic "hello, embedded signal processing" demo.
- Piezoelectric effect
- The two-way property of certain crystals (quartz, ceramic discs): apply a voltage → the crystal deforms (buzzer); deform the crystal → it produces a voltage (sensor).
- Knock sensor
- Any sensor that detects a sharp mechanical impulse — typically a piezo, but accelerometers count too. Differs from a microphone (steady audio) in that it's tuned for spikes.
- Bleed resistor
- A high-value resistor across a capacitor (or in this case the piezo) to drain stored charge slowly so each impulse is sharp and short rather than long and smeared.
- Cooldown
- A short period after a detection during which we ignore the sensor — prevents one physical event from producing many software events.
- Threshold + cooldown pattern
- The general recipe for "detect a discrete event from a continuous sensor": trigger when the reading exceeds a level AND enough time has passed since the last trigger.
- Tap tempo
- The rate of repeated knocks, usually expressed as beats per minute. Calculated from the gap between consecutive knock timestamps.
Extra Mission 5 min
Part 1 — Design a knock-controlled object
A knock is a free button with no switch to wear out. Pick an object — a desk, a door, a lunch box, a drum pad. On paper, design what different knocks make it do.
Your design must include:
- A name and a one-sentence job for the object.
- Where the piezo is mounted.
- At least two knock types — soft and hard, or one knock and two quick knocks — and what each one does.
- The threshold and cooldown you will start with.
Part 2 — Make it
Build it with the piezo on A0 and at least one LED or the buzzer. Print the strength of every knock and what the device decided.
Bring back next class: the uploaded sketch and a Serial Monitor screenshot showing each knock type.