How to make a RFID door lock with Arduino | Step by step instructions

How to make a RFID door lock with Arduino

Hello, welcome back. This tutorial talks about how to make an RFID door lock system using Arduino. Also, this tutorial covers how to make a door lock system step by step at minimal cost, so that everyone can do it easily. The RFID module is mainly used for this project. Also, the lock is designed to operate automatically using a servo motor and a manual door lock. Using the knowledge in this tutorial, you can easily design a door lock system for your home doors.

How this door lock system works

When powering this door lock, the servo motor activates and pushes the door lock forward. Also displayed as “Welcome, put your card” on the LCD. When the RFID tag is moved closer to the RFID reader, it is scanned. In that case, it is displayed as “scanning” on the LCD. Then, if the RFID tag is correct, the servo motor is activated and the door lock is pulled back. The LCD shows “Door is Open”.  When the RFID tag is moved closer to the RFID reader again, if it gets the correct tag, the servo motor will push the lock forward. Displays “Door is locked” on LCD. If a wrong RFID tag is used according to the program, it will be displayed as “Wrong card” on the LCD.

OK, let’s do this project step by step. For that, the required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Arduino UNO board

RFID module

LCD display

I2C module

Servo motor

Door lock

Foamboard

Iron stick

Jumper wires

Step 2

Secondly, connect the servo motor and the lock as shown below. Use the iron stick for that.How to make a RFID door lock with Arduino

How to make a RFID door lock with Arduino
How to make a RFID door lock with Arduino
How to make a RFID door lock with Arduino
How to make a RFID door lock with Arduino

Step 3

Thirdly, glue this lock and servo motor to the foam board.glue this lock and servo motor to the foam board

glue this lock and servo motor to the foam board
glue this lock and servo motor to the foam board
glue this lock and servo motor to the foam board

Step 4

Now, connect all the components to the Arduino board. To do this, use the circuit diagram below.rfid door lock circuit diagram

Step 5

Now let’s create the program for scanning RFID tags. It is as follows.

/*RFID tag scan code
 * https://srituhobby.com
 */
 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9
#define SS_PIN  10
byte readCard[4];
byte a = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  while (!Serial);
  SPI.begin();
  mfrc522.PCD_Init();
  delay(4);
  mfrc522.PCD_DumpVersionToSerial();
  lcd.setCursor(2, 0);
  lcd.print("Put your card");
}

void loop() {
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Scanned UID");
  a = 0;
  Serial.println(F("Scanned PICC's UID:"));
  for ( uint8_t i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
    Serial.print(" ");
    lcd.setCursor(a, 1);
    lcd.print(readCard[i], HEX);
    lcd.print(" ");
    delay(500);
    a += 3;
  }
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
}

Code explanation

Firstly, I2C, SPI, and RFID libraries are included.

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

Secondly, the RST and SDA pins are defined. Also, two variables have been created to help the program.

#define RST_PIN 9
#define SS_PIN 10
byte readCard[4];
byte a = 0;

Thirdly, objects are created for I2C and RFID libraries.

LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 mfrc522(SS_PIN, RST_PIN);

In the setup function, the Serial monitor, LCD, SPI bus, and RFID module are started. Also, “Put your card” is printed on the LCD.

void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
while (!Serial);
SPI.begin();
mfrc522.PCD_Init();
delay(4);
mfrc522.PCD_DumpVersionToSerial();
lcd.setCursor(2, 0);
lcd.print("Put your card");
}

In the loop function, the RFID tag is scanned and the UID is printed on the serial monitor and LCD.

void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) {
return 0;
}

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanned UID");
a = 0;
Serial.println(F("Scanned PICC's UID:"));
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
Serial.print(" ");
lcd.setCursor(a, 1);
lcd.print(readCard[i], HEX);
lcd.print(" ");
delay(500);
a += 3;
}
Serial.println("");
mfrc522.PICC_HaltA();
return 1;
}

Step 6

Now, select board and port. Afterward, upload this code to the Arduino board.select board and port. Afterward, upload this code to the Arduino board

select board and port. Afterward, upload this code to the Arduino board
select board and port. Afterward, upload this code to the Arduino board

Step 7

Then, turn on the serial monitor and bring the RFID tag closer to the RFID reader. Now, get the UID and copy it.turn on the serial monitor and bring the RFID tag closer to the RFID reader

turn on the serial monitor and bring the RFID tag closer to the RFID reader

Step 8

Next, create the main program for this project. It is as follows.

  • The complete program of this project – Download
/*Door lock system code
 * https://srituhobby.com
 */
 
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
String UID = "";
byte lock = 0;

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);


void setup() {
  Serial.begin(9600);
  servo.write(70);
  lcd.init();
  lcd.backlight();
  servo.attach(3);
  SPI.begin();
  rfid.PCD_Init();
}

void loop() {
  lcd.setCursor(4, 0);
  lcd.print("Welcome!");
  lcd.setCursor(1, 1);
  lcd.print("Put your card");

  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Scanning");
  Serial.print("NUID tag is :");
  String ID = "";
  for (byte i = 0; i < rfid.uid.size; i++) {
    lcd.print(".");
    ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
    ID.concat(String(rfid.uid.uidByte[i], HEX));
    delay(300);
  }
  ID.toUpperCase();

  if (ID.substring(1) == UID && lock == 0 ) {
    servo.write(70);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is locked");
    delay(1500);
    lcd.clear();
    lock = 1;
  } else if (ID.substring(1) == UID && lock == 1 ) {
    servo.write(160);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is open");
    delay(1500);
    lcd.clear();
    lock = 0;
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Wrong card!");
    delay(1500);
    lcd.clear();
  }
}

Code explanation

Firstly, servo, SPI, I2C, and RFID libraries are included.

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>

Secondly, the RST and SDA pins are defined. Also, a string variable is created for placing the UID.

#define SS_PIN 10
#define RST_PIN 9
String UID = "";

Thirdly, three objects have been created for Servo, I2C, and RFID.

Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);

In the setup function, the Serial monitor, LCD, SPI bus, servo, and RFID module are started. Also, the servo motor rotates 70 degrees.

void setup() {
Serial.begin(9600);
servo.write(70);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
}

In the loop function, the first thing printed on the LCD is “Welcome, put your card”.

lcd.setCursor(4, 0);
lcd.print("Welcome!");
lcd.setCursor(1, 1);
lcd.print("Put your card");

Also, the second code part reads the RFID tag. At that point, appears as “scanning” on the LCD.

if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanning");
Serial.print("NUID tag is :");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();

Next, the third part of this code tests this UID using the IF condition. Also, the servo motor rotates 70 degrees and 160 degrees. Then, the lock moves forward and backward. On the LCD appears as “Door open” and “Door locked”. If the RFID tag is incorrect, it will be printed as “Wrong Card” on the LCD display.

if (ID.substring(1) == UID && lock == 0 ) {
servo.write(70);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is locked");
delay(1500);
lcd.clear();
lock = 1;
} else if (ID.substring(1) == UID && lock == 1 ) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door is open");
delay(1500);
lcd.clear();
lock = 0;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}
}

Now, enter your UID in this code.

String UID = "15 75 FE 28";

enter your UID in this code

Step 9

Lastly, select the board and port, Afterward, upload this code to the Arduino board.now select board and port, Afterward, upload this code to the Arduino board

now select board and port, Afterward, upload this code to the Arduino board

now select board and port, Afterward, upload this code to the Arduino board

Step 10

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.

How to make a RFID door lock with Arduino | Step by step instructions

Similar Posts

21 Comments

  1. Worked great, thanks so much! The only thing was my lcd initially did not work and I had to change the code from LiquidCrystal_I2C lcd(0x27, 16, 2); to LiquidCrystal_I2C lcd(0x3F, 16, 2); as per my lcd manufacturer’s specifications and it worked!

  2. I’m going to try this project. I think you gave power to Arduino using the the PC. Is there any other way to give power to Arduino? If yes then kindly reply as soon as possible.

      1. Hi..i try using adaptor 12v 2A, it doesn’t work. But if powered using pc everything run just fine. Any idea.

  3. I have made this and everything is working properly except that whenever I show it the Card, it says Wrong Card. Even I have given it the UID from my serial monitor. Kindly help with this one.

  4. Hello, i uploaded the code and scanned the card but it is not working. when i scan the card it just says “NUID tag is:”

  5. Hi @srituhobby! I’m a 14 yr old girl and i love ur projects a lot. plz I have a question. can i build this without an lcd display and without i2c module??
    plz answer me soon!
    thank you

Leave a Reply

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