ESP32 with RFID Module for Secure Access Control

What is an RFID Module?

RFID (Radio Frequency Identification) is a technology that uses electromagnetic fields to automatically identify and track tags attached to objects. The MFRC522 module is a commonly used 13.56 MHz RFID reader that can communicate via SPI. It reads the UID (Unique Identifier) from passive RFID cards or tags and sends the data to a microcontroller for validation.

Working of RFID with ESP32

The MFRC522 RFID module communicates with the ESP32 via SPI protocol. When a tag/card is placed near the reader, the UID is read and compared with a list of authorized IDs stored in the ESP32. If the UID matches, access is granted (e.g., a relay is triggered). Otherwise, access is denied.

  • Connect the MFRC522 module to ESP32 using SPI pins.
  • Include and configure the RFID and SPI libraries.
  • Store authorized UID(s) in the ESP32 code.
  • Detect RFID card placement and read UID.
  • Compare scanned UID with stored authorized UIDs.
  • Trigger an action (unlock relay, turn on LED) if UID matches.

Formula: Access Granted = if (scannedUID == storedUID) Access Denied = if (scannedUID != storedUID)

Components Required

  • ESP32 Dev Board
  • MFRC522 RFID Reader Module
  • RFID Tags or Cards (13.56 MHz)
  • Jumper Wires
  • Breadboard
  • Relay Module or LED
  • Buzzer (optional)

Pin Configuration

  • SDA: Connect to GPIO 21 (Customizable)
  • SCK: Connect to GPIO 18
  • MOSI: Connect to GPIO 23
  • MISO: Connect to GPIO 19
  • IRQ: Not connected (optional)
  • GND: Ground
  • RST: Connect to GPIO 22
  • 3.3V: Connect to 3.3V on ESP32

Make sure the RFID module operates at 3.3V. Powering it with 5V may damage the ESP32 or the reader. Also, keep the RFID tags within 2-4 cm range of the antenna for reliable detection.

Wiring the MFRC522 RFID Module to ESP32

  • SDA -> GPIO 21
  • SCK -> GPIO 18
  • MOSI -> GPIO 23
  • MISO -> GPIO 19
  • RST -> GPIO 22
  • VCC -> 3.3V
  • GND -> GND

Arduino Code for ESP32 + RFID (MFRC522)

1#include <SPI.h>
2#include <MFRC522.h>
3
4#define SS_PIN 21
5#define RST_PIN 22
6#define RELAY_PIN 4
7
8MFRC522 rfid(SS_PIN, RST_PIN);
9
10byte authorizedUID[] = {0xDE, 0xAD, 0xBE, 0xEF};
11
12void setup() {
13  Serial.begin(115200);
14  SPI.begin();
15  rfid.PCD_Init();
16  pinMode(RELAY_PIN, OUTPUT);
17  digitalWrite(RELAY_PIN, LOW);
18  Serial.println("Place your RFID card near the reader...");
19}
20
21void loop() {
22  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
23    return;
24  }
25
26  Serial.print("Scanned UID: ");
27  bool isAuthorized = true;
28
29  for (byte i = 0; i < rfid.uid.size; i++) {
30    Serial.print(rfid.uid.uidByte[i], HEX);
31    if (rfid.uid.uidByte[i] != authorizedUID[i]) {
32      isAuthorized = false;
33    }
34  }
35  Serial.println();
36
37  if (isAuthorized) {
38    Serial.println("Access Granted");
39    digitalWrite(RELAY_PIN, HIGH);
40    delay(3000);
41    digitalWrite(RELAY_PIN, LOW);
42  } else {
43    Serial.println("Access Denied");
44  }
45
46  rfid.PICC_HaltA();
47  rfid.PCD_StopCrypto1();
48}

Code Explanation

  • #include <MFRC522.h>: Includes the library to interface with the MFRC522 RFID reader.
  • authorizedUID[]: Stores the valid UID to match incoming scans.
  • rfid.PICC_IsNewCardPresent(): Checks if a new RFID tag is present.
  • rfid.PICC_ReadCardSerial(): Reads the UID from the detected RFID tag.
  • digitalWrite(RELAY_PIN, HIGH): Activates output (relay or LED) if access is granted.
  • rfid.PICC_HaltA(): Halts the RFID tag communication until the next scan.

Applications

  • RFID Door Lock Systems
  • Attendance Management Systems
  • Electronic Locker Systems
  • RFID-Based Voting Machines
  • Inventory Access Control
  • Smart Campus Access Points

Conclusion

Integrating the MFRC522 RFID module with ESP32 enables secure and contactless access control solutions. This system can be expanded with databases, cloud connectivity, or integrated with IoT dashboards for scalable access management.