Projects / Door knock detector

Door knock detector

Beginner45 minGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Stick on the module

    Inside the door.

  2. NANOKY031

    Print the pin state

    See it change on a knock.

  3. NANOKY031

    Count the knocks

    Group by time.

  4. NANOKY031

    Add the chime

    One beep per knock.

  5. NANOKY031

    Test a soft and a hard knock

    Find the limit.

  6. NANO

    Set a secret pattern

    Beep only on a match.

  7. NANO

    Fit it to the door

    Tidy the wire.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Knock module S (signal)Nano D2Digital signal
Knock module middle pinNano 5VPower
Knock module minusNano GNDGround
10 k resistorBetween D2 and 5VHolds the line steady when idle
Buzzer plusNano D8Chime
Buzzer minusNano GNDReturn

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.

CheckExpected
One knockSerial prints 1, one beep
Three quick knocksPrints 3, three beeps
Door slamCounted, no false extra
IdleSilence

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 →

Ask Lark AI

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.