Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Project 7: RFID

Introduction

The MFRC522 is a widely used RFID module that communicates over SPI to read RFID tags.
It is commonly used for access control systems, attendance tracking, and automation projects.
The ESP32 can interface with the MFRC522 to read RFID tag data and process it for various applications.
In this tutorial, we’ll connect the MFRC522 RFID reader to the ESP32 and read RFID tag data.

Required Components

  • ESP32 Dev Board
  • RFID Module with tags
  • Jumper wires
  • Breadboard (optional)

Pinout

Circuit Diagram / Wiring

  • RFID SENSOR VCC → VIN, 5V (ESP32)
  • RFID SENSOR GND → GND (ESP32)
  • RFID SENSOR SDA  → GPIO 21 (ESP32)
  • RFID SENSOR SCK  → GPIO 18 (ESP32)
  • RFID SENSOR MOSI → GPIO 23 (ESP32)
  • RFID SENSOR MISO  → GPIO 19 (ESP32)
  • RFID SENSOR RST  → GPIO 22 (ESP32)

Code / Programming

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

#define RST_PIN 22   // Reset pin
#define SS_PIN 21    // SDA pin (Slave Select)

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

void setup() {
  Serial.begin(115200);  // Start serial communication
  SPI.begin();           // Initialize SPI bus
  mfrc522.PCD_Init();    // Initialize MFRC522 RFID reader
  Serial.println("Place your RFID card near the reader.");
}

void loop() {
  // Check if a new RFID card is present
  if (mfrc522.PICC_IsNewCardPresent()) {
    if (mfrc522.PICC_ReadCardSerial()) {
      // Print the UID of the RFID card
      Serial.print("UID tag: ");
      for (byte i = 0; i < mfrc522.uid.size; i++) {
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
      mfrc522.PICC_HaltA();  // Halt PICC (RFID card)
    }
  }
}

Explanation

  • The MFRC522 RFID reader is initialized and configured using the mfrc522.PCD_Init() function.
  • When a card is detected, its UID (Unique Identifier) is read and printed to the Serial Monitor.
  • mfrc522.PICC_HaltA() stops communication with the card after reading the UID.

Testing and Troubleshooting

  • Ensure the wiring connections between the MFRC522 and the ESP32 are correct, particularly the SPI pins.
  • If the RFID card isn’t detected, try moving it closer or ensure that the card is compatible with the MFRC522.
  • If the Serial Monitor shows no output, check for issues in power supply and ensure that the SPI and MFRC522 libraries are correctly installed.

    Leave a Reply

    Your email address will not be published.

    Need Help?