• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Get free circuit help 24/7

New Projects | Privacy Policy | About us | Contact | Disclaimer | Copyright | Videos | Circuits for Beginners | Basic Circuits | Hobby Projects | Transistor Circuits | LED Drivers 

You are here: Home / Security and Alarm / RFID Security Lock Circuit – Full Program Code and Testing Details

RFID Security Lock Circuit – Full Program Code and Testing Details

Last Updated on August 9, 2019 by Swagatam 6 Comments

caution electricity can be dangerous

In this article we will see how an Arduino based RFID reader circuit could be used for controlling a relay, which in turn could be used in security door lock applications.

Overview

If you haven’t yet checked the previous RFID article, please go ahead check it out, it covered the basics of RFID technology.

We are going to identify the authorized tags using UID. In a nutshell UID is the unique identification number of the tag, when you scan your card at your office or anywhere else, it extracts the UID from the card.

The UID of the card is saved on the database of your office and it will recognize the card holder and register your attendance.

The tag not only transfers UID, but also transfers some other information which is stored in the tag, the tags can generally store from 1KB to 4KB sometimes even more.

We won’t be discussing how to store information on the tag but, it will be discussed in a future article. In this post we are going to utilize the UID number to control the relay ON/OFF.

The motto of this project is to turn ON/OFF the device, which is connected with the given setup on scanning with authorized RFID tag.

The UID of the card is defined in the program and when authorized card is detected, it will turn on the relay on the first scan and scanning it again will deactivate the relay.

If any unauthorized card is detected, it will give out error message on serial monitor and relay continue its current task without any interruption.

Here when the authorized card is scanned, the relay activates/deactivates, this mechanism can be used anywhere, for example in: door locking system, where authorized card needs to be scanned to open door.

How it Works:

RFID Security Lock Circuit using Arduino

The RFID circuit consist of LED which indicate the status of the relay, BC 548 transistor drives the relay and 1N4007 diode is connected across the relay to arrest the high voltage spike at the instant of switching.

If you want to connect higher voltage rated relay (9V or 12V), you can connect external +Ve supply to relay and –Ve supply to ground of arduino’s GND pin. Please take at most care while proceeding this step, as you may damage the board if connections are not right.

The next step after completing the hardware setup is to upload the code to find the UID of you tag.
Now upload the below given program to arduino, open serial monitor and scan the tag.

Program to find UID:

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if(piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K)
{
Serial.println(F("Your tag is not of type MIFARE Classic, your card/tag can't be read :("));
return;
}
String StrID = "" ;
for (byte i = 0; i <4; i ++) {
StrID +=
(rfid.uid.uidByte[i]<0x10? "0" : "")+
String(rfid.uid.uidByte[i],HEX)+
(i!=3?":" : "" );
}
StrID.toUpperCase();
Serial.print("Your card's UID: ");
Serial.println(StrID);
rfid.PICC_HaltA ();
rfid.PCD_StopCrypto1 ();
}

The output on serial monitor (example):

Your card’s UID is: AA:BB:CC:DD

On the serial monitor, you will see some hexadecimal code, which is the UID of the tag. Note it down, which will be used in the next program to identify the tag.
After this step is completed, upload the below code on the same setup.

Program to identify the card and control relay:

//---------------Program developed by R.Girish------------//
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
int flag=0;
int op=8;
char UID[] = "XX:XX:XX:XX";            //Place your UID of your tag here.
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup()
{
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(op,OUTPUT);
}
void loop()
{
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if(piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic, your tag can't be read :("));
return;
}
String StrID = "" ;
for (byte i = 0; i <4; i ++)
{
StrID +=
(rfid.uid.uidByte[i]<0x10? "0" : "")+
String(rfid.uid.uidByte[i],HEX)+
(i!=3?":" : "" );
}
StrID.toUpperCase();
if(StrID!=UID)
{
Serial.println("This is an invalid tag :(");
Serial.println("***************************************");
delay(2000);
}
if (StrID==UID && flag==0)
{
flag=1;
digitalWrite(op,HIGH);
Serial.println("This is a vaild tag :)");
Serial.println("Status: ON");
Serial.println("***************************************");
delay(2000);
}
else if(StrID==UID && flag==1)
{
flag=0;
digitalWrite(op,LOW);
Serial.println("This is a vaild tag :)");
Serial.println("Status: OFF");
Serial.println("***************************************");
delay(2000);
}
rfid.PICC_HaltA ();
rfid.PCD_StopCrypto1 ();
}
//---------------Program developed by R.Girish------------//

char UID[] = "XX:XX:XX:XX";            //Place your UID of your tag here.
Replace XX:XX:XX:XX with your UID.

Author’s prototype which can be effectively used as a foolproof RFID security lock for doors and safes:

When an authorized card is scanned:

When an unauthorized tag is scanned:

If you have any questions regarding this Arduino RFID security lock circuit, please feel free to ask below in comment section.

You'll also like:

  • 1.  A Homemade Fence Charger, Energizer Circuit
  • 2.  Ultrasonic Weapon (USW) Circuit
  • 3.  How to Make a GHz Microwave Radar Security Alarm Circuit
  • 4.  Anti Spy RF Detector Circuit – Wireless Bug Detector
  • 5.  Solar Stud Light Circuit 
  • 6.  2 Simple Earth Leakage Circuit Breaker (ELCB) Explained

About Swagatam

I am an electronic engineer (dipIETE ), hobbyist, inventor, schematic/PCB designer, manufacturer. I am also the founder of the website: https://www.homemade-circuits.com/, where I love sharing my innovative circuit ideas and tutorials.
If you have any circuit related query, you may interact through comments, I'll be most happy to help!

Reader Interactions

Comments

    Have Questions? Please post your comments below for quick replies! Comments should be related to the above artcile Cancel reply

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

  1. Carlos says

    October 3, 2017

    Hi there! Code compiling with ATmega 2560, but not working.

    Reply
    • Swagatam says

      October 4, 2017

      Hey, please check it now, and let us know if still it has problems.

      Reply
  2. Geoff Nutley says

    December 23, 2016

    Hi Swagatam, Just wondering if there is any way that I could add more tags into the code, I have 3 different tags that I would like to use.
    Regards
    Geoff

    Reply
    • Swagatam says

      December 23, 2016

      Hi Geoff, I'll forward the question to the author of the article Mr. GR for the solution.

      Reply
    • Geoff Nutley says

      December 24, 2016

      Thank you Swagatam, shall wait for a reply.

      Reply
    • GR says

      December 24, 2016

      Hi geoff,

      Yes it is possible to add 2 more tags. I will try to update the code.
      Regards

      Reply

Primary Sidebar

Calculators

  • 3-Phase Power (15)
  • 324 IC Circuits (19)
  • 4017 IC Circuits (52)
  • 4060 IC Circuits (25)
  • 555 IC Circuits (98)
  • 741 IC Circuits (19)
  • Arduino Engineering Projects (83)
  • Audio and Amplifier Projects (114)
  • Battery Chargers (82)
  • Car and Motorcycle (94)
  • Datasheets (46)
  • Decorative Lighting (Diwali, Christmas) (33)
  • Electronic Components (100)
  • Electronic Devices and Circuit Theory (36)
  • Electronics Tutorial (116)
  • Fish Aquarium (5)
  • Free Energy (34)
  • Fun Projects (13)
  • GSM Projects (9)
  • Health Related (20)
  • Heater Controllers (29)
  • Home Electrical Circuits (102)
  • How to Articles (20)
  • Incubator Related (6)
  • Industrial Electronics (28)
  • Infrared (IR) (40)
  • Inverter Circuits (98)
  • Laser Projects (12)
  • LED and Light Effect (93)
  • LM317/LM338 (21)
  • LM3915 IC (25)
  • Meters and Testers (65)
  • Mini Projects (148)
  • Motor Controller (67)
  • MPPT (7)
  • Oscillator Circuits (26)
  • PIR (Passive Infrared) (8)
  • Power Electronics (34)
  • Power Supply Circuits (77)
  • Radio Circuits (10)
  • Remote Control (48)
  • Security and Alarm (61)
  • Sensors and Detectors (120)
  • SG3525 IC (5)
  • Simple Circuits (75)
  • SMPS (29)
  • Solar Controllers (60)
  • Timer and Delay Relay (53)
  • TL494 IC (5)
  • Transformerless Power Supply (8)
  • Transmitter Circuits (40)
  • Ultrasonic Projects (16)
  • Water Level Controller (45)

Calculators

  • AWG to Millimeter Converter
  • Battery Back up Time Calculator
  • Capacitance Reactance Calculator
  • IC 555 Astable Calculator
  • IC 555 Monostable Calculator
  • Inductance Calculator
  • LC Resonance Calculator
  • LM317, LM338, LM396 Calculator
  • Ohm’s Law Calculator
  • Phase Angle Phase Shift Calculator
  • Power Factor (PF) Calculator
  • Reactance Calculator
  • Small Signal Transistor(BJT) and Diode Quick Datasheet
  • Transistor Astable Calculator
  • Transistor base Resistor Calculator
  • Voltage Divider Calculator
  • Wire Current Calculator
  • Zener Diode Calculator

© 2023 · Swagatam Innovations