Arduino RFID Door Lock: An Impenetrable Electronic Door Locking Mechanism

An Arduino RFID door lock project is ideal for prototyping access control systems for electronic door locks. The technology holds water because the system is impenetrable; no one can pick electronic door locks. Although you …

An RFID electronic door lock project with a servo motor connected

Home » Arduino RFID Door Lock: An Impenetrable Electronic Door Locking Mechanism

An Arduino RFID door lock project is ideal for prototyping access control systems for electronic door locks.

The technology holds water because the system is impenetrable; no one can pick electronic door locks.

Although you can use a fingerprint or PIN system, RFID locks are better because they are: 

  • Cheaper than fingerprint access control systems
  • More secure than PIN systems
  • Have more applications than PIN systems (for instance, access control and attendance)

So we’ll build an Arduino RFID door lock to give you a starting point for developing and expanding the system.

Let’s get right into it!

Table of Contents

How RFID Works

RFID (Radio Frequency Identification) is a contactless technology with two primary components.

  • Tag/transponder: A passive component on the identifiable object (consists of a microchip and antenna)
  • Reader/transceiver: Packs in an antenna, control unit, and radio frequency module

The reader generates a high-frequency magnetic field using its antenna to induce a voltage in the transponder’s antenna.

Tags don’t have power sources, so the induced voltage powers their circuits and microchips.

An up-close image of the antenna and circuit inside an RFID card

An up-close image of the antenna and circuit inside an RFID card

This power enables the chip to extract the incoming message from the reader and send the response.

The transponder uses either of these techniques to send the identification data back to the reader.

Load Modulation

This technique involves switching the load at the antenna on and off, affecting the tag’s power consumption.

The switching forms voltage fluctuations that create logic highs and lows that match the UID data.

So the reader gets the message via load manipulation.

Backscattered Coupling

With backscattered coupling, the transponder utilizes the power it receives from the transceiver to generate an electromagnetic field.

This wave has the UID data encoded inside and gets sent back to the reader.

Arduino RFID Door Lock Project

We will run the project using an MFRC522 RFID transceiver and a transponder based on the MIFARE protocol.

These tags feature 1KB memory and 13.56 MHz operating frequency with a 10cm range max.

A person using an RFID key card to access a building

A person using an RFID key card to access a building

On the other hand, the transceiver uses the SPI protocol to communicate with the Arduino board. So we connect its MOSI, MISO, SCK, and SS pins as follows.

Arduino MicrocontrollerMFRC522 RFID Receiver
3.3V3.3V
GNDGND
9RST
12MISO
11MOSI
13SCK
10SDA

This RFID transceiver runs on 3.3V, so don’t connect it to the 5V pin. And don’t connect the NC pin to Arduino.

You can test this setup before connecting the other components using the examples in this MFRC522 library.

For instance, the DumpInfo example will display the tag information on the serial monitor when within range.

The MFRC522 RFID reader module

The MFRC522 RFID reader module

Other Required Components

Besides the Arduino board and RFID kit, we’ll use the following components.

For the Arduino board, we’ll use the NANO. But you can use the Arduino UNO.

The purpose of the servo motor is to operate the door locking/opening mechanism.

On the other hand, the proximity sensor checks if the door is open or shut.

Lastly, the 16×2 character display shows the operation status.

A 16x2 LCD

A 16×2 LCD

You can build the project using a solenoid and relay, but we’ll use a servo motor for this project.

Hardware Connections

Arduino BoardLCD Display Screen
5VVDD and LED+
GNDVSS, RW, and LED-
D2RS
D3E
D4D4
D5D5
D6D6
D7D7

Connect a potentiometer to the VEE pin to adjust the LCD contrast.

Arduino BoardCNY70
5V5V and 5V
GNDGND and GND
A0A0

Complete the process by connecting the motor’s yellow signal line to the Nano’s D8 pin. But drive it using an external power supply.

Using the 5V Arduino power supply pin can damage the board because the motor draws a lot of power.

Circuit Diagram

What We Are About To Build

The project has a straightforward workflow that begins with a master tag.

Once launched, we must set the master tag to control the system.

After that, the program switches to normal mode.

But we don’t have slave tags to operate the opening mechanism.

So whenever you scan the master tag, the system goes into program mode.

In this mode, you can add an unknown transponder to authorize it to unlock the system by scanning it.

An RFID receiver with two tags (card and key fob). You can use one as a master tag and the other as a slave tag

An RFID receiver with two tags (card and key fob). You can use one as a master tag and the other as a slave tag.

So next time you scan this transponder, it will open the door.

What if you want to remove a tag?

Scan the master tag to go to program mode, then scan the already registered tag. The system will erase its UID from the system.

Code

Compile the code below on the Arduino IDE, then upload it to the board.

Definitions

Void Setup

Void Loop

Custom Functions

Code Explanation

We’ll begin the code by including the required libraries for the following.

  • SPI communication between the RFID receiver and Arduino
  • RFID module
  • LCD
  • Servo motor

The following lines of code define the RST and SS RFID pins connected to Arduino pins 9 and 10.

After that, we declare the variables and instances (objects).

Void Setup

The setup function initializes the system, beginning with the modules.

It sets the servo motor to 10 (its locking position) and prints the message “No master tag” followed by “Scan Now.”

We want to skip to the loop function after initializing the master tag.

So we use a while loop to keep the setup waiting until it successfully reads the master tag’s ID.

After a successful scan, the program will store the transponder’s UID in the tags array’s first location.

Before going into the loop function, let’s go through the two custom functions at the end.

Custom Functions

The first function (Get_ID) checks if a tag is close to the reader.

If not, it will return 0 (exit). Otherwise, it will go to the for loop to read the tag’s UID.

It goes through four iterations to capture the UID because each takes up four bytes. So it’s one iteration per byte).

The concatenation function joins the four bytes into a single string variable, which gets converted into uppercase.

After that, the code halts reading the card.

The “print normal mode message” function shows the “Access Control” message on the LCD.

Void Loop

This function begins by reading the proximity sensor’s input. This section determines if the door is open or shut.

If shut, the code scans to check for any tag in proximity. The code will not proceed past this point until the receiver scans a tag.

Once scanned, the code checks if the tag is the master tag registered earlier. It compares with the ID stored at the array position myTags[0].

If it is the master, the lock will enter program mode, where you can add or remove a tag.

If you scan an already registered tag, the program will delete its UID from the array.

Additionally, it will display the message “Tag Removed” on the screen.

But if you scan an unknown tag, the program will add the tag’s UID into the array. And it will print the message “Tag Added.”

If the scanned tag is not the master, the code will skip the section above.

It will check whether the UID is available under the registered tags array list.

If available, you will get the message “Access Granted,” and the door will open.

Greenlight after scanning an RFID smart key

Greenlight after scanning an RFID smart key

Otherwise, you will get the message “Access Denied,” and the door will remain locked.

Remember, the first if statement checks if the door is open or closed using the proximity sensor.

You’ll get the message “Door Opened” when you scan the tag if the door opens.

Before concluding, the code checks the boolean variable ”doorOpened.” False means a shut door.

If not open, the program will wait five milliseconds, lock the door, then call the “printNormalModeMessage” function.

This function will display the message “Scan Your Tag” and return to the top of the void loop function.

Wrap Up

As you can see, building an Arduino RFID door lock is a relatively easy project because the components are ready-made.

You only have to assemble them, then write the code above to get the project running. 

Remember, the electronics might work, but intruders can force their way through by pushing the door.

The supporting structure should be able to withstand these forces. So focus on building a sturdy installation after finalizing this lock. 

That’s it for this article. Contact us if you encounter any issues. We’ll be happy to help.