Jump to content

RFID Module: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
Created page with "===Interfacing RFID Reader with ESP32 using MFRC522=== '''Interfacing RFID Reader with ESP32 using MFRC522''' demonstrates how an MFRC522 RFID reader can be interfaced with an ESP32 board using the SPI communication interface to read the unique identification number (UID) of an RFID card or tag. The ESP32 communicates with the MFRC522 RFID reader through the '''SPI''' interface. When an RFID card is placed near the reader, the card UID is read and displayed on the Seri..."
 
CogWikiEditor (talk | contribs)
Line 151: Line 151:
| MFRC522
| MFRC522
| VCC
| VCC
| Appropriate supply
| Appropriate supply 3.3V/5V
| Power
| Power
|-
|-
Line 159: Line 159:
| Ground
| Ground
|}
|}
Always try MRF522 with 3.3V first, and if it doesn't work then utilize 5V


== Steps for Assembling the Circuit ==
== Steps for Assembling the Circuit ==

Revision as of 04:06, 4 September 2026

Interfacing RFID Reader with ESP32 using MFRC522

Interfacing RFID Reader with ESP32 using MFRC522 demonstrates how an MFRC522 RFID reader can be interfaced with an ESP32 board using the SPI communication interface to read the unique identification number (UID) of an RFID card or tag.

The ESP32 communicates with the MFRC522 RFID reader through the SPI interface. When an RFID card is placed near the reader, the card UID is read and displayed on the Serial Monitor.

RFID Card → MFRC522 Reader → SPI → ESP32 → Serial Monitor

Objectives

  • To study the working of an RFID reader.
  • To interface the MFRC522 RFID reader with the ESP32.
  • To understand SPI communication between the ESP32 and MFRC522.
  • To read the UID of an RFID card or tag.
  • To display the card UID on the Serial Monitor.

Software

  • Arduino IDE
  • MFRC522 library

Hardware Modules

  • ESP32 Board
  • MFRC522 RFID Reader Module
  • RFID Card or RFID Tag
  • PC / Laptop
  • Connecting wires

Theory

Introduction to RFID

RFID stands for Radio Frequency Identification.

RFID is a technology used to identify objects using radio-frequency communication between a reader and an RFID tag or card.

An RFID system generally consists of:

  • RFID Reader
  • RFID Card or Tag

The RFID reader generates a radio-frequency field. When a compatible RFID card or tag is brought near the reader, the card communicates with the reader and provides identification information.

RFID Reader ↔ RFID Card / Tag

The MFRC522 is an RFID reader/writer module commonly used for reading and writing compatible RFID cards and tags.

MFRC522 RFID Reader

The MFRC522 RFID reader communicates with the ESP32 using the SPI communication protocol.

The SPI interface uses separate lines for clock, data input and data output, along with a chip-select signal.

In this practical, the following ESP32 pins are used:

MFRC522 Pin ESP32 Function
SDA / SS GPIO 15 Slave Select
RST GPIO 33 Reset
SCK GPIO 14 SPI Clock
MISO GPIO 21 Master In Slave Out
MOSI GPIO 25 Master Out Slave In

SPI Communication

SPI stands for Serial Peripheral Interface.

SPI is a synchronous serial communication protocol used for communication between a controller and peripheral devices.

The main SPI signals used in this practical are:

Signal Description
SCK Serial Clock
MISO Master In Slave Out
MOSI Master Out Slave In
SS / SDA Slave Select

The ESP32 acts as the SPI controller and the MFRC522 acts as the SPI peripheral.

ESP32 → SPI → MFRC522

Connections

Device Pin ESP32 Function
MFRC522 SDA / SS GPIO 15 Slave Select
MFRC522 RST GPIO 33 Reset
MFRC522 SCK GPIO 14 SPI Clock
MFRC522 MISO GPIO 21 SPI Data
MFRC522 MOSI GPIO 25 SPI Data
MFRC522 VCC Appropriate supply 3.3V/5V Power
MFRC522 GND GND Ground

Always try MRF522 with 3.3V first, and if it doesn't work then utilize 5V

Steps for Assembling the Circuit

  1. Connect the SDA / SS pin of the MFRC522 to GPIO 15 of the ESP32.
  2. Connect the RST pin of the MFRC522 to GPIO 33 of the ESP32.
  3. Connect the SCK pin of the MFRC522 to GPIO 14 of the ESP32.
  4. Connect the MISO pin of the MFRC522 to GPIO 21 of the ESP32.
  5. Connect the MOSI pin of the MFRC522 to GPIO 25 of the ESP32.
  6. Connect the GND pin of the MFRC522 to the GND of the ESP32.
  7. Connect the RFID reader to the appropriate power supply.
  8. Connect the ESP32 to the PC/Laptop using a USB cable.

Safety Precautions

  • First, make all the connections according to the circuit diagram.
  • Check the MFRC522 pin configuration before connecting the module.
  • Ensure that the SPI pins are connected correctly.
  • Ensure that VCC and GND connections are correct.
  • Do not interchange the MISO and MOSI connections.
  • Then connect the ESP32 board to the PC/Laptop.
  • Keep the RFID card within the operating range of the reader while testing.

Schematic Diagram

The MFRC522 RFID reader is connected to the ESP32 using the SPI interface.

RFID Card → MFRC522 → SPI Interface → ESP32 → Serial Monitor

Interfacing Diagram Using Breadboard

The MFRC522 module can be connected to the ESP32 using a breadboard and jumper wires.

The SPI connections are:

MFRC522 ESP32
SDA / SS GPIO 15
RST GPIO 33
SCK GPIO 14
MISO GPIO 21
MOSI GPIO 25

Procedure

  1. Install the MFRC522 library in the Arduino IDE.
  2. Connect the MFRC522 RFID reader to the ESP32 according to the connection table.
  3. Write the program given below.
  4. Save and compile the program.
  5. Connect the ESP32 board to the PC/Laptop using a USB cable.
  6. Upload the program to the ESP32.
  7. Open the Serial Monitor.
  8. Set the Serial Monitor baud rate to 115200.
  9. Place an RFID card or tag near the MFRC522 reader.
  10. Observe the UID of the RFID card on the Serial Monitor.

Algorithm

The following algorithm is used to interface the MFRC522 RFID reader with the ESP32:

  1. Start the Arduino IDE.
  2. Include the SPI library.
  3. Include the MFRC522 library.
  4. Define the SS pin as GPIO 15.
  5. Define the RST pin as GPIO 33.
  6. Create an MFRC522 RFID reader object.
  7. In the void setup() function, initialize Serial communication at 115200 baud.
  8. Initialize the ESP32 SPI interface using the specified SPI pins.
  9. Initialize the MFRC522 RFID reader.
  10. In the void loop() function, check whether a new RFID card is present.
  11. If no card is present, wait for the next card.
  12. Read the RFID card serial number.
  13. Read each byte of the card UID.
  14. Display the UID on the Serial Monitor.
  15. Halt communication with the RFID card.
  16. Repeat the process continuously.

The overall operation is:

RFID Card → Detect Card → Read UID → Display UID → Wait for Next Card

Working

The ESP32 initializes the SPI interface using the following pins:

SPI Signal ESP32 Pin
SCK GPIO 14
MISO GPIO 21
MOSI GPIO 25
SS GPIO 15

The MFRC522 reader is initialized using the PCD_Init() function.

The program continuously checks whether a new RFID card is present using:

rfid.PICC_IsNewCardPresent()

If a card is detected, the program reads the card serial number using:

rfid.PICC_ReadCardSerial()

The UID bytes stored in the RFID reader are then displayed on the Serial Monitor.

RFID Card → MFRC522 → UID → ESP32 → Serial Monitor

After the UID is displayed, the card is halted using:

rfid.PICC_HaltA();

The reader then waits for another RFID card or tag.

Functions Used

Function Purpose
SPI.begin() Initializes the SPI interface using the specified pins
rfid.PCD_Init() Initializes the MFRC522 reader
rfid.PICC_IsNewCardPresent() Checks whether a new RFID card is present
rfid.PICC_ReadCardSerial() Reads the RFID card serial number
rfid.PICC_HaltA() Halts communication with the RFID card

Program

#include <SPI.h>

#include <MFRC522.h>

#define SS_PIN 15

#define RST_PIN 33

// SDA -> 15

// RST -> 33

// SCK -> 14

// MISO -> 21

// MOSI -> 25


MFRC522 rfid(SS_PIN, RST_PIN);

void setup() {

  Serial.begin(115200);

  // ESP32 SPI pins

  SPI.begin(14, 21, 25, 15);  // SCK, MISO, MOSI, SS

  rfid.PCD_Init();

  Serial.println("RFID Reader Ready");

  Serial.println("Place your card near the reader...");

}

void loop() {

  // Check if card present

  if (!rfid.PICC_IsNewCardPresent()) {

    return;

  }

  // Read card

  if (!rfid.PICC_ReadCardSerial()) {

    return;

  }

  Serial.print("UID Tag: ");

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

    Serial.print(rfid.uid.uidByte[i]);

    Serial.print(" ");

  }

  Serial.println();

  Serial.println();

  rfid.PICC_HaltA();

}

Observation

Open the Serial Monitor and set the baud rate to 115200.

Initially, the Serial Monitor displays:

RFID Reader Ready
Place your card near the reader...

When an RFID card or tag is placed near the MFRC522 reader, its UID is displayed.

An example output format is:

UID Tag: 163 45 78 201

The actual UID depends on the RFID card or tag being used.

Result

The ESP32 successfully interfaces with the MFRC522 RFID reader using SPI communication.

The RFID reader successfully detects an RFID card or tag and reads its UID. The UID is displayed on the Serial Monitor at a baud rate of 115200.