Build it step by step
0 of 8 doneWire the disc
One lead to A0, the other to GND.
Add the 1 megohm resistor
From A0 to GND, across the disc.
Add the two diodes
One from A0 to 5V (stripe to 5V), one from GND to A0 (stripe to A0).
Wire the LED
D13 through 220 ohm to GND.
Upload the sketch
Serial monitor says "Knock on the door".
Tap the disc
The LED blinks and a knock is counted.
Tap twice quickly
The pattern line says double knock.
Fix the disc on a door
Raise THRESHOLD until footsteps do not count.
How do you build a door knock counter?
A piezo disc stuck inside a door counts knocks and blinks an LED for each one. You need Arduino Uno R3, Piezo disc buzzer element, Resistor 1 MΩ, Signal Diode 1N4148 × 2, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 100 minutes.
- Level
- Beginner
- Time
- About 100 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- Motors & ActuatorsBuy
- PassivesBuyResistor 1 MΩ × 1
- SemiconductorsBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Piezo disc (one lead) | Uno A0 | Signal from the disc |
| Piezo disc (other lead) | Uno GND | Return |
| 1 megohm | Across the disc, from A0 to GND | Bleeds the charge so the signal falls back to zero |
| 1N4148 diode (cathode to 5V) | Uno A0 and 5V | Clamps spikes above 5 V |
| 1N4148 diode (anode to GND) | Uno A0 and GND | Clamps negative spikes |
| Red LED through 220 ohm | Uno D13 to GND | Blinks on each knock |
Code
A piezo disc makes a small voltage when it is bent or struck, so stuck to the inside of a door it picks up the vibration of a knock. The signal is a short spike followed by ringing. The 1 megohm resistor lets the charge leak away so the voltage returns to zero, and the two small diodes keep the spikes within the range of the input pin, because a hard knock can briefly make several volts. The sketch reads the pin as fast as it can, treats anything above the threshold as a knock, then ignores the next 120 milliseconds so one knock is not counted several times. Knocks that arrive within 700 milliseconds of the first are grouped as one pattern, which lets you tell a single knock from a double knock. Fix the disc with double-sided tape on a solid part of the door; a loose disc buzzes and gives false counts. This is a learning project and a convenience chime idea, not a security device.
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int PIEZO = A0;
const int LED = 13;
const int THRESHOLD = 40; // 0 to 1023, raise it if footsteps count as knocks
const unsigned long LOCKOUT_MS = 120; // ignore the ringing after a knock
const unsigned long WINDOW_MS = 700; // two knocks inside this time make a double knock
unsigned long lastKnock = 0;
int knockCount = 0;
int burst = 0;
unsigned long burstStart = 0;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Knock on the door");
}
void loop() {
int v = analogRead(PIEZO);
unsigned long now = millis();
if (v > THRESHOLD && now - lastKnock > LOCKOUT_MS) {
lastKnock = now;
knockCount++;
if (burst == 0) burstStart = now;
burst++;
digitalWrite(LED, HIGH);
Serial.print("Knock "); Serial.print(knockCount);
Serial.print(" peak "); Serial.println(v);
}
if (now - lastKnock > 60) digitalWrite(LED, LOW);
// Report a pattern once the window has closed
if (burst > 0 && now - burstStart > WINDOW_MS) {
if (burst == 1) Serial.println("Pattern: single knock");
else if (burst == 2) Serial.println("Pattern: double knock");
else { Serial.print("Pattern: "); Serial.print(burst); Serial.println(" knocks"); }
burst = 0;
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Knock on the door Knock 1 peak 512 Knock 2 peak 513 Knock 3 peak 512 Knock 4 peak 512 Knock 5 peak 512
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Single tap | One knock counted and a single pattern |
| Two quick taps | Double knock pattern |
| Hard knock | One count, not several |
| Gentle breath on the disc | No count |
| Disc taped on a door | Knocks counted from the other side |
If it does not work
Every knock counts twice
Increase LOCKOUT_MS to 200.
Nothing counts
Lower THRESHOLD and check the 1 megohm resistor is in place.
Random counts
A loose disc or lead is buzzing, tape it down firmly.
Readings stay high
Check the diode orientation and the 1 megohm resistor.
LED stays on
Check the LED legs and the pin number.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the door knock counter?
Arduino Uno R3, Piezo disc buzzer element, Resistor 1 MΩ, Signal Diode 1N4148 × 2, LED 5 mm red, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the door knock counter take to build?
About 100 minutes for a beginner maker, in 8 steps.
How do I know the door knock counter works?
Single tap: One knock counted and a single pattern. Two quick taps: Double knock pattern. Hard knock: One count, not several.
Every knock counts twice: what should I check?
Increase LOCKOUT_MS to 200.
Nothing counts: what should I check?
Lower THRESHOLD and check the 1 megohm resistor is in place.
Random counts: what should I check?
A loose disc or lead is buzzing, tape it down firmly.
Readings stay high: what should I check?
Check the diode orientation and the 1 megohm resistor.
