Smart Knock Detecting Door Lock using Arduino

Published  July 21, 2017   12
S Saddam
Author
Smart Knock Detecting Door Lock Project using Arduino Uno

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 System, RFID based Security System, Digital 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.

 

Components:

  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.

Secret Knock Detecting Door Lock using Arduino circuit

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.

Secret Knock Detecting Door Lock using Arduino

Servo for Knock based Smart Lock Project

 

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.

Knock based Smart Lock Project

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.

Knocking Pattern Detector with Arduino and servo

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.

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

void setup() 
{
  pinMode(sw, INPUT_PULLUP);
  myServo.attach(servoPin);
  myServo.write(180);
  Serial.begin(9600);
}

 

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)
   {   
      Serial.println("Start");
      delay(1000);
      long stt= millis();
      while(millis()<(stt+patternInputTime))
      {
        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++)
      {
         knok=1;
         if(slot[i+1]-slot[i] <500 )
            pattern[i]=0;
         else
            pattern[i]=1;
         Serial.println(pattern[i]);
      }

 

And then save if push button is still pressed

      if(digitalRead(sw) == 0)
      {
         for(int i=0;i<patternLenth;i++)
            EEPROM.write(i,pattern[i]);
         while(digitalRead(sw) == 0);
      }

 

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

      else
      {
         if(knok == 1)
         {
            for(int i=0;i<patternLenth;i++)
            {
               if(pattern[i] == EEPROM.read(i))
               {
                  Serial.println(acceptFlag++);
               }

               else
               {
                    Serial.println("Break");
                    break;                
               }
            }
         }

 

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

         Serial.println(acceptFlag);
         if(acceptFlag >=  patternLenth-1)
         {
             Serial.println(" Accepted");
             myServo.write(openGate);
             delay(5000);
             myServo.write(closeGate);
         }
         else
            Serial.println("Rejected");
      }

 

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

Code

#include<EEPROM.h>
#include<Servo.h>

#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() 
{
  pinMode(sw, INPUT_PULLUP);
  myServo.attach(servoPin);
  myServo.write(180);
  Serial.begin(9600);
}

void loop() 
{
   int i=0;
   if(digitalRead(sw) == LOW)
   {   
      Serial.println("Start");
      delay(1000);
      long stt= millis();
      while(millis()<(stt+patternInputTime))
      {
        int temp=analogRead(A0);
        if(temp>sensitivity && flag==0 && i<=patternLenth)
        {
             delay(10);
             flag=1;
             slot[i++]=millis()-stt;
             //Serial.println(slot[i-1] - stt);
             if(i>patternLenth)
             break;
        }

        else if(temp == 0)
        flag=0;
      }
      
      long stp=millis();
      Serial.println("Stop");
     // Serial.println(stp-stt);
      for(int i=0;i<patternLenth;i++)
      {
         knok=1;
         if(slot[i+1]-slot[i] <500 )
            pattern[i]=0;
         else
            pattern[i]=1;
         Serial.println(pattern[i]);
      }

      if(digitalRead(sw) == 0)
      {
         for(int i=0;i<patternLenth;i++)
            EEPROM.write(i,pattern[i]);
         while(digitalRead(sw) == 0);
      }

      else
      {
         if(knok == 1)
         {
            for(int i=0;i<patternLenth;i++)
            {
               if(pattern[i] == EEPROM.read(i))
               {
                  Serial.println(acceptFlag++);
               }

               else
               {
                    Serial.println("Break");
                    break;                
               }
            }
         }

         Serial.println(acceptFlag);
         if(acceptFlag >=  patternLenth-1)
         {
             Serial.println(" Accepted");
             myServo.write(openGate);
             delay(5000);
             myServo.write(closeGate);
         }
         else
            Serial.println("Rejected");
      }

      for(int i=0;i<patternLenth;i++)
      {
         pattern[i]=0;
         slot[i]=0;
      }
      slot[i]=0;
      acceptFlag=0;
   }
}

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by Devendra singh on Thu, 07/27/2017 - 16:49

Permalink

sir/mam,
After uploading my code in arduino in serial monitor shows every pattern accepted always.
please solve my issue.

Submitted by BenetDonato on Mon, 03/12/2018 - 16:18

Permalink

Hello, I've read this article and tried to recreate the project using arduino uno and a servo motor sg92r. The codes are working fine, checking with the serial monitor. However, the servo motor is not accepting the program and is not rotating at all. Could you give me some advice on how I can solve this porblem. Thank You

did this code working fine for you or you made some changes because i am doing the same project my servo motor is moving but it is acce[ting every [attern

Submitted by osman on Thu, 05/10/2018 - 17:00

Permalink

how can ı connect the buzzer to ordunio, your connect of buzzer is different than the graphic?
ı couldnt see the buzzer's ground pin in photo or your video? please gıve me brief explain.thank you

Submitted by abhishek on Sun, 05/20/2018 - 13:45

Permalink

you code is not working...in the software ..please send me a perfect code for this project

I finished the circuit but it gives an error when loading to the card. This is the error: "Your work program uses 4282 bytes (13%) storage space. Maximum 32256 bytes.

Global variables use up to 305 bytes of memory (14%). There are 1743 bytes for local variables. Up to 2048 bytes can be used. "

There is no other line on the error screen. How to fix this. Thanks in advance.
I'd appreciate it if you got mail.lordhasan41@gmail.com