Arduino

Arduino Tutorial 30# How to make Arduino Based RFID (RC522) Door Lock

RFID door lock

In this tutorial, we will see how to make arduino based RFID Door Lock. And you can see When I show wrong card or wrong keychain tag, it will make sound. When I show my college ID it will open the door. For closing the door, we have to again show college ID. So let’s get started.

For this you will need

  1. Arduino,
  2. RFID sensor (RC522),
  3. Servo motor,
  4. Piezo buzzer,
  5. Jumper wires,
  6. Breadboard (Optional).

Do connection as shown in diagram.

RFID door lock circuit diagram
RFID door lock circuit diagram

Before uploading our RFID door lock code. Add MFRC522 library (RFID library) in your computer. First of all, we have to find out unique identification number. For doing this

Go to file –> Examples –> MFRC522 –> READNUID

Upload the sketch. After uploading the sketch. Open serial monitor. Show the card which you want to use for Door lock. I am using my college Id card for door lock. Now we can see identification number. copy this number and come to our sketch of RFID door lock.

RFID door lock Sketch
#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>


#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[] = {94,73,241,250}; //This is the stored UID
int codeRead = 0;
int Buzzer = 6;
int activate =0;
Servo myservo;
String uidString;
void setup() {
 
 Serial.begin(9600);
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522 
 myservo.attach(5); //attach the servo to pin 5
 pinMode(Buzzer,OUTPUT); 
 myservo.write(0); // 0 degree angle for servo



}

void loop() {
 if( rfid.PICC_IsNewCardPresent())
 {
 readRFID();
 }
 delay(100);

}

void readRFID()
{
 
 rfid.PICC_ReadCardSerial();
 Serial.print(F("\nPICC type: "));
 MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
 Serial.println(rfid.PICC_GetTypeName(piccType));

// Check is the PICC of Classic MIFARE type
 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."));
 return;
 }


 Serial.println("Scanned PICC's UID:");
 printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);



 int i = 0;
 boolean match = true;
 while(i<rfid.uid.size)
 {
 if(!(rfid.uid.uidByte[i] == code[i]))
 {
 match = false;
 }
 i++;
 }

if(match)
 { if (activate==0){
 myservo.write(90); // 90 degree angle for servo (Door will open)
 Serial.println("\nDoor is open");
 delay(1000);
 activate = activate+1;
 }
 else
 { myservo.write(0); // 0 degree angle for servo (Door will close)
 activate = 0;
 Serial.println("\nDoor is close");
 delay(1000);
 }
 }
 
 else
 {
 tone(6,400,1000);
 Serial.println("\nUnknown Card");
 }


 // Halt PICC
 rfid.PICC_HaltA();

// Stop encryption on PCD
 rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
 for (byte i = 0; i < bufferSize; i++) {
 Serial.print(buffer[i] < 0x10 ? " 0" : " ");
 Serial.print(buffer[i], DEC);
 }
}

Identification number which we have copied earlier, paste here.

int code[] = {94,73,241,250}; //This is the stored UID

I have given 0 degree angle for door lock position. readRFID will read the card number. If card  number match with our card number door will open and for door opening 90 degree angle is given. Activate will become 1. You can change these angles according to your door lock opening and closing position. If we again show same card it will close the door. If card number does not match with our card number, buzzer will make sound for 1 second. You can see when I show unknown card or keychain tag it makes sound. When I show right card servo will rotate to 90 degree and when I again show same card servo will rotate back to 0 degree angle.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2DKDZyI (RFID RC522)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2weYm4R (servo motor)

KEEP LEARNING KEEP MAKING 🙂

Leave a Reply

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