Arduino projects

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3. #include <LiquidCrystal.h>
  4. #include <Servo.h>
  5. #define RST_PIN 9
  6. #define SS_PIN 10
  7. byte readCard[4];
  8. char* myTags[100] = {};
  9. int tagsCount = 0;
  10. String tagID = “”;
  11. boolean successRead = false;
  12. boolean correctTag = false;
  13. int proximitySensor;
  14. boolean doorOpened = false;
  15. // Create instances
  16. MFRC522 mfrc522(SS_PIN, RST_PIN);
  17. LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Parameters: (rs, enable, d4, d5, d6, d7)
  18. Servo myServo; // Servo motor

Copy code

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.

  1. void setup() {
  2.   // Initiating
  3.   SPI.begin(); // SPI bus
  4.   mfrc522.PCD_Init(); // MFRC522
  5.   lcd.begin(16, 2); // LCD screen
  6.   myServo.attach(8); // Servo motor
  7.   myServo.write(10); // Initial lock position of the servo motor
  8.   // Prints the initial message
  9.   lcd.print(“-No Master Tag!-“);
  10.   lcd.setCursor(0, 1);
  11.   lcd.print(” SCAN NOW”);
  12.   // Waits until a master card is scanned
  13.   while (!successRead) {
  14.     successRead = getID();
  15.     if ( successRead == true ) {
  16.       myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
  17.       lcd.clear();
  18.       lcd.setCursor(0, 0);
  19.       lcd.print(“Master Tag Set!”);
  20.       tagsCount++;
  21.     }
  22.   }
  23.   successRead = false;
  24.   printNormalModeMessage();
  25. }

Copy code

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.

  1. uint8_t getID() {
  2.   // Getting ready for Reading PICCs
  3.   if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  4.     return 0;
  5.   }
  6.   if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  7.     return 0;
  8.   }
  9.   tagID = “”;
  10.   for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
  11.     readCard[i] = mfrc522.uid.uidByte[i];
  12.     tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  13.   }
  14.   tagID.toUpperCase();
  15.   mfrc522.PICC_HaltA(); // Stop reading
  16.   return 1;
  17. }

Copy code

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.

  1. void printNormalModeMessage() {
  2.   delay(1500);
  3.   lcd.clear();
  4.   lcd.print(“-Access Control-“);
  5.   lcd.setCursor(0, 1);
  6.   lcd.print(” Scan Your Tag!”);
  7. }

Copy code

In the main loop, we first read the value of the proximity sensor, which tells us whether the door is closed or not.

  1. int proximitySensor = analogRead(A0);

Copy code

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.

  1. // Checks whether the scanned tag is the master tag
  2.     if (tagID == myTags[0]) {
  3.       lcd.clear();
  4.       lcd.print(“Program mode:”);
  5.       lcd.setCursor(0, 1);
  6.       lcd.print(“Add/Remove Tag”);
  7.       while (!successRead) {
  8.         successRead = getID();
  9.         if ( successRead == true ) {
  10.           for (int i = 0; i < 100; i++) {
  11.             if (tagID == myTags[i]) {
  12.               myTags[i] = “”;
  13.               lcd.clear();
  14.               lcd.setCursor(0, 0);
  15.               lcd.print(” Tag Removed!”);
  16.               printNormalModeMessage();
  17.               return;
  18.             }
  19.           }
  20.           myTags[tagsCount] = strdup(tagID.c_str());
  21.           lcd.clear();
  22.           lcd.setCursor(0, 0);
  23.           lcd.print(” Tag Added!”);
  24.           printNormalModeMessage();
  25.           tagsCount++;
  26.           return;
  27.         }
  28.       }
  29.     }

Copy code

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.

  1. // Checks whether the scanned tag is the master tag
  2.     if (tagID == myTags[0]) {
  3.       lcd.clear();
  4.       lcd.print(“Program mode:”);
  5.       lcd.setCursor(0, 1);
  6.       lcd.print(“Add/Remove Tag”);
  7.       while (!successRead) {
  8.         successRead = getID();
  9.         if ( successRead == true ) {
  10.           for (int i = 0; i < 100; i++) {
  11.             if (tagID == myTags[i]) {
  12.               myTags[i] = “”;
  13.               lcd.clear();
  14.               lcd.setCursor(0, 0);
  15.               lcd.print(” Tag Removed!”);
  16.               printNormalModeMessage();
  17.               return;
  18.             }
  19.           }
  20.           myTags[tagsCount] = strdup(tagID.c_str());
  21.           lcd.clear();
  22.           lcd.setCursor(0, 0);
  23.           lcd.print(” Tag Added!”);
  24.           printNormalModeMessage();
  25.           tagsCount++;
  26.           return;
  27.         }
  28.       }
  29.     }

Copy code

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.

  1. // Checks whether the scanned tag is authorized
  2.     for (int i = 0; i < 100; i++) {
  3.       if (tagID == myTags[i]) {
  4.         lcd.clear();
  5.         lcd.setCursor(0, 0);
  6.         lcd.print(“Access Granted!”);
  7.         myServo.write(170); // Unlocks the door
  8.         printNormalModeMessage();
  9.         correctTag = true;
  10.       }
  11.     }
  12.     if (correctTag == false) {
  13.       lcd.clear();
  14.       lcd.setCursor(0, 0);
  15.       lcd.print(” Access Denied!”);
  16.       printNormalModeMessage();
  17.     }
  18.   }
  19.   // If door is open…
  20.   else {
  21.     lcd.clear();
  22.     lcd.setCursor(0, 0);
  23.     lcd.print(” Door Opened!”);
  24.     while (!doorOpened) {
  25.       proximitySensor = analogRead(A0);
  26.       if (proximitySensor > 200) {
  27.         doorOpened = true;
  28.       }
  29.     }
  30.     doorOpened = false;
  31.     delay(500);
  32.     myServo.write(10); // Locks the door
  33.     printNormalModeMessage();
  34.   }

Copy code

The above is the full content of this article. If you have any questions, please leave a reply below this post.

Source: Link

Back to top button