Build it step by step
0 of 7 doneStick on the module
Inside the door.
Print the pin state
See it change on a knock.
Count the knocks
Group by time.
Add the chime
One beep per knock.
Test a soft and a hard knock
Find the limit.
Set a secret pattern
Beep only on a match.
Fit it to the door
Tidy the wire.
How do you build a door knock detector?
A knock sensor on a door tells you when someone knocks, and counts the knocks. You need Arduino Nano, Knock sensor module (KY-031), Active buzzer 5 V and Resistor 10 kΩ. It takes 7 steps and about 45 minutes.
- Level
- Beginner
- Time
- About 45 min
- Parts
- 4
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- Motors & ActuatorsBuy
- PassivesBuyResistor 10 kΩ × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Knock module S (signal) | Nano D2 | Digital signal |
| Knock module middle pin | Nano 5V | Power |
| Knock module minus | Nano GND | Ground |
| 10 k resistor | Between D2 and 5V | Holds the line steady when idle |
| Buzzer plus | Nano D8 | Chime |
| Buzzer minus | Nano GND | Return |
Code
The KY-031 module contains a spring and a contact; a knock makes the spring touch for a moment, which the Nano sees as a change on the pin. Because a spring bounces, one knock can show up as several, so the sketch ignores changes for 120 milliseconds after each and waits 1.5 seconds before counting, so a pattern such as two quick knocks and a pause is counted as two. Stick the module to the inside of the door with double-sided tape, or to the back of a letter box, and test with different strengths. Sensitivity is set by the spring, not by the code, so it is best for solid knocks; light taps may not register. Try a secret knock by comparing the count with a stored number. Keep the chime short, since it will fire for every visitor. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano 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).
// Door knock detector: counts knocks within a 1.5 second window and plays that many chimes.
// Board: Arduino Nano. The module changes its output when the spring touches; the sketch works with either polarity.
const byte SENSOR = 2, BUZZER = 8;
bool restLevel;
int knocks = 0;
unsigned long lastKnock = 0, windowStart = 0;
void setup() {
Serial.begin(115200);
pinMode(SENSOR, INPUT);
pinMode(BUZZER, OUTPUT);
delay(500);
restLevel = digitalRead(SENSOR); // whatever the line reads at rest
}
void chime(int n) {
for (int i = 0; i < n && i < 5; i++) { tone(BUZZER, 1200, 120); delay(220); }
}
void loop() {
bool now = digitalRead(SENSOR);
if (now != restLevel && millis() - lastKnock > 120) { // ignore the sensor's own ringing
lastKnock = millis();
if (knocks == 0) windowStart = millis();
knocks++;
}
if (knocks > 0 && millis() - windowStart > 1500) {
Serial.print("Knocks: "); Serial.println(knocks);
chime(knocks);
knocks = 0;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| One knock | Serial prints 1, one beep |
| Three quick knocks | Prints 3, three beeps |
| Door slam | Counted, no false extra |
| Idle | Silence |
If it does not work
Always firing
Check the 10 k resistor and the rest level.
Never fires
Press the module firmly on the door.
Counts too many
Raise the 120 ms ignore time.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the door knock detector?
Arduino Nano, Knock sensor module (KY-031), Active buzzer 5 V and Resistor 10 kΩ.
How long does the door knock detector take to build?
About 45 minutes for a beginner maker, in 7 steps.
How do I know the door knock detector works?
One knock: Serial prints 1, one beep. Three quick knocks: Prints 3, three beeps. Door slam: Counted, no false extra.
Always firing: what should I check?
Check the 10 k resistor and the rest level.
Never fires: what should I check?
Press the module firmly on the door.
Counts too many: what should I check?
Raise the 120 ms ignore time.
