Arduino-Based RFID Door Lock System

Arduino-Based RFID Door Lock System

Secure Your Home with an Arduino-Based RFID Door Lock System – Easy DIY Guide

This blog presents a complete project on building an RFID-based door lock security system using Arduino, covering every essential detail required to successfully implement the project.

When it comes to enhancing door security, several technologies are available, such as RFID, fingerprint authentication, facial recognition, and keypad-based systems. Among these, RFID (Radio Frequency Identification) is widely preferred because it is efficient, reliable, and requires minimal memory.

An RFID system mainly consists of two components: an RFID reader and RFID tags. The reader scans the tag’s unique identification code and sends it to the controller. Each RFID tag has a unique ID, which the system verifies before taking action.

How Does an RFID System Work?

An RFID reader module continuously emits high-frequency radio waves (measured in megahertz). The RFID tag contains a small coil made of spiral copper traces. When the tag is brought close to the reader, the emitted radio waves induce a current in the coil. This induced electromagnetic force powers the chip inside the tag.

Once powered, the tag transmits its unique RFID code using frequency modulation. The reader receives this modulated signal, decodes it, and sends the data to the microcontroller using standard communication protocols such as SPI, UART, or I²C.

Types of RFID Systems

There are two main types of RFID systems:

  • Active RFID:
    These tags contain an internal battery and operate at higher frequencies, allowing detection over several meters.

  • Passive RFID:
    These tags do not have a battery and rely on the reader’s electromagnetic field for power. Their detection range is typically 5–10 cm, which is ideal for door lock applications.

In this project, a passive RFID system is used.

RFID Reader Module Used

The RFID reader module used in this project is the RC522. It supports SPI, UART, and I²C communication protocols. In this project, the SPI protocol is used.

Components Required

Hardware Components

  • Arduino Nano with USB cable

  • RC522 RFID reader module

  • RFID tags (2 tags included with the module)

  • 12V DC solenoid cabinet door lock

  • IRF540 N-channel MOSFET (1 pc)

  • 10kΩ resistor (1 pc)

  • 100kΩ resistor (1 pc)

  • 1kΩ resistor (1 pc)

  • 3mm LED (orange) (1 pc)

  • Mini breadboard (1 pc)

  • 12V 5A power adapter

  • Jumper wires (F–F, F–M, M–M)

(REFER TO CIRCUIT DIAGRAM ABOVE)

RFID Reader Connections

The RC522 uses the SPI (Serial Peripheral Interface) protocol, which includes four main pins:

  • SCK (Serial Clock) → Arduino Nano pin 13

  • MISO (Master In Slave Out) → Arduino Nano pin 12

  • MOSI (Master Out Slave In) → Arduino Nano pin 11

  • SDA (Slave Select / SS) → Arduino Nano pin 10

An LED is connected in parallel to Arduino pin 2 to indicate when a valid RFID card is detected.

Why Is a MOSFET Used?

The Arduino Nano detects a registered RFID tag and sets pin 2 HIGH. However, Arduino output pins provide only 5V with a maximum current of 40mA. The solenoid door lock requires 12V and approximately 0.8A, which cannot be supplied directly by the Arduino.

To overcome this limitation, an IRF540 N-channel MOSFET is used. This MOSFET can handle high voltage (up to 100V) and high current (up to 30A). The Arduino controls the MOSFET gate using pin 2. When the pin goes HIGH, the MOSFET switches ON, allowing current to flow through the solenoid, retracting the latch and unlocking the door.

RFID Library Installation

After completing the hardware connections, install the RC522 RFID Reader Library.

Then follow these steps in Arduino IDE:

  1. Open Arduino IDE

  2. Click Sketch → Include Library → Add ZIP Library

  3. Select the downloaded ZIP file

  4. The library will be installed automatically

Arduino Code

Upload the following code after installing the library:


#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define LED 2
#define NUM_ID 3

String reg_tag_IDs[NUM_ID] = {"4A6C7E12","D9DB1916","C153FB1D"};
String tagID = "";

MFRC522 mfrc522(SS_PIN, RST_PIN);

int c = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
}

void loop()
{
  if(readID())
  {
    Serial.println(tagID);
    for(int i = 0; i < NUM_ID; i++)
    {
      if(tagID == reg_tag_IDs[i])
      {
        digitalWrite(LED, HIGH);
        delay(3000);
        digitalWrite(LED, LOW);
      }
      else
      {
        c++;
      }
    }

    if(c == NUM_ID)
    {
      Serial.println("Access denied. ID not registered.");
    }
    c = 0;
  }
}

boolean readID()
{
  if(!mfrc522.PICC_IsNewCardPresent()) return false;
  if(!mfrc522.PICC_ReadCardSerial()) return false;

  tagID = "";
  for(uint8_t i = 0; i < 4; i++)
  {
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA();
  return true;
}

Adding RFID Tag IDs

After uploading the code, open the Serial Monitor and tap an RFID tag on the reader. The tag’s 4-byte UID will appear on the screen. Copy this code and add it to the reg_tag_IDs array.

Multiple tags can be added by separating them with commas, for example:
{"4A6C7E12", "D9DB1916", "C153FB1D"}

How Many Tag IDs Can Be Stored?

Arduino has limited memory. Each RFID tag ID consumes approximately 7 bytes. With about 30 KB of available memory, you can store roughly 3000 RFID tag IDs.

Conclusion

In this project, we successfully built an RFID-based door lock system using Arduino Nano. The door unlocks only when a registered RFID tag is detected, ensuring enhanced security.

If you have any questions or need clarification on any part of this project, feel free to leave a comment. Our team will be happy to help you.

Back to blog