Smart Knock Detecting Door Lock using Arduino

Smart Knock Detecting Door Lock

Security is a major concern in our day to day life, and digital locks have become an important part of these security systems. There are many types of security systems available to secure our place. Some examples are PIR based Security SystemRFID based Security SystemDigital Lock System, bio-matrix systems, Electronics Code lock. In this post, let us build a Secret Knock Detecting Door Lock using Arduino which can detect the pattern of your knocks at the door and will only open the lock if the knocking pattern matches with the correct pattern. For the proper working Demo, check the Video at the end.

Smart Knock Detecting Door Lock


  1. Arduino Uno
  2. Push Button
  3. Buzzer
  4. 1M Resistor
  5. Power
  6. Connecting wires
  7. Box
  8. Servo Motor

Circuit Explanation:

The circuit Diagram of this Knocking Pattern Detector is very simple which contains Arduino for controlling whole the process of the project, push button, buzzer, and Servo Motor. Arduino controls the complete processes like taking password form Buzzer or Sensor, comparing patterns, driving Servo for open and close the gate and save the pattern to Arduino.

The push button is directly connected to pin D7 of Arduino with respect to ground. And a buzzer is connected at analog pin A0 of Arduino with respect to ground and with a 1M resistance between A0 and ground also. A Servo motor is also connected to PWM pin D3 of Arduino.

Feeding Knocking Pattern in Arduino:

In this circuit, we have used Buzzer or Peizo Sensor to take knock input pattern in the system. Here we are using a push button to allow to take input from the sensor and also save that into the Arduino. This system is designed by taking idea from Morse code pattern but not exactly similar with that.

Here we have used a card board box for demonstration. To take input we knock over the board after pressing push button. Here we have knocked by keeping a time period in mind that is 500ms. This 500ms is because we have fixed it in code and input pattern is depending upon it. This 500ms time period will define the input was 1 or 0. Check the code below to understand this thing.

When we knock it, Arduino starts monitoring the time of the first knock to second knock and put that in an array. Here in this system, we are taking 6 knocks. It means we will get 5 time periods.

Now we check the time period one by one. First, we check time period between first knock and second knock if the time difference between these less the 500ms then it will be 0 and if greater than 500ms it will be 1 and it will be saved into a variable. Now after it, we check time period between second knock and third knock and so on.

Finally, we will get 5 digit output in 0 and 1 format (binary).

Working Explanation:

Working of Knock based Smart Lock Project is simple. First we have to save a pattern in the system. So we have to press and hold push button until we knock 6 times. Here in this project, I have used 6 knocks but the user may change it as they want. After six times knock, Arduino find the knock pattern and save that in EEPROM. Now after saving the input pattern, press and immediately released the push button for taking input from the sensor to Arduino to open the lock. Now we have to knock 6 times. After it, Arduino decodes it and compares with saved pattern. If a match occurs then Arduino open the gate by driving servo motor.

Note: when we press or press and hold the push button Arduino start a 10 seconds timer to take all 6 knock. Means user need to knock within this 10 seconds time. And the user may open Serial monitor to see the log.

Programming Explanation:

In a program first of all we include the header file and defines input and output pin and define the macro and declared variables as you can see in the Full Code in code section below.

Smart Knock Detecting Door Lock schematic

After this, in setup function, we give direction to defined pin and initiate servo motor.

void setup() 
  pinMode(sw, INPUT_PULLUP);

After it, we take input and save the input pattern or knock time in an array.

void loop() 
   int i=0;
   if(digitalRead(sw) == LOW)
      long stt= millis();
        int temp=analogRead(A0);
        if(temp>sensitivity && flag==0 && i<=patternLenth)
.... .
..... ....

After it, we decode the input pattern

      for(int i=0;i<patternLenth;i++)
         if(slot[i+1]-slot[i] <500 )

And then save if push button is still pressed

      if(digitalRead(sw) == 0)
         for(int i=0;i<patternLenth;i++)
         while(digitalRead(sw) == 0);

And if push button is not still pressed then Arduino will compare input decoded pattern with saved pattern.

         if(knok == 1)
            for(int i=0;i<patternLenth;i++)
               if(pattern[i] ==


If any password matched, then Servo open the gate otherwise nothing happened but the user may see result over serial monitor.

         if(acceptFlag >=  patternLenth-1)
             Serial.println(" Accepted");

You can check the complete code below with a demo Video.



#define patternLenth 5
#define patternInputTime 10000
#define sensitivity 80
#define margin 100
#define sw 7
#define servoPin 3
#define openGate 0
#define closeGate 180
long slot[patternLenth+1];
int pattern[patternLenth];
int flag=0;
int acceptFlag=0;
int knok;

Servo myServo;

void setup()

Read more: Smart Knock Detecting Door Lock using Arduino

Leave a Comment

Your email address will not be published. Required fields are marked *