Arduino Projects

RFID Master Card Door Lock System using Arduino

Hello, everybody in this tutorial I will show you a tutorial to make RFID Master Card Door Lock System using Arduino. with which you can register multiple RFID Tags and control your door lock system.

RFID Master Card Door Lock System Using Arduino

How does this RFID master card door lock system work?

Basically, you will have one RFID card Registered as a master card in a system. Now, you can use that RFID Master card to register other RFID cards/tags to this system to control your solenoid door lock.

To add an RFID Card/tag into our system, first, you should scan the Master RFID Card. Now, when the Green LED is ON, you should scan the second RFID card/tag that you want to register to the system. After that, your card is added to the RFID master door lock system. Now, you can scan the newly added card/tag to access the door lock.

Similarly, we can remove access to RFID card/tag as well. For that, you have to scan the master card then scan the RFID card/tag that you want to remove. Now, that card won’t have any access to the RFID door lock system.

Arduino RFID Master Card Door Lock System

Note: In a similar way you can add multiple cards/tags and remove them as well.

Components Required

The following are the list of the required components for making RFID Master Card Door Lock System using Arduino.

  • Arduino UNO
  • RFID MF-RC522 Module
  • RFID Cards/Tags
  • Relay Module
  • Solenoid Door Lock
  • A Green and A Red LED
  • 12V DC power supply
  • Jumper wires.

Circuit Diagram of Arduino RFID Master Card Door Lock System

Now, follow the circuit diagram provided below and do your connections.

Circuit Diagram of RFID Master Card Door Lock System Using Arduino
Circuit Diagram

Connections between Arduino and RFID are given in the table below. The solenoid lock is connected to Arduino through the relay module.

RFID PinArduino Uno Pin  
SDADigital 10
SCKDigital 13
MOSIDigital 11
MISODigital 12
IRQUnconnected
GNDGND
RSTDigital 9
3.3V3.3V  
Relay PinArduino Uno Pin  
VccVcc
GNDGND
Sinal3

Green LED Positive pin is connected to the digital pin 5 of Arduino and Negative pin to the GND. Similarly, the RED LED Positive pin is interfaced with digital pin 4 and Negative pin to the GND.

Program Code

Let’s start coding by including all the required libraries. Here, this program only requires two libraries, one for SPI communication between Arduino and RFID, and the second for the RFID module. Both the libraries can be downloaded from the links provided below:

These are the pins for RFID Module, LEDs, Relays and different states of program for RFID.

#define RST_PIN 9
#define SS_PIN  10

#define STATE_STARTUP       0
#define STATE_STARTING      1
#define STATE_WAITING       2
#define STATE_SCAN_INVALID  3
#define STATE_SCAN_VALID    4
#define STATE_SCAN_MASTER   5
#define STATE_ADDED_CARD    6
#define STATE_REMOVED_CARD  7

#define REDPIN 4
#define GREENPIN 5
#define Relay 3

Here, you need to change the UID of your Master Card.

byte masterCard[cardSize] = {129,163,220,121};   //Change Master Card ID

Note: To know your master card UID. First, upload the program to your Arduino by selecting the correct board and COM Port. After successful upload open the serial monitor at the baudrate of 9600. Now, scan your card to get the UID. Just copy that ID and paste it on the program code. Now, again upload the code.

Final Program code for RFID Master Card Door Lock

//RFID Based Solenoid Door lock using Arduino
//The IoT Projects https://www.youtube.com/c/TheIoTProjects
//website:https://iotprojectsideas.com/
//RFID RC522 Master Card, Add and Remove multiple Cards 

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

#define RST_PIN 9
#define SS_PIN  10

#define STATE_STARTUP       0
#define STATE_STARTING      1
#define STATE_WAITING       2
#define STATE_SCAN_INVALID  3
#define STATE_SCAN_VALID    4
#define STATE_SCAN_MASTER   5
#define STATE_ADDED_CARD    6
#define STATE_REMOVED_CARD  7

#define REDPIN 4
#define GREENPIN 5
#define Relay 3

const int cardArrSize = 10;
const int cardSize    = 4;
byte cardArr[cardArrSize][cardSize];
byte masterCard[cardSize] = {129,163,220,121};   //Change Master Card ID
byte readCard[cardSize];
byte cardsStored = 0;

// Create MFRC522 instance
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Set the LCD I2C address

byte currentState = STATE_STARTUP;
unsigned long LastStateChangeTime;
unsigned long StateWaitTime;

//------------------------------------------------------------------------------------
int readCardState()
{
  int index;

  Serial.print("Card Data - ");
  for(index = 0; index < 4; index++)
  {
    readCard[index] = mfrc522.uid.uidByte[index];

    
    Serial.print(readCard[index]);
    if (index < 3)
    {
      Serial.print(",");
    }
  }
  Serial.println(" ");

  //Check Master Card
  if ((memcmp(readCard, masterCard, 4)) == 0)
  {
    return STATE_SCAN_MASTER;
  }

  if (cardsStored == 0)
  {
    return STATE_SCAN_INVALID;
  }

  for(index = 0; index < cardsStored; index++)
  {
    if ((memcmp(readCard, cardArr[index], 4)) == 0)
    {
      return STATE_SCAN_VALID;
    }
  }

 return STATE_SCAN_INVALID;
}

//------------------------------------------------------------------------------------
void addReadCard()
{
  int cardIndex;
  int index;

  if (cardsStored <= 20)
  {
    cardsStored++;
    cardIndex = cardsStored;
    cardIndex--;
  }

  for(index = 0; index < 4; index++)
  {
    cardArr[cardIndex][index] = readCard[index];
  }
}

//------------------------------------------------------------------------------------
void removeReadCard() 
{     
  int cardIndex;
  int index;
  boolean found = false;
  
  for(cardIndex = 0; cardIndex < cardsStored; cardIndex++)
  {
    if (found == true)
    {
      for(index = 0; index < 4; index++)
      {
        cardArr[cardIndex-1][index] = cardArr[cardIndex][index];
        cardArr[cardIndex][index] = 0;
      }
    }
    
    if ((memcmp(readCard, cardArr[cardIndex], 4)) == 0)
    {
      found = true;
    }
  }

  if (found == true)
  {
    cardsStored--;
  }
}

//------------------------------------------------------------------------------------
void updateState(byte aState)
{
  if (aState == currentState)
  {
    return;
  }

  // do state change
  switch (aState)
  {
    case STATE_STARTING:
      StateWaitTime = 1000;
      digitalWrite(REDPIN, HIGH);
      digitalWrite(GREENPIN, LOW);
      break;
    case STATE_WAITING:
      StateWaitTime = 0;
      digitalWrite(REDPIN, LOW);
      digitalWrite(GREENPIN, LOW);
      break;
    case STATE_SCAN_INVALID:
      if (currentState == STATE_SCAN_MASTER)
      {
        addReadCard();
        aState = STATE_ADDED_CARD;
        StateWaitTime = 2000;
        digitalWrite(REDPIN, LOW);
        digitalWrite(GREENPIN, HIGH);
      }
      else if (currentState == STATE_REMOVED_CARD)
      {
        return;
      }
      else
      {
        StateWaitTime = 2000;
        digitalWrite(REDPIN, HIGH);
        digitalWrite(GREENPIN, LOW);
      }
      break;
    case STATE_SCAN_VALID:
      if (currentState == STATE_SCAN_MASTER)
      {
        removeReadCard();
        aState = STATE_REMOVED_CARD;
        StateWaitTime = 2000;
        digitalWrite(REDPIN, LOW);
        digitalWrite(GREENPIN, HIGH);
      }
      else if (currentState == STATE_ADDED_CARD)
      {
        return;
      }
      else
      {
        StateWaitTime = 2000;
        digitalWrite(REDPIN, LOW);
        digitalWrite(GREENPIN, HIGH);
        digitalWrite(Relay,LOW);
        delay(3000);
        digitalWrite(Relay,HIGH);
      }
      break;
    case STATE_SCAN_MASTER:
      StateWaitTime = 5000;
      digitalWrite(REDPIN, LOW);
      digitalWrite(GREENPIN, HIGH);
      break;
  }

  currentState = aState;
  LastStateChangeTime = millis();
}

void setup() 
{
  SPI.begin();         // Init SPI Bus
  mfrc522.PCD_Init();  // Init MFRC522

  LastStateChangeTime = millis();
  updateState(STATE_STARTING);

  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay,HIGH);

  Serial.begin(9600);
}

void loop() 
{
  byte cardState;

  if ((currentState != STATE_WAITING) &&
      (StateWaitTime > 0) &&
      (LastStateChangeTime + StateWaitTime < millis()))
  {
    updateState(STATE_WAITING);
  }

  // Look for new cards 
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  { 
    return; 
  } 
  
  // Select one of the cards 
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  { 
    return; 
  }

  cardState = readCardState();
  updateState(cardState);
}

Video Demonstration of RFID Door Lock

Now, your RFID Master Card Door Lock System using Arduino is ready to test. Here, is a full video demonstration. If you like it then please share it with your friends.

Conclusion

So, that’s pretty much for this session. I hope you enjoyed making this RFID Master Card Door Lock System using Arduino. If you did, don’t forget to share this article with your friends. If you have any doubts regarding this weather station project then do let me know them in the comment section below.

Recommended Readings:

Related Articles

10 Comments

  1. RFID Master Card Door Lock System using Arduino
    Hello, I have run the MasterCard program, but it has a major problem, that is, when the power goes out, everything is deleted. Please guide me, thank you.

Leave a Reply

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

Back to top button