Originally published on ShillehTek IoT & Beyond.
Project Overview
Arduino Piezo Knock Sensor - From Tap Detection to a Secret-Knock Lock: Build an Arduino Nano piezo knock sensor that detects taps, flashes an LED on each knock, and unlocks a relay when a three-knock pattern is detected within a timing window.
A piezo disc generates a voltage spike when it is struck, and the Arduino analog input can catch that spike. You will start by lighting an LED on every knock, then count knocks inside a time window to create a simple secret-knock lock that fires a relay when the pattern is right. This project teaches thresholds, debouncing a mechanical signal, and timing windows.
- Time: ~40 minutes
- Skill level: Beginner
- What you will build: A knock detector with per-knock LED flashes and a three-knock secret code that unlocks a relay for three seconds.
A piezo disc turns a knock into a voltage spike the Arduino can read.
Parts List
From ShillehTek
- Arduino Nano V3.0 Pre-Soldered - the microcontroller that reads the piezo sensor and controls the LED and relay
- KY-006 Passive Piezo Buzzer - the element inside a passive buzzer is the same piezo disc used as the sensor
- Resistor Kit - 1MΩ across the piezo, 220Ω for the LED
- 1-Channel 5V Relay Module - switches a lock or lamp once the secret pattern is detected
- 400-Point Breadboard - quick prototyping without soldering
- Dupont Jumper Wires - wiring between the Arduino, sensor, LED, and relay
External
- A bare piezo disc with leads (or harvest one from an old buzzer or musical greeting card)
- An LED, and optionally a 12 V solenoid lock for the finished build
Note: The 1MΩ resistor in parallel with the piezo is not optional. It bleeds the charge off the disc so the reading returns to zero after each knock, and it limits the voltage spike. A hard tap on a bare piezo can exceed the Arduino 5 V input limit.
Step-by-Step Guide
Step 1 - Wire the Piezo and LED
Goal: A sensor the ADC can read safely.
What to do: Connect the piezo red lead to A5 and the black lead to GND. Place a 1MΩ resistor between A5 and GND. For the LED, wire D9 to a 220Ω resistor, then to the LED, then to GND.
Tape the piezo disc flat to the surface you will knock on (a tabletop, a box lid, or a door) so vibrations couple into the disc.
Piezo on A5 with a 1MΩ bleed resistor; LED on D9.
Expected result: A sensor that reads near 0 at rest and spikes when tapped.
Step 2 - Find Your Threshold
Goal: Separate knocks from noise.
What to do: Upload a sketch that prints analogRead(A5) as fast as it can and open the Serial Plotter. Tap the surface: a firm knock should spike into the hundreds while the rest sits near zero. The original workshop used a threshold of 120, so pick a value about half your typical knock peak.
Expected result: A threshold number that catches real knocks and ignores the room.
Step 3 - Upload the Sketch for Knocks and the Secret Pattern
Goal: Flash the LED on each knock, then unlock a relay when the correct knock count happens inside a timing window.
What to do: Upload the code below, then test: knock once to see a flash. Next, knock three times within 1.5 seconds to trigger the relay for three seconds.
Code:
const int PIEZO = A5, LED = 9, RELAY = 7;
const int THRESHOLD = 120; // raise it if footsteps trigger it
const int SECRET_KNOCKS = 3; // knock this many times...
const unsigned long WINDOW = 1500; // ...within this many milliseconds
const unsigned long DEBOUNCE = 120; // ignore the ringing after each hit
int knocks = 0;
unsigned long firstKnock = 0, lastKnock = 0;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT); pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); // active-LOW relay board: HIGH = locked
}
void loop() {
int level = analogRead(PIEZO);
if (level > THRESHOLD && millis() - lastKnock > DEBOUNCE) {
lastKnock = millis();
if (knocks == 0) firstKnock = lastKnock;
knocks++;
Serial.print("Knock "); Serial.print(knocks);
Serial.print(" level "); Serial.println(level);
digitalWrite(LED, HIGH); delay(30); digitalWrite(LED, LOW); // flash per knock
}
// window closed: judge the pattern
if (knocks > 0 && millis() - firstKnock > WINDOW) {
if (knocks == SECRET_KNOCKS) {
Serial.println("Secret knock! Unlocking for 3 s");
digitalWrite(RELAY, LOW); digitalWrite(LED, HIGH);
delay(3000);
digitalWrite(RELAY, HIGH); digitalWrite(LED, LOW);
} else {
Serial.println("Wrong pattern");
for (int i = 0; i < 3; i++) { // angry triple blink
digitalWrite(LED, HIGH); delay(80); digitalWrite(LED, LOW); delay(80);
}
}
knocks = 0;
}
}
Expected result: Single knocks flash the LED and get rejected; exactly three knocks light the LED solid and pull the relay for three seconds.
Step 4 - Add the Lock (Relay Output)
Goal: Make the secret knock trigger a real load.
What to do: Wire the relay module IN to D7, VCC to 5V, and GND to GND. If you are driving a 12 V solenoid lock, power it from its own 12 V supply and route the lock power through the relay NO and COM contacts.
Mount the piezo on the inside of the door or drawer front so knocks travel through the wood.
Expected result: A drawer or door that unlocks only for the right knock pattern.
Step 5 - Make the Code Harder to Guess
Goal: Use rhythm, not just knock count.
What to do: Record the time between knocks into an array and compare it to a stored rhythm, allowing plus or minus 25% on each gap. Add a program button that saves whatever you knock next as the new secret. This changes the lock from a simple counter to a beat matcher.
Expected result: A rhythm-based lock that is harder to guess and more fun to demo.
Conclusion
You built an Arduino piezo knock sensor that reads vibration spikes, filters noise with a threshold and debounce, and uses a timing window to unlock a relay on a secret knock pattern. The same approach applies to tap-to-wake inputs, drum triggers, and shock detection projects.
Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.
Credits: Photos and the original reference guide are credited to Sarful (Mechatronics LAB) on Hackster.io.


Top comments (0)