esp32 RFID door lock system

Certainly! Here’s an example of an ESP32 RFID door lock project. The project uses an ESP32 microcontroller, an RFID reader module, and a relay module to control an electronic door lock. It verifies RFID tags and triggers the relay to unlock the door. Here’s the code:

#include <SPI.h>

#include <MFRC522.h>


#define RST_PIN         22          // Reset pin for RFID reader module

#define SS_PIN          21          // SS pin for RFID reader module

#define RELAY_PIN       5           // Pin to control the relay module


MFRC522 mfrc522(SS_PIN, RST_PIN);    // Create an MFRC522 instance


void setup() {

  Serial.begin(9600);       // Initialize serial communication

  SPI.begin();              // Initialize SPI bus

  mfrc522.PCD_Init();       // Initialize MFRC522 RFID reader


  pinMode(RELAY_PIN, OUTPUT);   // Set relay pin as an output

  digitalWrite(RELAY_PIN, HIGH);  // Set relay initially in locked state

}


void loop() {

  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {

    String tagID = “”;


    for (byte i = 0; i < mfrc522.uid.size; i++) {

      tagID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? “0” : “”));

      tagID.concat(String(mfrc522.uid.uidByte[i], HEX));

    }


    Serial.println(“Scanned RFID Tag ID: ” + tagID);


    if (tagID == “YOUR_TAG_ID_HERE”) {   // Replace with the desired RFID tag ID

      unlockDoor();   // Call the function to unlock the door

    } else {

      Serial.println(“Access Denied”);

    }


    mfrc522.PICC_HaltA();

  }

}


void unlockDoor() {

  digitalWrite(RELAY_PIN, LOW);   // Set relay to unlock the door

  Serial.println(“Door Unlocked”);

  delay(2000);   // Keep the door unlocked for 2 seconds

  digitalWrite(RELAY_PIN, HIGH);  // Set relay back to lock the door

  Serial.println(“Door Locked”);

}

Make sure to install the MFRC522 library before uploading the code to your ESP32 board. You can find the library in the Arduino Library Manager or download it from the Arduino website.
Remember to replace “YOUR_TAG_ID_HERE” with the actual RFID tag ID that you want to grant access to. Each RFID tag has a unique ID, which you can obtain by scanning the tag using the provided code and checking the serial monitor for the tag ID.
Ensure that the connections between the ESP32, RFID reader module, and relay module are correctly wired according to the pin configurations defined in the code.
This example demonstrates the basic functionality of an RFID door lock using an ESP32. You can further enhance the project by integrating Wi-Fi connectivity to enable remote access control or adding additional security features.