Pages

Tuesday, January 10, 2017

Arduino 2-in-1 Door Lock System - RFID and PIN

RFID (Radio Frequency IDentification) has many real world applications. One of them is in locking and unlocking systems which scan the RFID tag and compare the information to the already stored information in the database and decide whether to unlock the door or not.


Such a system is designed by our team of four members (I, Hamza Ghaffar, Hamza Sumbal and Muhammad Bisaam) as an end semester project for the course "Electrical Network Analysis".


Video Demo:
The demonstration video is given below



Details are given in the following:

Components used:
An Arduino Mega 2560 board
An RFID RC522 module
A 4x4 keypad
A 16x2 Liquid Crystal Display for Arduino
A potentiometer
A breadboard
Jumper wires

Pin numbering of different components is given below:
Keypad
Arduino Mega 2560
4 row pins
2, 3, 4, 5
4 column pins
6, 7, 8, 9
LCD
Arduino Mega 2560
VSS
GND
VDD
5V
Vo
5V through potentiometer
RS(Reset)
24
RW
GND
E(Enable)
25
D4, D5, D6, D7 (data pins)
26, 27, 28, 29
A
5V through 330 ohm resistance
K
GND
RFID RC522 Module
Arduino Mega 2560
3.3V
3.3V
RST
48
GND
GND
SDA
53
SCK
52
MOSI
51
MISO
50
Green LED’s +ve terminal
40
Red LED’s +ve terminal
41

Arduino Code:


 /*  
 Fahad bin Khalid  
 Hamza Sumbal  
 Hamza Ghaffar  
 Muhammad Bisaam  
 */  
 #include <SPI.h>  
 #include <MFRC522.h>  
 #include <Keypad.h>  
 #include <LiquidCrystal.h>  
 const byte numRows= 4; //number of rows on the keypad  
 const byte numCols= 4; //number of columns on the keypad  
 //keymap defines the key pressed according to the row and columns just as appears on the keypad  
 char keymap[numRows][numCols]=   
 {  
 {'1', '2', '3', 'A'},   
 {'4', '5', '6', 'B'},   
 {'7', '8', '9', 'C'},  
 {'*', '0', '#', 'D'}  
 };  
 //Code that shows the the keypad connections to the arduino terminals  
 byte rowPins[numRows] = {2, 3, 4, 5}; //Rows 0 to 3  
 byte colPins[numCols]= {6, 7, 8, 9}; //Columns 0 to 3  
 //initializes an instance of the Keypad class  
 Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);  
 // initialize the library with the numbers of the interface pins  
 LiquidCrystal lcd(24, 25, 26, 27, 28, 29);  
 #define SS_PIN 53  
 #define RST_PIN 48  
 MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class  
 MFRC522::MIFARE_Key key;   
 // Init array that will store new NUID   
 byte nuidPICC[4];  
 byte knownTac0[4] = { 246,95,236,213};  
 byte knownTac1[4] = {150,014,65,59};  
 byte knownTac2[4] = {102,27,07,73};//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
 boolean KNOW = true;  
 int id;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
 int redpin = 41;  
 int greenpin = 40;  
 void setup() {   
  Serial.begin(9600);  
  pinMode(redpin, OUTPUT);  
  pinMode(greenpin, OUTPUT);  
  // set up the LCD's number of columns and rows:  
  lcd.begin(16, 2);  
  SPI.begin(); // Init SPI bus  
  rfid.PCD_Init(); // Init MFRC522   
  // Print a message to the LCD and Serial.  
  lcd.clear();  
  lcd.print("Waiting for Card");  
  Serial.println("Waiting for Card");  
  digitalWrite(redpin, HIGH);  
 }  
 void loop() {  
  // Look for new cards  
  if ( ! rfid.PICC_IsNewCardPresent())  
   return;  
  // Verify if the NUID has been read  
  if ( ! rfid.PICC_ReadCardSerial())  
   return;  
  lcd.clear();  
  lcd.print("Card Detected");  
  Serial.println(F("Card Detected"));  
  delay(1000);  
  // Store NUID into nuidPICC array  
  for (byte i = 0; i < 4; i++) {  
   nuidPICC[i] = rfid.uid.uidByte[i];  
  }  
  // comparing IDs  
  if (knownTac0[0] == nuidPICC[0] &&  
    knownTac0[1] == nuidPICC[1] &&  
    knownTac0[2] == nuidPICC[2] &&  
    knownTac0[3] == nuidPICC[3]){  
    KNOW = true;  
    id =0 ; // first case  
  }  
  else if(knownTac1[0] == nuidPICC[0] &&  
      knownTac1[1] == nuidPICC[1] &&  
      knownTac1[2] == nuidPICC[2] &&  
      knownTac1[3] == nuidPICC[3]){  
    KNOW = true;  
    id = 1; // 2nd case  
  }  
  else if(knownTac2[0] == nuidPICC[0] &&  
      knownTac2[1] == nuidPICC[1] &&  
      knownTac2[2] == nuidPICC[2] &&  
      knownTac2[3] == nuidPICC[3]){  
    KNOW = true;  
    id = 2; // 3rd case  
  }  
  else {  
   KNOW = false;  
   id=99; // default case  
  }  
  bool success = false;  
   switch(id){  
   case 0:  
   lcd.clear();  
   lcd.print("Sumbal");  
   Serial.println(F("Sumbal"));  
   delay(1000);  
   success = keypad_loop("1111");  
   break;  
   case 1:  
   lcd.clear();  
   lcd.print("Hamza");  
   Serial.println(F("Hamza"));  
   delay(1000);  
   success = keypad_loop("2222");  
   break;  
   case 2:  
   lcd.clear();  
   lcd.print("FAHAD");  
   Serial.println(F("FAHAD"));  
   delay(1000);  
   success = keypad_loop("3333");  
   break;  
   default:  
   lcd.clear();  
   lcd.print("Not Found");  
   Serial.println("Not found");  
   delay(1000);  
  }  
  if(success){  
   lcd.clear();  
   lcd.print("Door Unlocked!");  
   Serial.println("Door Unlocked!");  
   digitalWrite(redpin, LOW);  
   digitalWrite(greenpin, HIGH);  
  }  
  else{  
   lcd.clear();  
   lcd.print("Try again!");  
   Serial.println("Try again!");  
  }  
  // Halt PICC  
  rfid.PICC_HaltA();  
  // Stop encryption on PCD  
  rfid.PCD_StopCrypto1();  
  // Print a message to the LCD and Serial.  
  delay(2000);  
  lcd.clear();  
  lcd.print("Waiting for Card");  
  Serial.println("Waiting for Card");  
  digitalWrite(greenpin, LOW);  
  digitalWrite(redpin, HIGH);  
 } // void loop() ends  
 ///////////////////////////////////////////////////////////////////////////////////////  
 bool keypad_loop(String PIN){ // PIN section  
  lcd.clear();  
  lcd.print("Enter PIN...");  
  Serial.println("Enter PIN...");  
  String input;  
  int i = 0;  
  bool control = true;  
  bool success = false;  
  while(control == true){  
  char keypressed = myKeypad.getKey();  
  if (keypressed != NO_KEY){  
   if(i<4){ // 4-digit input  
    if(i==0)lcd.clear();  
    i++;  
    input.concat( String(keypressed) );  
    lcd.print("*");  
   }  
   if(i==4){  
    control = false;  
    if(input.equals(PIN) ){  
     success = true;  
    }  
  }  
  } // NO_KEY if ends  
  } // while ends  
  return success;  
 }  

Please feel free to comment below if you find something inaccurate or awesome :-)

13 comments:

  1. Please tell me your email-id!I need Help! The Code doesn't work with success=keypad_loop
    It calls the function indefinite time and it code doesn't work with that!

    ReplyDelete
  2. Hey, great blog, but I don’t understand how to add your site in my rss reader. Can you Help me please? Garage Door Spring Repair Houston TX

    ReplyDelete
  3. https://blog.wildfiction.com/2011/06/i-was-recently-lucky-enough-to-be.html

    ReplyDelete
  4. He was very expert in addition to enjoyable and i will be phoning her or him with regard to future function.24/7 DC

    ReplyDelete
  5. The point you talk about in your post is wonderful and adore understanding it. A debt of gratitude is in order for sharing.
    Locksmith Dublin

    ReplyDelete
  6. Hi. I discovered your blog utilizing msn. This is a greatly elegantly composed article.
    best keyless door locks

    ReplyDelete
  7. Most of the crime incidences such as theft and intrusion happen due to our negligence. Therefore, it is necessary to make sure that you have protected all the doors and windows of your home, shop, or office. I live in Richmond, and I know the local locksmiths very well, because they have assisted my friends multiple times. Now, I have saved the phone number of Jude Webb, the best locksmith service provider in the region, as it helps calling him for rekeying door locks much easily.

    ReplyDelete
  8. On the off chance that you ever need to take a portion of the heap off,
    https://www.starnfc.com/major-applications-of-rfid/

    ReplyDelete
  9. and make sure that I can operate it remotely.
    Once, I was locked out my home when I returned from office, because I lost the key unknowingly, so I requested my close friend to find a locksmith immediately, and he advised me to click Locksmithboksburg.com and ask them for quick assistance. I did not have to wait for longer, because their emergency response services are very efficient, and they helped rekeying the lock in a hassle free manner.

    ReplyDelete
  10. Have just been confronted with a window problem? Window repair is the smart choice to make front door repairs Goodwindowworks Damp air may be seeping through the windows

    ReplyDelete
  11. I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. GT Locksmith

    ReplyDelete