• Skip to main content
  • Skip to primary sidebar

Homemade Circuit Projects

Need circuit help? Post them in the comments! I've answered over 50,000!

Blog | Categories | About | Contact | Calculators-online
You are here: Home / Security and Alarm / RFID Security Lock Circuit – Full Program Code and Testing Details

Circuit Simulator: Assemble and Simulate

RFID Security Lock Circuit – Full Program Code and Testing Details

Last Updated on August 9, 2019 by Swagatam 6 Comments

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.

Table of Contents
  • Overview
  • How it Works:
  • Program to find UID:
  • Program to identify the card and control relay:

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:

warning message: electricity is dangerous, proceed with caution
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:

P 20161027 182018 1 p 1 2

When an authorized card is scanned:

rfid1 2

When an unauthorized tag is scanned:

rfid 2

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.  Make this SleepWalk Alert Circuit – Protect yourself from Sleepwalking Dangers
  • 2.  3 Smart Laser Alarm Protection Circuits
  • 3.  Laser Diode Driver Circuit
  • 4.  How to Make a Barcode Security Lock Circuit
  • 5.  Power Interruption Alarm Circuit for Instant Power Failure Indications
  • 6.  3 Accurate Infrared Intruder Alarm Circuits using Photodiodes

Filed Under: Security and Alarm Tagged With: Code, Full, Lock, Program, RFID, Security

About Swagatam

I am an electronics engineer and doing practical hands-on work from more than 15 years now. Building real circuits, testing them and also making PCB layouts by myself. I really love doing all these things like inventing something new, designing electronics and also helping other people like hobby guys who want to make their own cool circuits at home.

And that is the main reason why I started this website homemade-circuits.com, to share different types of circuit ideas..

If you are having any kind of doubt or question related to circuits then just write down your question in the comment box below, I am like always checking, so I guarantee I will reply you for sure!

Previous Post: « Triac Phase Control using PWM Time Proportional
Next Post: Illuminated Crosswalk Safety Light Circuit »

Reader Interactions

Comments

  1. Carlos says

    October 3, 2017 at 10:02 pm

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

    Reply
    • Swagatam says

      October 4, 2017 at 2:16 am

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

      Reply
  2. Geoff Nutley says

    December 23, 2016 at 1:32 am

    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 at 12:02 pm

      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 at 3:13 am

      Thank you Swagatam, shall wait for a reply.

      Reply
    • GR says

      December 24, 2016 at 11:07 am

      Hi geoff,

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

      Reply

Need Help? Please Leave a Comment! We value your input—Kindly keep it relevant to the above topic! Cancel reply

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

Primary Sidebar




Subscribe to New Posts

Categories

  • Arduino Projects (89)
  • Audio and Amplifier Projects (132)
  • Automation Projects (17)
  • Automobile Electronics (101)
  • Battery Charger Circuits (83)
  • Datasheets and Components (104)
  • Electronics Theory (143)
  • Free Energy (37)
  • Games and Sports Projects (11)
  • Grid and 3-Phase (19)
  • Health related Projects (25)
  • Home Electrical Circuits (12)
  • Indicator Circuits (14)
  • Inverter Circuits (88)
  • Lamps and Lights (142)
  • Meters and Testers (69)
  • Mini Projects (46)
  • Motor Controller (64)
  • Oscillator Circuits (27)
  • Pets and Pests (15)
  • Power Supply Circuits (108)
  • Remote Control Circuits (50)
  • Security and Alarm (64)
  • Sensors and Detectors (101)
  • Solar Controller Circuits (59)
  • Temperature Controllers (42)
  • Timer and Delay Relay (49)
  • Transmitter Circuits (29)
  • Voltage Control and Protection (39)
  • Water Controller (36)




Other Links

  • Privacy Policy
  • Cookie Policy
  • Disclaimer
  • Copyright
  • Videos
  • Sitemap




People also Search

555 Circuits | 741 Circuits | LM324 Circuits | LM338 Circuits | 4017 Circuits | Ultrasonic Projects | SMPS Projects | Christmas Projects | MOSFETs | Radio Circuits | Laser Circuits | PIR Projects |

Social Profiles

  • Twitter
  • YouTube
  • Instagram
  • Pinterest
  • My Facebook-Page
  • Quora
  • Stack Exchange
  • Linkedin



  • Recent Comments

    • Swagatam on How to Repair Mosquito Swatter Bats
    • Pradosh on How to Repair Mosquito Swatter Bats
    • Swagatam on 100A AC Load Monitoring Circuit using Arduino, Watt Limit, LCD, Alarm, Auto Shutdown
    • Swagatam on How to Modify 78XX, LM323, LM350, LM317 Voltage Regulator Circuits
    • Swagatam on 5 Simple Audio Mixer Circuits Explained

    © 2025 · Swagatam Innovations