Arduino - RFID/NFC Door Lock System

In this tutorial, we are going to learn how to make Arduino RFID/NFC Door Lock System using Arduino, RFID/NFC RC522 Kit, and electromagnetic lock.

※ NOTE THAT:

We also made the door lock system using keypad tutorial .

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×RFID/NFC RC522 Kit (reader + tags)
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
10×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About RFID/NFC RC522 Module and Electromagnetic Lock

If you do not know about RFID/NFC RC522 Module and Electromagnetic Lock (pinout, how it works, how to program ...), learn about them in the following tutorials:

System Components

The door lock system includes two main parts:

  • Door Lock: Arduino + RFID/NFC reader + Electromagnetic lock
  • Door Key: RFID/NFC tags
  • Arduino arduino rfid door lock component

There are some types of RFID/NFC readers that can work with Arduino. This tutorial uses RFID-RC522 as an RFID/NFC reader because it is cheap.

How RFID/NFC Door Lock Works

  • Some tags that have UIDs set in Arduino code acts as the authorized keys
  • User taps an RFID/NFC tag on RFID/NFC reader
  • The reader reads UID from the tag.
  • Arduino gets the UID from the reader
  • Arduino compares the read UID with the predefined UIDs
  • If the UID is matched with one of the predefined UIDs, Arduino deactivates the electromagnetic lock to unlock the door.

Wiring Diagram

Arduino RFID RC522 Door Lock System wiring diagram

Image is developed using Fritzing. Click to enlarge image

Arduino Code - Single Key

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-door-lock-system */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 #define RELAY_PIN A5 // the Arduino pin connects to relay MFRC522 rfid(SS_PIN, RST_PIN); byte keyTagUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, HIGH); // lock the door Serial.println("Tap RFID/NFC Tag on reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); if (rfid.uid.uidByte[0] == keyTagUID[0] && rfid.uid.uidByte[1] == keyTagUID[1] && rfid.uid.uidByte[2] == keyTagUID[2] && rfid.uid.uidByte[3] == keyTagUID[3] ) { Serial.println("Access is granted"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.print("Access denied for user with UID:"); for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(rfid.uid.uidByte[i], HEX); } Serial.println(); } rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }

Quick Steps

Because UID is usually not printed on RFID/NFC tag, The first step we need to do is to find out the tag's UID. This can be done by:

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Tap an RFID/NFC tag on RFID-RC522 module
  • Get UID on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Access denied for user with UID: 3A C9 6A CB
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

After having UID:

  • Update UID in the line 18 of the above code. For example, change byte keytagUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte keytagUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Upload the code to Arduino again
  • Tap an RFID/NFC tag on RFID-RC522 module
  • See output on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Access is granted
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • Check the electromagnetic lock, it should be unlocked
  • Tap another RFID/NFC tag on RFID-RC522 module
  • See output on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Access is granted Access denied for user with UID: BD 1E 1D 00
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

※ NOTE THAT:

  • To make it easy to test, the unlocked time is set to 2 seconds, it should be increased in practical use/demonstration

Arduino Code - Multiple Keys

Let's imagine that a room allows the manager and secretary to unlock the door only.

In this case, we need to use two RFID/NFC tag: one for the manager and the other for the secretary. We have to specify the UIDs of two tags in the code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-door-lock-system */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 #define RELAY_PIN A5 // the Arduino pin connects to relay MFRC522 rfid(SS_PIN, RST_PIN); byte managerKeyUID[4] = {0x3A, 0xC9, 0x6A, 0xCB}; byte secretaryKeyUID[4] = {0x30, 0x01, 0x8B, 0x15}; void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, HIGH); // lock the door Serial.println("Tap RFID/NFC Tag on reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); if (rfid.uid.uidByte[0] == managerKeyUID[0] && rfid.uid.uidByte[1] == managerKeyUID[1] && rfid.uid.uidByte[2] == managerKeyUID[2] && rfid.uid.uidByte[3] == managerKeyUID[3] ) { Serial.println("Access is granted for manager"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else if (rfid.uid.uidByte[0] == secretaryKeyUID[0] && rfid.uid.uidByte[1] == secretaryKeyUID[1] && rfid.uid.uidByte[2] == secretaryKeyUID[2] && rfid.uid.uidByte[3] == secretaryKeyUID[3] ) { Serial.println("Access is granted for secretary"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.print("Access denied for user with UID:"); for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(rfid.uid.uidByte[i], HEX); } Serial.println(); } rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }

Do the similar steps as the above, and then tap one by one tag on RFID-RC522 module. The result on Serial Monitor looks like below:

COM6
Send
Tap RFID/NFC tag on reader Access is granted for secretary Access is granted for manager
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can extend the above code for three, four, or more tags.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES