Arduino based RFID Door Lock

In this tutorial we will learn how to make an Arduino based RFID door lock. The following video shows the results of this project read the tutorial below for more details.

 Overview 

RFID stands for Radio Frequency Identification and it’s a non-contact technology that’s broadly used in many industries for tasks such as personnel tracking, access control, supply chain management, books tracking in libraries, tollgate systems and so on. When you have finished reading this tutorial, you will be familiar with:

  • Connecting and using a Servo motor
  • Connecting and using a I2C LCD
  • Connecting and using a MFRC522 RFID Reader
  • Connecting and using a RGB LED

What You Will Need

We will need the following parts and libraries in this tutorial:

Parts:

Libraries:

The following libraries will be used in this tutorial. Please refer to this tutorial on how to add an Arduino library in eclipse

We will be using Eclipse for Arduino, if you haven’t setup please refer to my previous tutorial on eclipse for Arduino.

RFID and Arduino

Let’s see how we can use RFID with Arduino and build our own RFID door lock. We will use tags that are based on the MIFARE protocol and the MFRC522 RFID reader, which cost just a couple of shillings.

These tags have 1kb of memory and have a microchip that can do arithmetic operations. Their operating frequency is 13.56 MHz and the operating distance is up to 10 cm depending on the geometry of antenna.

As for the RFID reader module, it uses the SPI protocol for communication with the Arduino board and here’s how we need to connect them. Please note that we must connect the VCC of the module to 3.3V and as for the other pins we don’t have to worry as they are 5V tolerant.

RFID Reader pin Arduino Nano pin
3.3V 3.3V
RST D9
GND GND
IRQ No connection
MISO D12
MOSI D11
SCK D13
SDA/SS D10

 

Arduino RFID Door Lock Access Control Project

Before we go through the code of our RFID door lock project, let’s take a look at the components and the circuit schematics of this project.

The project has the following workflow: First we have to set a master tag and then the system goes into normal mode. If we scan an unknown tag the access will be denied, but if we scan the master we will be granted access so we can open the door. The door will automatically lock after we close the door.

 

Source Code

Now let’s take a look at the code. So first we need to include the libraries for the RFID module, the display and the servo motor, define some variables needed for the program below as well as create the instances of the libraries.

In the setup section, first we initialize the modules, and set the initial value of the servo motor into a lock position. Then we print the initial message to the display and with the following “while” loop we wait until a master tag is scanned. The getID() custom function gets the tag UID and we put it into the first location of the myTags[0] array

Let’s take a look at the getID() custom function. First it checks whether there is a new tag placed near the reader and if so we will continue to the “for” loop which will get the UID of the tag. The tags that we are using have 4 byte UID number so that’s why we need to do 4 iterations with this loop, and using the concat() function we add the 4 bytes into a single String variable. We also set all characters of the string to upper cases and the end we stop the reading.

Next let’s look at the getMotion( ) custom function this checks whether there is a person moving near the system.

Below the getID() function there are four other functions, these work as below.

The printScanTagMessage( ) prints a message on the LCD to inform the incoming person to scan their tags.

The printAddTagMessage( ) prints a message on the LCD to inform the add the master Card.

The printHomeMessage( ) prints a message on the LCD to when the system is Idle .

The  Alert( Color _color, long _interval = 200, long _delay = 1500 ) is used to blink any LED on the RBG LED module at any desired frequency and duration.

The parameter color defines the desired color which can be any of colors in the enum datatype below.

The parameter interval is the frequency at which the LED blinks and finally the delay is the duration the blinking of a LED takes

In the main loop we start with reading the value of the motion sensor, which tell us whether the there is a person in front of the door or not.

So if there is a person, the system will ask the person to scan his card. We can notice here that the code won’t proceed any further for 5000 miliseconds until we scan a tag because of the “return” lines in the “if” statements.

Once we have the tag scanned we check whether that tag is the master that that we previously registered, and if that’s true the system will display access granted and flash the GREEN LED then  open the door. In case we use a different card from the master ,the system will display access denied and flash the RED LED. Finally the system goes back to the home screen to wait for another person.

So that’s pretty much everything and here’s the complete code of the project: