RFID RC522 Based Security System using Arduino LCD Display & Servo

RFID RC522 Based Security System using Arduino (Last Updated On: December 1, 2018)

Introduction

In this project we have designed RFID RC522 Based Security System using Arduino LCD Display & Servo. RC522 is very simple yet effective module. It is an RFID module and is used for scanning RFID cards. Its a new technology and is expanding day by day. Now-a-days it is extensively used in offices where employees are issued an RFID card and their attendance is marked when they touch their card to rfid reader. We have seen it in many movies that when someone places ones card over some machine then door opens or closes. In short, its a new emerging technology which is quite useful.



Let’s first have a little introduction of RFID. RFID is the abbreviation of Radio frequency identification. RFID modules use electromagnetic fields to transfer data between card and the reader. Different tags are attached to objects and when we place that object in front of the reader, the reader reads that tags. Another benefit of RFID is that it doesn’t require to be in a line of sight to get detected. As in barcode, the reader has to be in the line of sight to the tag and then it can scan but in RFID there’s no such restriction. So, let’s get started with Interfacing of RFID RC522 with Arduino.


RFID RC522 Module

Features

Module Name: MF522-ED
Working current:13—26mA/ DC 3.3V
Standby current:10-13mA/DC 3.3V
sleeping current:<80uA
peak current:<30mA
Working frequency:13.56MHz
Card reading distance :0~60mm(mifare1 card)
Protocol:SPI
Data communication speed:Maximum 10Mbit/s
Card types supported:mifare1 S50、mifare1 S70、
mifare UltraLight、mifare Pro、mifare Desfire
Dimension:40mm×60mm
Working temperature:-20—80 degree
Storage temperature:-40—85 degree
Humidity:relevant humidity 5%—95%
Max SPI speed: 10Mbit/s

RFID RC522 Based Security System using Arduino


Specifications & Pin Details
The simple specifications of RC522 module from left to right first pins are as follows:

Pin    Name    Details
1.  3.3V    +3.3V Power Supply
2.  RST Reset
3.  GND Ground Pin
4.  IRO Not Connected
5.  MISO    Serial Communication
6.  MOSI    Serial Communication
7.  SCK TX/RX with Arduino
8.  SDA TX/RX with Arduino

RFID RC522 Based Security System using Arduino

Here we have interfaced RFID RC522 Module with Arduino Uno to design RFID based security system. Following are the components required.

1. Arduino Uno Board
2. RFID RC522 Module - (Buy Online from Amazon)
3. SG90 Servo Motor - (Buy Online from Amazon)
4. LEDs - 1 green and 1 red
5. Buzzer 
6. 16*2 LCD
7. Breadboard

Now assemble the circuit as shown in the figure here.
RFID RC522 Based Security System using Arduino


Working of the RFID System

Functioning Principle of RFID Device:
• RFID (radio frequency identification) is a technique facilitating identification of any product or item without the requirement of any line of sight amid transponder and reader.
• RFID Structure is continuously composed of 2 main hardware components. The transponder which is located on the product to be scanned and the reader which can be either just a reader or a read & write device, depending upon the system design, technology employed and the requirement. The RFID reader characteristically comprise of a radio frequency module, a controlling unit for configurations, a monitor and an antenna ti investigate the RFID tags. In addition, a number of RFID readers are in-built with an extra interface allowing them to forward the data received to another system (control system or PC).
• RFID Tag – The actual data carrying tool of an RFID structure, in general comprise of an antenna (coupling element) and an electronic micro-chip.




Video Preview: RFID RC522 Based Security System using Arduino


Source Code

//Include sections
#include <RFID.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include "pitches.h"

//Define Component to Arduino Pins
#define SS_PIN 10
#define RST_PIN 9

#define SERVO_PIN 6

#define Red_LED 8
#define Green_LED 7

#define Buzzer 5


//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
Servo DoorLock;
RFID rfid(SS_PIN, RST_PIN );

//Unique ID of RFID Tag, which you want to give access. 
int My_RFID_Tag[5] = {0xE9,0xD6,0xA4,0x59,0xC2};

//variable to hold your Access_card
boolean My_Card = false;  
   
// notes in the melody, taken from:
//File -> Examples -> Digital -> ToneMelody
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() 
{
  // put your setup code here, to run once:
  //set the pins as an input/output
  pinMode(Red_LED,OUTPUT);
  pinMode(Green_LED,OUTPUT);
  pinMode(Buzzer,OUTPUT);
  
  //Servo Connnected to pin Digital Pin 5
  DoorLock.attach(SERVO_PIN);
  //open the serial port at 9600 baudrate.
  Serial.begin(9600); 
  //Initialise the LCD to 16x2 Character Format
  lcd.begin(16,2);
  //Initialise Servo and RFID
  SPI.begin();
  rfid.init();
}

void loop() 
{
  // put your main code here, to run repeatedly:
  
  //First Assume detected card(Or tag) is My_Card, 
  //Then later we will check is it My_Card or not! 
  My_Card = true; 
  DoorLock.write(0); //Servo at 0 Position, Door is Closed.
  lcd.clear();
  lcd.print(" Put Your Card  ");
  
  

  
  //Check if any RFID Tags Detected or not?
  if( rfid.isCard() )
  {
      //if RFID Tag is detected, check for the Unique ID,
      //and print it on the Serial Window
      if( rfid.readCardSerial() )
      {   
          lcd.clear();      
          lcd.print("UNIQUE ID is:- ");
          delay(500);          
          lcd.setCursor(0,1); //Set LCD Cursor to Second Row, First Character
  
      //Unique id is 5 Digit Number.
          //Printing in HEX for better Understanding
          for( int i = 0; i < 5; i++ )
          {
              Serial.print(rfid.serNum[i], HEX);
              Serial.print(" ");              
              lcd.print(rfid.serNum[i],HEX);
              lcd.print(" ");                            
          }  
          delay(500);
          
      //Compare this RFID Tag Unique ID with your My_RFID_Tag's Unique ID
          for(int i = 0; i < 5; i++) { //if any one Unique ID Digit is not matching, //then make My_Card = false and come out from loop //No need to check all the digit! if( My_RFID_Tag[i] != rfid.serNum[i] ) { My_Card = false; break; } } Serial.println(); delay(1000); //If RFID Tag is My_Card then give access to enter into room //else dont open the door. if(My_Card) { Serial.println("\nWelcome To Your Room, Robodia!"); lcd.clear(); lcd.print("Welcome to Your"); lcd.setCursor(0,1); lcd.print("Room!"); delay(2000); //Turn on the Green LED as an indication of permission is given //to access the room. digitalWrite(Green_LED,HIGH); //Buzzer Config, taken from: //File -> Examples -> Digital -> ToneMelody
            // iterate over the notes of the melody:
            int i = 0;
            while(i < 2)
            {
              for (int thisNote = 0; thisNote < 12; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(5, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(5); } i = i + 1; delay(500); } delay(1000); //Now, Open the Door with the help of Servo Motor DoorLock.write(180); delay(200); lcd.clear(); lcd.print("Door is Open"); lcd.setCursor(0,1); lcd.print("Now!"); delay(2000); lcd.clear(); //Give 10 Sec delay to enter into room //After that door will again closed! for(int i = 10; i > 0; i--)
            {
              lcd.print("Door will close");
              lcd.setCursor(0,1);
              lcd.print("in ");
              lcd.print(i);
              lcd.print(" Sec HurryUp!");
              delay(1000);
              lcd.clear();
            }

            //Now,Door is closed and Green LED is Turned-Off.
            DoorLock.write(0);
            digitalWrite(Green_LED,LOW);
            delay(200);
            lcd.clear();
            lcd.print("Door is Close");
            lcd.setCursor(0,1);
            lcd.print("Now!");
            delay(2000);                            
          }
          
          // If RFID Tag is not My_Card then
          // Do not open the Door and 
          //Turn-On Red LED and Buzzer as an indication of Warning:
          //Somebody else is trying to enter into your room. 
          else
          {
            Serial.println("\nGet Out of Here !");
            lcd.clear();
            lcd.print("Card NOT FOUND!");
            lcd.setCursor(0,1);
            lcd.print("Get Out of Here!");

            for(int i = 0; i < 7; i++)
            {
              digitalWrite(Buzzer, HIGH);
              digitalWrite(Red_LED,HIGH);
              delay(500);
              digitalWrite(Buzzer, LOW);
              digitalWrite(Red_LED,LOW);
              delay(500);              
            }
            delay(1000);            
          }                 
      }      
   }
  //Put RFID Reader into Halt, untill it not detects any RFID Tag.
  rfid.halt();
}

If you are unable to copy the code then please download the code from here:

RFID Arduino Source Code


1 Comment

  1. i am using arduino R3

    Arduino: 1.8.5 (Windows 10), Board: “Arduino/Genuino Uno”

    C:\Users\Elias Boutros\Desktop\sketch_sep03a\sketch_sep03a.ino:2:18: fatal error: RFID.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

Leave a Reply

Your email address will not be published. Required fields are marked *