Hello guys. In this tutorial, we will see how to make RFID Door Lock. And you can see When I show wrong card or wrong key-chain 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 Wire,
  6. Breadboard (optional).

Do connection as shown in diagram.

How to make RFID Door Lock.

Sketch of RFID door lock:
#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);
 }
}
Explanation:

Before uploading this code. Download RFID (MFRC522) library from github in your computer OR you can download it from here because github library may change by the time or it may not work.

Download library for RFID master.zip

Add library to your arduino libraries. First we have to find out unique identification number.

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. As 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. Identification number which we have copied earlier, paste here in main sketch.

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

RFID Sensor Identification number

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.

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;
 }

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.

 else
 { myservo.write(0); // 0 degree angle for servo (Door will close)
 activate = 0;
 Serial.println("\nDoor is close");
 delay(1000);
  }

If card number does not match with our card number, buzzer will make sound for 1 second.

else
 {
 tone(6,400,1000);
 Serial.println("\nUnknown Card");
 }

You can see when I show unknown card or key chain 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.

RFID Door Lock

Till then keep learning keep making.

Categories: Arduino