ESP8266IoT Projects

IoT based RFID Door Lock using ESP8266

IoT RFID Door Lock with ESP8266 & Blynk

Today in this project you will learn How to make an IoT RFID Door Lock system using NodeMCU ESP8266, RFID MFRC522 Module, a Relay, Solenoid lock, and Blynk IoT platform.


Overview: IoT RFID Door Lock with ESP8266

In a previous project, we made an RFID-based smart iot door lock, where we can remotely monitor and control users. But, we cannot add a new RFID card or change the RFID card without reprogramming the device. So, in this project, we made a system where you can add and remove RFID cards easily. You can save up to 10 RFID Card UID on NodeMCU EEPROM. User RFID UIDs are saved on EEPROM, data won’t get erased even when the device is offline. With the help of this project, you can remotely control your door lock from anywhere in the world using your phone.

Scan the RFID Card to unlock the door

First of all, let’s see the overview of this project. In the Blynk IoT App, I have added an LCD Widget for Displaying Information. And some buttons to control the whole system over the internet. Each time user swipes a card a message is received. There are Add and Remove button which helps to add a new RFID card and remove it if required. Similarly, The Open button helps the admin to unlock the door without having an RFID card.

RFID Door App unlock

In the hardware part, you can add a push button to the D3 pin of NodeMCU that helps to unlock the door when you are inside the room. For this demonstration, I am using the NodeMCU built-in flash button to open the door lock.

RFID Door Button Unlock

Features of IoT based RFID Smart Door Lock System

  • Easily add and remove RFID cards from anywhere in the world.
  • Add or Remove RFID Cards without a hardcoding device.
  • Easily Open the door lock using a push-button when you are Inside.
  • Live Monitor Your Door Lock From anywhere in the world.
  • A LED indicator for power.
  • Buzzer for an alert system.
  • Open the door using a button on the app when you don’t have an RFID card.
Blinking Green LED

You can also add a LED to indicate WiFi connection and Blynk IoT server connection. It blinks while data is sent successfully to the server. Similarly, a Buzzer produces two different sounds for authorized access and access denied.

RFID Card Not Found

As You can see here, only authorized users can unlock the door lock.

RFID Card access Door Lock

Previous Door Lock Projects:


Components Required

The components and tools used in this project are easily available on Amazon.

S.NComponents NameQuantityGet Products from Amazon
1NodeMCU ESP82661https://amzn.to/3mTuL95
2RFID MFRC-522 Module1https://amzn.to/3pOPpb4
312V Solenoid Door Lock1https://amzn.to/3NygYTa
4Green LED1https://amzn.to/3JXRSLd
5Few jumpers wires20https://amzn.to/3klh0A4
63.7V 18650 Battery4https://amzn.to/3DnimDm
73.7V 18650 Battery holder1https://amzn.to/3LpS7z8
8LM2596 Buck Converter1https://amzn.to/3ISoXHh
9Buzzer1https://amzn.to/3mJNp4p

IoT Smart RFID Door Lock Circuit Diagram

As you can see the circuit diagram is very simple to understand.

IoT Smart RFID Door Lock System Using NodeMCU ESP8266 Circuit Diagram

Connect the RFID Module to NodeMCU as per the circuit diagram.

D2 <———-> SDA/SS

D5 <———-> SCK

D7 <———-> MOSI

D6 <———-> MISO

GND <———-> GND

D1 <———-> RST

3V/3V3 <———-> 3.3V

Connect Relay Input Pin to D4 pin of NodeMCU. Similarly, Buzzer Negative Terminal is connected to the Ground and the Positive terminal to the D8 Pin of NodeMCU. Now the Green LED is connected to the SD3(GPIO 10)  Pin of NodeMCU.


Project PCB Gerber File & PCB Ordering Online

You can simply assemble the circuit on a breadboard. But To remove messy wiring and give a clean look, I designed a PCB prototype for this project. It is also helpful for troubleshooting without any errors. You can download the Gerber file of my PCB Design from the link attached below. The PCB looks like the image shown below.

PCB for IoT Smart RFID Door Lock System Using NodeMCU ESP8266

I provided the Gerber File for IoT RFID Door Lock System Using NodeMCU ESP8266 PCB below.

You can simply download the Gerber File and order your custom PCB from PCBWay

PCBWay website

Visit the PCBWay official website by clicking here: https://www.PCBWay.com/. Simply upload your Gerber File to the Website and place an order. I prefer PCBWay for ordering custom PCBs. PCBWay is a place that brings manufacturers and customers together. They have more than a decade of experience in this field of fabrication and prototyping and assembling of PCBs. PCBWay has proved its focus on its customers needs in terms of cost-effectiveness, delivery, and quality. And this can be proved by their outstanding customer reviews.

  • 3D PCB of IoT Smart RFID Door Lock System Using NodeMCU ESP8266
  • PCB for IoT Smart RFID Door Lock System Using NodeMCU ESP8266
  • PCB for Smart door lock

Setting Up Blynk Application

We need to set up the Blynk App to monitor and control door lock using the ESP8266 NodeMCU board. To set up Blynk Application, Download & Install the Application on your smartphone. Available in both Android PlayStore and IOS AppStore. Open the App & create a New Account using your email address.

Create IoT RFID Door Lock Project
  • Click on create a new project
  • Provide the Name of your project as “IoT RFID Door Lock
  • Choose NodeMCU Board
  • Select connection type as Wi-Fi, then click on Create Button.
  • They sent the Blynk authentication token to your email address. (We need it later on programming)
  • Now, click on the (+) icon at the top right corner of the screen.
  • Search for the “LCD” widget and it to your main screen.
  • Also, add 3 Button Widget to the main screen.
Adding widgets to blynk iot
  • Click on the LCD Widget and Change Mode to Advanced. Then set the Input Pin to Virtual Pin V0.
LCD Widget Settings
  • Now click on the button Widget. Name the Widget as ADD Card
  • Set the Input Pin to Virtual Pin V1, Enter the On-Off levels but leave the button mode to push.
  • You can set colors and fonts size according to your need.
Add new RFID cards

Similarly, do the same for Remove Card, and Open Door with the help of the images below.

  • Remove RFID Cards
  • Open Door Widgets

Finally, the Blynk App setup for IoT RFID Door Lock using NodeMCU ESP8266 is completed.


Source Code/Program

The Source Code for the ESP8266-based IoT RFID Door Lock System is given below. The code requires MRFC522 library & Blynk Library for compilation. First, download the required libraries and add them to the Arduino IDE.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>

From this code part, change the Wi-Fi SSID, Password & Blynk Authentication Token. Use the same API Token sent to your email address.

char auth[] = "NF_xxx-yyy-zzz_ErFyB";
char ssid[] = "IoT RFID Door Lock";
char pass[] = "RFID@DoorLock";

Final Program Code

This is the final program code for IoT RFID Door Lock using ESP8266 and Blynk IoT Platform. Most of the code is the same and is already explained in my previous projects. Simply copy the code and paste it into your Arduino IDE.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>

#define SS_PIN 4   //D2
#define RST_PIN 5   //D1
#define BTN_PIN 0  //D3
#define SLN_PIN 2  //D4

MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long uidDec, uidDecTemp;
int ARRAYindexUIDcard;
int EEPROMstartAddr;
long adminID = 1122539531;
bool beginCard = 0;
bool addCard = 1;
bool skipCard = 0;
int LockSwitch;
unsigned long CardUIDeEPROMread[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int PiezoPin = 15;  //D8

char auth[] = "NF_xxx-yyy-zzz_ErFyB";
char ssid[] = "IoT RFID Door Lock";
char pass[] = "RFID@DoorLock";

WidgetLCD lcd(V0);
BlynkTimer timer;

 
void setup() {
  Serial.begin(115200);
  pinMode(SLN_PIN, OUTPUT); digitalWrite(SLN_PIN, LOW);
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(PiezoPin, OUTPUT);

  SPI.begin();
  mfrc522.PCD_Init();

  Blynk.begin(auth, ssid, pass);
  lcd.clear(); //Use it to clear the LCD Widget
  EEPROM.begin(512);
  DisplayWAiT_CARD();
  EEPROMreadUIDcard();

  digitalWrite(PiezoPin, HIGH), delay(100), digitalWrite(PiezoPin, LOW);

}

void loop() {

  digitalWrite(SLN_PIN, LOW);

  if (digitalRead(BTN_PIN) == LOW) {
    digitalWrite(SLN_PIN, HIGH); //unlock
    lcd.clear();
    lcd.print(0, 0, " BUTTON UNLOCK ");
    lcd.print(0, 1, "   DOOR OPEN   ");
    digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
    delay(2000);
    DisplayWAiT_CARD();
  }

  if (beginCard == 0) {
    if ( ! mfrc522.PICC_IsNewCardPresent()) {  //Look for new cards.
      Blynk.run();
      return;
    }

    if ( ! mfrc522.PICC_ReadCardSerial()) {  //Select one of the cards.
      Blynk.run();
      return;
    }
  }

  //Read "UID".
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    uidDecTemp = mfrc522.uid.uidByte[i];
    uidDec = uidDec * 256 + uidDecTemp;
  }

  if (beginCard == 1 || LockSwitch > 0)EEPROMwriteUIDcard();  //uidDec == adminID

  if (LockSwitch == 0) {
    //CardUIDeEPROMread.
    for (ARRAYindexUIDcard = 0; ARRAYindexUIDcard <= 9; ARRAYindexUIDcard++) {
      if (CardUIDeEPROMread[ARRAYindexUIDcard] > 0) {
        if (CardUIDeEPROMread[ARRAYindexUIDcard] == uidDec) {
          lcd.clear();
          lcd.print(0, 0, "CARD ACCESS OPEN");
          lcd.print(3, 1, uidDec);
          digitalWrite(SLN_PIN, HIGH); //unlock
          digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
          delay(2000);
          break;
        }
      }
    }

    if (ARRAYindexUIDcard == 10) {
      lcd.clear();
      lcd.print(0, 0, " Card not Found ");
      lcd.print(0, 1, "                ");
      lcd.print(0, 1, "ID : ");
      lcd.print(5, 1, uidDec);
      for (int i = 0; i <= 2; i++)delay(100), digitalWrite(PiezoPin, HIGH), delay(100), digitalWrite(PiezoPin, LOW);
      digitalWrite(SLN_PIN, LOW);  //lock();
      delay(2000);
    }

    ARRAYindexUIDcard = 0;
    DisplayWAiT_CARD();
  }

  Blynk.run();
  timer.run();
}

BLYNK_WRITE(V1) {
  int a = param.asInt();
  if (a == 1) beginCard = 1; else beginCard = 0;
}

BLYNK_WRITE(V2) {
  int a = param.asInt();
  if (a == 1) {
    skipCard = 1;
    if (EEPROMstartAddr / 5 < 10) EEPROMwriteUIDcard();
  } else {
    skipCard = 0;
  }
}

BLYNK_WRITE(V3) {
  int a = param.asInt();
  if (a == 1) {
    digitalWrite(SLN_PIN, HIGH); //unlock
    lcd.clear();
    lcd.print(0, 0, " APP UNLOCK OK ");
    lcd.print(0, 1, "   DOOR OPEN   ");
    digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
    delay(2000);
    DisplayWAiT_CARD();
  } 
}

void EEPROMwriteUIDcard() {

  if (LockSwitch == 0) {
    lcd.clear();
    lcd.print(0, 0, " START REC CARD ");
    lcd.print(0, 1, "PLEASE SCAN CARDS");
    delay(500);
  }

  if (LockSwitch > 0) {
    if (skipCard == 1) {  //uidDec == adminID
      lcd.clear();
      lcd.print(0, 0, "   Remove RECORD   ");
      lcd.print(0, 1, "                ");
      lcd.print(0, 1, "   label : ");
      lcd.print(11, 1, EEPROMstartAddr / 5);
      EEPROMstartAddr += 5;
      skipCard = 0;
    } else {
      Serial.println("writeCard");
      EEPROM.write(EEPROMstartAddr, uidDec & 0xFF);
      EEPROM.write(EEPROMstartAddr + 1, (uidDec & 0xFF00) >> 8);
      EEPROM.write(EEPROMstartAddr + 2, (uidDec & 0xFF0000) >> 16);
      EEPROM.write(EEPROMstartAddr + 3, (uidDec & 0xFF000000) >> 24);
      EEPROM.commit();
      delay(10);
      lcd.clear();
      lcd.print(0, 1, "                ");
      lcd.print(0, 0, "RECORD OK! IN   ");
      lcd.print(0, 1, "MEMORY : ");
      lcd.print(9, 1, EEPROMstartAddr / 5);
      EEPROMstartAddr += 5;
      delay(500);
    }
  }

  LockSwitch++;

  if (EEPROMstartAddr / 5 == 10) {
    lcd.clear();
    lcd.print(0, 0, "RECORD FINISH");
    delay(2000);
    EEPROMstartAddr = 0;
    uidDec = 0;
    ARRAYindexUIDcard = 0;
    EEPROMreadUIDcard();
  }
}

void EEPROMreadUIDcard() {
  for (int i = 0; i <= 9; i++) {
    byte val = EEPROM.read(EEPROMstartAddr + 3);
    CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
    val = EEPROM.read(EEPROMstartAddr + 2);
    CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
    val = EEPROM.read(EEPROMstartAddr + 1);
    CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
    val = EEPROM.read(EEPROMstartAddr);
    CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;

    ARRAYindexUIDcard++;
    EEPROMstartAddr += 5;
  }

  ARRAYindexUIDcard = 0;
  EEPROMstartAddr = 0;
  uidDec = 0;
  LockSwitch = 0;
  DisplayWAiT_CARD();
}

void DisplayWAiT_CARD() {
  lcd.clear();
  lcd.print(0, 0, "    SCAN THE   ");
  lcd.print(0, 1, "      CARD      ");
}

Now choose your NodeMCU 12E board and correct port. Compile the Program sketch and Upload it to the NodeMCU board. When you successfully upload the program to the NodeMCU board. Open the serial monitor to see the project in action.

Card Access Open Door Lock

Conclusion

Finally, we have successfully made an IoT-based RFID Door Lock System using NodeMCU ESP8266, RFID MFRC522 Module, a relay module, a Buzzer, and led. I hope this project was helpful for you. If you have any issues do let me know in the comment section below.


Related Articles

32 Comments

  1. How can you make sure that there’s no random RFID card that can open the door lock? Please reply. Thanks.

  2. what is LM2596 Buck Converter? where did you put that ? Do I really need to buy that component if I use breadboard?

  3. Cause you didnt add it to the circuit diagram. Thats why im confused where did you attached it.

    1. Only required if you want to power your nodemcu and solenoid lock with same 12V power source. It is used to step down 12V to 5V power supply.

  4. So its not necessary to buy buck converter and instead you can power your nodemcu and solenoid lock with the 12v power ? Am I correct?

    1. No. If you don’t buy buck converter you will need two separate power supply 5V for nodemcu and 12V for solenoid lock. If you connect nodemcu to 12V without buck converter you will burn it.

      1. I have a problem, the rfid stops if the esp8266 is not connected to the internet, how can it work without internet

  5. can I use 5v optocoupler relay module that supports high/low level trigger with dc 5v? or just only Single Channel Relay 1 way relay module 1ch 10A Low level trigger?

  6. If i will be using 12v power so the required relay module is 12v right? Correct me if im wrong.

  7. How do I connect esp8266 to my blynk app? Its just says IoT RFID Door Lock wasn’t online yet ?

  8. Hello can you help me with the blynk app it cannot connect to my nodemcu its just says iot dor lock wasn’t online yet

  9. excuse me, can I use Blynk IoT version 2.0?
    one more thing, why does an error appear
    ‘DisplayWait_CARD() ;’ was not declared in this scope
    ‘EEPROMreadUIDcard();’ was not declared in this scope
    ‘EEPROMwriteUIDcard();’ was not declared in this scope

Leave a Reply

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

Back to top button