RFID/NFC Door Lock

by taifur in Circuits > Arduino

8453 Views, 65 Favorites, 0 Comments

RFID/NFC Door Lock

cover1.JPG
cover2.JPG
cover3.JPG
cover4.JPG

Oftentimes, we need to secure a room at our home or office (perhaps a secret dexter’s laboratory) so that no one can access the room without our permission and ensure protection against theft or loss of our important accessories and assets. There are so many readymade security systems present today but behind the scene, for authentication they all relay on fingerprint, retina scanner, iris scanner, face id, tongue scanner, RFID reader, password, pin, patterns, etc. Of all the solutions the low-cost one is to use a password or pin-based system. - Nilanjan Roy

It is always a pleasure for a maker to make something by himself. In this project, I am going to share with you how I made a prototype of an RFID/NFC control secure door lock. By following this project you will be able to make a secure door lock by yourself very easily.

Watch the demo video clip:

Supplies

supplies.jpg
double-sided-tissue-tape-500x500.jpg
lcd.jpg
rfid.jpg
  • RYRR10S_Lite UART Interface Evaluation Board: This is an NFC/RFID reader that supports UART protocol means you can interface it with any microcontroller using 2 pins only. The board is also capable to communicate with any microcontroller using SPI protocol. It supports most of the Near Field Communication (NFC) & RFID protocols like ISO/IEC 18092, ISO/IEC 15693, ISO/IEC 14443A, ISO/IEC 14443B. This evaluation board comes with an integrated PCB antenna. You can find details of the board from their official page.
  • Arduino UNO: Arduino UNO microcontroller board will be used to interface the NFC reader, LCD display, and the solenoid lock. Other Arduino instead of UNO will also work. You can buy an Arduino using my affiliated link from Aliexpress.com.
  • LCD Display: A 16x2 character LCD is used in the project to display the lock and authentication status. You can buy one using my affiliated link from Aliexpress.com.
  • Electromagnetic Lock: This is a type of lock made based on electromagnetism. You can operate this type of lock by applying an electric voltage to the input of the lock. Lock for different voltage ranges is available. We used a 12V type for our project. You can buy one from aliexpress.com.
  • Adjustable Boost Converter: As we need 12V for driving the solenoid lock and li-ion battery provided on 3.7V. So, we need a DC-DC converter that can convert 3.7V to 12V. You can buy one from aliexpress.com.
  • 2X18650 Li-ion Battery Case: Li-ion battery provides power to the circuit and we can recharge a li-ion battery. For placing the battery we need a battery holder. You can buy one from aliexpress.com.
  • 18650 LI-ion Battery: You can buy one from my affiliated aliexpress.com link.

Circuit Diagram & Explanation

schematic-rfid-lock.png
16x2lcd.jpg
reader-connenction.jpg
D880-Pinout.png
KSP2222A-Pinout_0.png

The main part of the circuit is the Arduino microcontroller board. The Reyax 13.56MHz NFC reader is connected to Arduino using a soft UART port to read NFC or RFID card. A 16x2 LCD is used to display the lock status and card authorization information. For providing the power to the system a 3.7V Li-ion battery is used but a single Li-ion battery has only 3.7V output that is not enough to drive the circuit. Besides the solenoid lock required 12V to operate. For this reason, a DC-DC boost converter module is used to convert 3.7V to 12V.

As the Arduino pin is not capable of directly turning on or off the solenoid lock, a power BJT is used for switching purposes. You have to use a 1K resistor to the base of the BJT to protect it from burning.

A general-purpose diode is connected in an antiparallel manner to protect the electronics from the high voltage spike generated from the coil of the lock during switching.

The connection between Arduino and Reyax NFC reader is as follows:

  • Connect TX pin of NFC reader to Arduino D4
  • Connect RX pin of the NFC reader to Arduino D5
  • Connect VDD pin of the Reyax reader to Arduino 5V
  • Connect GND to Arduino GND

The connection between LCD and Arduino is as follows:

  • Connect RS pin of the LCD to Arduino D13
  • Connect E pin of the LCD to Arduino D12
  • Connect D4 pin of the LCD to Arduino D11
  • Connect D5 pin of the LCD to Arduino D10
  • Connect D6 pin of the LCD to Arduino D9
  • Connect D7 pin of the LCD to Arduino D8
  • Connect LED-, R/W, GND pin of the LCD to Arduino GND
  • Connect VDD of the LCD to Arduino 5V
  • Connect LED+ to 5V of the Arduino through a 220ohm resistor
  • Connect Vo to GND through a 1K resistor
  • Connect Vo to 5V through a 10K resistor

The 1K and 10K resistors determine the contrast of the LCD. Changing the value will change the LCD contrast.

A single or double Li-ion cell can be used in this project. If you use double cells then they should be connected in series. Connecting series will increase the voltage to around 8V. Some boost converter is not capable to convert 3.7V to 12V properly. In that case, two cells connected in series will be a good solution. For providing power to Arduino you can directly connect battery output to the Vin pin of the Arduino if you used dual-cell battery. If you use a single-cell then the power for Arduino must be taken from the boost converter output. You can also provide power to the Arduino board through the 5V pin of the Arduino. In that case, you must use a 7805 regulator to produce 5V output from the boost converter. If you get 12V output but the solenoid doesn't work then make sure that the boost converter is capable to provide enough current (at least 1A current).

Preparing LCD

IMG_0995.JPG
IMG_0998.JPG
IMG_1022.JPG

The LCD used in this project is a 16X2 character LCD. This is a parallel LCD means data from the microcontroller are sent to LCD parallelly. It can work in 4-bit and 8-bit modes. In this project, I used it as the 4-bit mode to save 4 pins of Arduino. In 4-bit mode, we need to connect RS, EN, D4-D7 (total 6) pins of the LCD to the Arduino.

For attaching the LCD with Arduino I used an 8-line jumper cable. One terminal is soldered with Arduino and another terminal is soldered with an 8 pin male pin-header so that we can place it very easily to the female pin header of Arduino. The images above illustrate the soldering and jumpering.

Preparing Solenoid Driver

IMG_0988.JPG
IMG_0994.JPG

An Arduino pin is not capable to provides the current and voltage required to drive the coil of the solenoid lock. So, we need to use a transistor or relay to indirectly drive the coil using an Arduino signal. Use can use any general-purpose power transistor for this purpose. I tried with D880 and 2N2222. If you use a single 2N2222 transistor it becomes very hot. So, I used two 2N2222 transistors in parallel and it also works fine. You must put a current limiting resistor to the base of the transistor if you use a BJT. For attaching the solenoid with this transistor I used a 2 pin male header.

Connecting With Boost Converter

IMG_1006.JPG
IMG_1012.JPG

We are using a battery for our project. I used 2 li-ion batteries for my project. If you wish you can use 4 1.5V primary cells also. In both cases, you have to use a boost converter that will converter your battery voltage into 12V. A 12V supply is required for driving the lock. But for your Arduino, you only need a 5V supply. Though you can provide 7-24 V to the Arduino through the Vin pin. If you like you can use a 7805 voltage regulator to convert boost converter or battery voltage to 5V and provide the Arduino through the 5V pin. It is better to use an on/off switch so that we can easily turn on or off the device.

Uploading Program

upload-program.png
upload-program1.png

This is the program for our project. We used software serial library for communicating NFC reader using a software define serial port. The hardware serial port is used for debugging purpose. The program has the option to read different types of NFC/RFID cards. Enable or disable the statements according to your card type.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial NFCserial(4, 5); //RX, TX

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int lock_pin = 2;

uint8_t echo_command[2] = {0x55};
uint8_t info_command[2] = {0x01, 0x00};

uint8_t protocol_command_iso15693[4]  =  {0x02, 0x02, 0x01, 0x05}; 
uint8_t protocol_command_iso14443a[4] =  {0x02, 0x02, 0x02, 0x00};
uint8_t protocol_command_iso14443b[4] =  {0x02, 0x02, 0x03, 0x01};
uint8_t protocol_command_iso18092[4]  =  {0x02, 0x02, 0x04, 0x51};

uint8_t detect_command_iso15693[5]  =  {0x04, 0x03, 0x26, 0x01, 0x00}; 
uint8_t detect_command_iso14443a[4] =  {0x04, 0x02, 0x26, 0x07}; 
uint8_t detect_command_iso14443b[5] =  {0x04, 0x03, 0x05, 0x00, 0x00};
uint8_t detect_command_iso18092[7]  =  {0x04, 0x05, 0x00, 0xFF, 0xFF, 0x00, 0x00};

uint8_t received_data[256];
uint8_t received_buf_pos;
uint8_t response_byte;
uint8_t data_len;
boolean received_complete;
String received_id = "";

void init_nfc(){
  NFCserial.begin(57600);
  delay(10);

  Serial.println("Receiving device info...");
  NFCserial.write(info_command, 2);
  delay(100);
  show_serial_data();
  Serial.println("");
  
  delay(2000);
  
  Serial.println("Sending protocol settings...");
  NFCserial.write(protocol_command_iso15693, 4);
  //NFCserial.write(protocol_command_iso14443a, 4);
  //NFCserial.write(protocol_command_iso14443b, 4);
  //NFCserial.write(protocol_command_iso18092, 4);
  delay(100);
  show_serial_data();
  Serial.println("");
  delay(2000);

  lcd.clear();
  lcd.print("Door is locked");
} 


void serial_receive(){
  uint8_t received_char;
  
  while(NFCserial.available()!=0){
    received_char = char (NFCserial.read());

    //the first byte of the received message is the response
    if(received_buf_pos == 0){
      response_byte = received_char;
      }

    //second byte of the received data is the data length
    else if(received_buf_pos == 1){
      data_len = received_char;
      }

    else if(received_buf_pos == 2){
      
      }

    else if(received_buf_pos == 3){
      
      }

    else{
      received_data[received_buf_pos-4] = received_char;
      received_id += received_char; //adding to a string
      }
    received_buf_pos++;

    if(received_buf_pos >= data_len){
      received_complete = true;
      }
    }
}


void scan_tag(){
  received_buf_pos = 0;
  received_complete = false;

  Serial.println("Searching new card...");
  NFCserial.write(detect_command_iso15693, 5);
  //NFCserial.write(detect_command_iso14443a, 4);
  //NFCserial.write(detect_command_iso14443b, 5);
  //NFCserial.write(detect_command_iso18092, 7);
  delay(20);
  if(NFCserial.available()) serial_receive();
  else return;
  if(response_byte == 0x80){
    Serial.println("Tag detected...");
    Serial.print("ID in HEX: ");
    for(int pos=7; pos>=0; pos--){
      Serial.print(received_data[pos], HEX);
      }
      Serial.println("");
    Serial.print("ID in DEC: ");
    Serial.println(received_id);    
    delay(2000); //this is important
    }
   else{
    Serial.println("No tag detected.");
    }
   //comparing the authorize id
   if(received_id == "19660247350142242481990"){
    lcd.clear();
    lcd.print("Authorized");
    delay(2000);
    //open the lock
    Serial.println("lock open");
    digitalWrite(lock_pin, HIGH);
    lcd.clear();
    lcd.print("Lock is open");
    delay(10000);
    digitalWrite(lock_pin, LOW);
    lcd.clear();
    lcd.print("Lock is closed");
    delay(1000);
    received_id = "";
    delay(2000);
    }
}


void setup() {  
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Initializing...");
  pinMode(lock_pin, OUTPUT);
  delay(5000);
  init_nfc();
}


 
void loop() {  
  scan_tag();
  delay(100);
}

// this display the debug message
void show_serial_data()
{
  while(NFCserial.available()!=0)  /* If data is available on serial port */
  Serial.print(NFCserial.read(), HEX); /* Print character received on to the serial monitor */
}

Downloads

Connecting Reyax RFID/NFC Reader

IMG_1016.JPG
IMG_1020.JPG
IMG_1039.JPG

We are making an RFID control lock. SO, for reading an RFID card we must use an RFID reader. Different types of RFID readers and cards are available. I am using a UART RFID reader manufactured by Reyax and it is based on the popular CR95HF chip from ST semiconductor. I found this reader can detect a card up to 10cm distance that gives you huge flexibility on RFID-based system design.

Assembling & Testing

IMG_1026.JPG
IMG_1027.JPG
IMG_1029.JPG
IMG_1034.JPG
IMG_1045.JPG

This is the final stage of making this demo project. We use several modules and components in our project. So, we need to place all things together. For this purpose, I chose an ebonite board and fixed all the modules, and lock into the board using double-sided tape and hot glue.

After fixing all the parts and I powered up the device. I found it is perfectly working. Images are added above.

Thank you for reading this blog attentively.