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