Arduino Based RFID Door Lock
In this article, we will learn what RFID is, how it works, and how to make an RFID smart door lock based on the Arduino development board. RFID stands for Radio Frequency IDentification , which is a contactless technology widely used in many industries for tasks such as personnel tracking, access control, supply chain management, library book tracking, and charging systems.
How does RFID work?
An RFID system consists of two main components, a transponder or a tag that is located on the object we want to identify, and a transceiver or reader.
The RFID reader consists of a radio frequency module, a control unit and an antenna coil that generates a high frequency electromagnetic field. On the other side, the tag is usually a passive component, consisting only of an antenna and an electronic microchip, so when it is close to the reader’s electromagnetic field, a voltage is generated in its antenna coil due to induction, which serves as a power supply for the microchip.
Now, when the tag is powered, it can pick up the transmitted message from the reader, and to send the message back to the reader, it uses a technique called load operation. Switching the load at the tag antenna on and off will affect the power consumption of the reader antenna, which can be measured as a voltage drop. This voltage variation will be captured as 1s and 0s, and that is how the data is transferred from the tag to the reader.
There is another way of data transmission between the reader and the tag, which is called backscatter coupling. In this case, the tag uses part of the received power to generate another electromagnetic field, which will be picked up by the antenna of the reader.
RFID and Arduino Development Board
That’s the basic working principle, now let’s see how to build our own RFID door lock using RFID and Arduino development board. We will use tags based on MIFARE protocol and MFRC522 RFID reader, which costs only a few dollars.
These tags have 1kb of memory and have a microchip that can perform arithmetic operations. They operate at a frequency of 13.56 MHz and can operate at distances of up to 10 cm, depending on the geometry of the antenna. If we bring them in front of a light, we can notice the antenna and microchip we talked about above.
As for the RFID reader module, it uses the SPI protocol to communicate with the Arduino board and that is how we need to connect them. Note that we have to connect the VCC of the module to 3.3V, while for the other pins, we don’t have to worry, they are 5V tolerant.
After connecting the module, we need to download the MFRC522 library from GitHub . The library comes with several good examples from which we can learn how to use the module.
First, we can upload the “DumpInfo” example and test that our system is working. Now, if we run the serial monitor and bring a tag near the module, the reader will start reading the tag and all the information on the tag will be displayed on the serial monitor.
Here we can notice the UID number of the tag and the 1 KB of memory, which is actually divided into 16 sectors, each sector is divided into 4 blocks, and each block can store 2 bytes of data. In this article, we will not use any of the tagged memory, we will only use the tagged UID number.
Arduino-based RFID door lock control example
Before we start writing code for the RFID door lock, let’s take a look at the components and circuit schematic for the example.
In addition to the RFID module, we will also use a proximity sensor to detect whether the door is closed or open, a servo motor for the locking mechanism, and a character display.
The components required for this example are as follows:
● MFRC522 RFID module
● Servo motor
● LCD display
● Arduino development board
● Breadboard and jumper wires
● Proximity sensor CNY70
The workflow of the example is as follows: First, we have to set the master tag, then the system goes into normal mode. If we scan an unknown tag, access will be denied, but if we scan the master, we will go into program mode, from which we can add and authorize unknown tags. So, now if we scan the tag again, access will be granted so that we can open the door.
The door automatically locks when we close it. If we ever want to delete a tag from the system, we just need to go into program mode again, scan for known tags, and delete them.
Code
Now let’s take a look at the code. First, we need to include the libraries for the RFID module, display, and servo motor, define some variables required for the following program, and create an instance of the library.
In the setup function section, first we initialize the module and set the initial value of the servo motor to the locked position. Then we print the initial message to the display, and then using the following “while” loop, we wait for the main tag to be scanned. The getID() custom function gets the tag UID, which we put into the first position of the myTags[0] array.
Let’s take a look at the getID() custom function. First, it checks if there is a new tag near the reader, if so, we continue with the “for” loop, which will get the UID of the tag. The tags we are using have 4 bytes for the UID number, that’s why we need 4 iterations with this loop, and using the concat() function we add the 4 bytes to a single String variable. We also set all the characters of the string to uppercase letters, and at the end we stop reading.
Before we enter the main loop, at the end of the setup section, we also call the printNormalModeMessage() custom function which prints an “Access Control” message on the display.
In the main loop, we first read the value of the proximity sensor, which tells us whether the door is closed or not.
So if the door is closed, using the same lines as we described in our getID() custom function, we will scan and get the UID of the new tag. We can notice here that because of the “return” line in the “if” statement, the code will not continue until we scan for a tag.
Once we scan a tag we check to see if that tag is the master tag we registered earlier, if it is, we enter program mode. In this mode, if we scan a tag that is already authorized it will be removed from the system, or if the tag is unknown it will be authorized to be added to the system.
Outside of program mode, through the next “for” loop, we check if the scanned tag is the same as any registered tag, and we either unlock the door or deny access. At the end of the “else” statement, we wait until the door is closed, then we lock the door and print the normal mode message again.
The above is the full content of this article. If you have any questions, please leave a reply below this post.