【Based on Arduino RFID access control system】

Arduino based RFID access control system

Introduction
insert image description here
Radio Frequency Identification or RFID (Radio-Frequency Identification) is a method of automatic identification through radio signals, retrieval and storage of data through RFID tags.

These RFID tags can be attached to animals, objects. Therefore, these tags have many applications, such as no parking tags attached to vehicles, animal identification. There are 3 types of RFID tags: passive tags are tags that respond to a signal sent by a transmitter, semi-passive tags, and active tags that emit a signal themselves. There are several ways to control access to a location: using fingerprints, keypads with passwords, and using RFID systems.

In this article, we will learn how to develop an access control system using RFID technology. The system will consist of a MFRC522 RFID module, a servo motor for opening the door, a display as the system HMI, and signal LEDs. So in this article, we will learn how to develop access control using the RFID module.
Therefore, through this article you will learn:

Carry out circuit assembly on the breadboard board;
understand the function of the RFID module;
start the servo motor;
display characters on the LCD screen.

Now, we will start a complete introduction to develop a parking lot access control system project using RFID modules. Developing a Parking Lot Access Control System Using RFID Module with Arduino
The heart of the project is the RFID module which consists of a printed circuit board with MFRC522 integrated circuit and an antenna on the board. When the board is powered on, the module emits a radio frequency signal, and the tags are read as they approach the module, each with a different code. The module is powered by 3.3 V and it uses SPI (Serial Peripheral Interface) communication to communicate with the microcontroller used.

The first step in developing this project is to assemble the circuit in Figure 1.
insert image description hereThe operation of the road is very simple! A servo motor is used as the mechanism for opening and closing the door. Every time a tag is recognized by the RFID module, the Arduino sends a message to activate or close the door. The LCD is used as a communication interface with the user. Next, we'll see how the programming logic for this project works.
Code Logic for Arduino Parking System Control
To program the Arduino Nano we need the following libraries:

SPI - A library containing the functions needed to perform SPI communication.
MFRC522 - Library containing the necessary functions to communicate with the RFID module.
Servo - A library containing the necessary functions to start a servo motor.
Wire - A library containing the functions needed for I2C communication with LCD displays.

The liquidCrystal_I2C and MFRC522 libraries are not installed in the Arduino IDE, so we have to install them. After installing the library, close the Arduino IDE and open it again. The full code is shown below.

/* * Teste Leitor RFID * tag 1 F1 B103 1F 241 17703 31
F1 B1 03 1F
 tag 2 14 45 29 57 20 69 41 87
14 45 29 57
 */

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Servo myservo ;
LiquidCrystal_I2C lcd (0x27,2,1,0,4,5,6,7,3,POSITIVE);

#define vermelho 4
#define verde 5
#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522 (SS_PIN, RST_PIN);

void setup() {
    
      Wire.begin();
  lcd.begin(16,2);  lcd.setBacklight(HIGH);  lcd.setCursor(0,0);  lcd.print("Aproxime a sua   ");  lcd.setCursor(0,1);  lcd.print("tag do leitor ");    pinMode(verde,OUTPUT);  pinMode(vermelho,OUTPUT);    Serial.begin(9600);  SPI.begin();  Serial.println("Aproxime a tag do leitor ");    mfrc522.PCD_Init();     digitalWrite(verde,0);  digitalWrite(vermelho,0);    myservo.attach(6);  }

void loop() {
    
        if(!mfrc522.PICC_IsNewCardPresent())  {
    
        return;  }    if(!mfrc522.PICC_ReadCardSerial())  {
    
        return;  }
  Serial.print(" UID da tag : ");    String conteudo = "";    byte letra;    for (byte i = 0; i< mfrc522.uid.size; i++)  {
    
       Serial.print(mfrc522.uid.uidByte[i] <0x10 ? "0":" ");   Serial.print(mfrc522.uid.uidByte[i], HEX);      conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));   conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));  }    Serial.println();  Serial.print("Mensagem : ");    conteudo.toUpperCase();    if(conteudo.substring(1) == "14 45 29 57")  {
    
        lcd.clear();    Serial.println("Acesso liberado ");      lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Ola !");    lcd.setCursor(0,1);    lcd.print("Acesso liberado");        digitalWrite(verde,1);    digitalWrite(vermelho,0);        myservo.write(95);    delay(800);    myservo.write(10);    digitalWrite(verde,0);    digitalWrite(vermelho,1);  }   if(conteudo.substring(1) == "F1 B1 03 1F")  {
    
            lcd.clear();    Serial.println("Acesso negado ");    digitalWrite(verde,0);    digitalWrite(vermelho,1);        lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Erro ! Tag nao ");    lcd.setCursor(0,1);    lcd.print("autorizada ");      }      delay(1000);   lcd.clear();       lcd.setBacklight(HIGH);   lcd.setCursor(0,0);   lcd.print("Aproxime a sua  ");   lcd.setCursor(0,1);   lcd.print("tag do leitor ");
}

Below, we explain the complete logic of this project. The first thing you need to do is declare all libraries for the components used in your project.

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

After that, we declare the objects for the servo motors and LCD. Object creation is shown below.

Servo myservo;
LiquidCrystal_I2C lcd (0x27,2,1,0,4,5,6,7,3,POSITIVE);
MFRC522 mfrc522 (SS_PIN, RST_PIN);

We now have a map of the connected pins on the Arduino.

#define vermelho 4
#define verde 5
#define SS_PIN 10
#define RST_PIN 9

Below is the void setup function. It initializes the I2C serial communication, serial communication, configures the pins as outputs, and connects the pins of the servo motors.

void setup() {
    
    
  Wire.begin();
  lcd.begin(16,2);  lcd.setBacklight(HIGH);  lcd.setCursor(0,0);  lcd.print("Aproxime a sua   ");  lcd.setCursor(0,1);  lcd.print("tag do leitor ");    pinMode(verde,OUTPUT);  pinMode(vermelho,OUTPUT);    Serial.begin(9600);  SPI.begin();  Serial.println("Aproxime a tag do leitor ");    mfrc522.PCD_Init();     digitalWrite(verde,0);  digitalWrite(vermelho,0);    myservo.attach(6);  }

Let us now understand how the complete programming logic works, which is implemented in the void loop function. See code below for the loop that doesn't work.

void loop() {
    
        if(!mfrc522.PICC_IsNewCardPresent())  {
    
        return;  }    if(!mfrc522.PICC_ReadCardSerial())  {
    
        return;  }
  Serial.print(" UID da tag : ");    String conteudo = "";    byte letra;    for (byte i = 0; i< mfrc522.uid.size; i++)  {
    
       Serial.print(mfrc522.uid.uidByte[i] <0x10 ? "0":" ");   Serial.print(mfrc522.uid.uidByte[i], HEX);      conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));   conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));  }    Serial.println();  Serial.print("Mensagem : ");    conteudo.toUpperCase();    if(conteudo.substring(1) == "14 45 29 57")  {
    
        lcd.clear();    Serial.println("Acesso liberado ");      lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Ola !");    lcd.setCursor(0,1);    lcd.print("Acesso liberado");        digitalWrite(verde,1);    digitalWrite(vermelho,0);        myservo.write(95);    delay(800);    myservo.write(10);    digitalWrite(verde,0);    digitalWrite(vermelho,1);  }   if(conteudo.substring(1) == "F1 B1 03 1F")  {
    
            lcd.clear();    Serial.println("Acesso negado ");    digitalWrite(verde,0);    digitalWrite(vermelho,1);        lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Erro ! Tag nao ");    lcd.setCursor(0,1);    lcd.print("autorizada ");      }      delay(1000);   lcd.clear();       lcd.setBacklight(HIGH);   lcd.setCursor(0,0);   lcd.print("Aproxime a sua  ");   lcd.setCursor(0,1);   lcd.print("tag do leitor ");
}

The first thing you should do is check that no tags are detected, nor read. The code is shown below.

if(!mfrc522.PICC_IsNewCardPresent())  {
    
        return;  }    if(!mfrc522.PICC_ReadCardSerial())  {
    
        return;  }

After that, the system reads the tag and displays its value on the IDE Arduino serial monitor. See the code section below.

Serial.print(" UID da tag : ");    String conteudo = "";    byte letra;    for (byte i = 0; i< mfrc522.uid.size; i++)  {
    
       Serial.print(mfrc522.uid.uidByte[i] <0x10 ? "0":" ");   Serial.print(mfrc522.uid.uidByte[i], HEX);      conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));   conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));  }    Serial.println();  Serial.print("Mensagem : ");    conteudo.toUpperCase();

After presenting the tag code, the system compares the tag value with the hexadecimal value registered in the system.

if(conteudo.substring(1) == "14 45 29 57")  {
    
    
    lcd.clear();    Serial.println("Acesso liberado ");      lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Ola !");    lcd.setCursor(0,1);    lcd.print("Acesso liberado");        digitalWrite(verde,1);    digitalWrite(vermelho,0);        myservo.write(95);    delay(800);    myservo.write(10);    digitalWrite(verde,0);    digitalWrite(vermelho,1);
  }

If the tag value is equal to 14 45 29 57, the command for the above condition will be executed. During these commands, the LCD will print the access information and activate the servo motor to open the entry door.

If the tag value is equal to the F1 B1 03 1F value, the code flow enters the condition and displays the message "Tag not authorized!" and does not trigger the servo to release access.

The code part is shown below.

if(conteudo.substring(1) == "F1 B1 03 1F")  {
    
            lcd.clear();    Serial.println("Acesso negado ");    digitalWrite(verde,0);    digitalWrite(vermelho,1);        lcd.setBacklight(HIGH);    lcd.setCursor(0,0);    lcd.print("Erro ! Tag nao ");    lcd.setCursor(0,1);    lcd.print("autorizada ");      }

Finally, the system cleans the LCD screen and displays a message telling the user to hold the tag close to the RFID reader. The code area is shown below.

delay(1000);   lcd.clear();       lcd.setBacklight(HIGH);   lcd.setCursor(0,0);   lcd.print("Aproxime a sua  ");   lcd.setCursor(0,1);   lcd.print("tag do leitor ");

We will now see the result of the code working with the circuit on the bench. After programming the Arduino Uno, we will get the system working. In Figure 2, we have the system's initial message asking the user to approach the tag to release access.
insert image description here
In Figure 3, we ask the user to use the correct tag to allow access, then the green LED lights up and the servo motor is activated, and the access rights are shown on the display.

After 800 milliseconds, the servo motor returns to the initial position, the green LED turns off and the red LED turns on.
insert image description hereIn Figure 4, the user used an unregistered tag, so the system gave an error and did not release access.
insert image description hereAfterwards, the system restarts the programming logic run cycle.

Conclusions and Future Project Improvements
Systems using RFID technology are applied to various types of projects. They are not limited to control and access systems. For example, a very common application is to use it to identify batches of clothing and other goods. Use your creativity and you will find different types of applications for RFID modules with Arduino for your projects. In the future, we will use an Arduino to create a prototype of the gate system for you to set up your parking lot in practice using a laser cutter or 3D printer.

Guess you like

Origin blog.csdn.net/m0_46509684/article/details/129037226