Esp32

Interfacing RC522 RFID Sensor with ESP32 Using Arduino IDE

ESP32 is a power conserving compact microcontroller-based board that contains a number of GPIO pins for interfacing sensors. In this tutorial ESP32 will be interfaced with an RFID sensor using the Arduino code. RC522 is a SPI based smart sensor that can read RFID cards, tags, and smart bands.

Content covered in this article:

1: Introduction to RC522 Sensor

The MFRC522 is a RFID based contactless IC that can read and write data at a frequency of 13.56 MHz. It is designed for easy integration into a wide range of applications, including access control systems, payment terminals, and other systems that require secure wireless communication.

The sensor features a low power consumption design and is compliant with the ISO/IEC 14443 A/MIFARE standard, which allows it to communicate with a wide range of contactless cards and tags.

Additionally, the MFRC522 features a built-in antenna, making it a convenient and compact solution for adding contactless communication capabilities to a project.

2: RC522 Sensor Pinout

The sensor has a total of 8 pins that interface it with a microcontroller or other control device. The pinout of the MFRC522 sensor is as follows:

Diagram Description automatically generated

The SDA, SCK, MOSI, and MISO pins are used to interface the MFRC522 sensor with a microcontroller via a 4-wire Serial Peripheral Interface (SPI) communication protocol.

The IRQ pin can be used to generate an interrupt when certain events occur, such as a successful card or tag read, however it is not commonly used in many projects.

The GND pin connects to the ground of the circuit, and the RST pin is used to reset the sensor.

Finally, the 3.3V pin is used to supply power to the sensor.

It is important to note that these pin names may vary slightly depending on the specific module, so it’s always best to consult the manufacturer’s datasheet for the correct pinout information.

Table Description automatically generated

3: Interfacing RC522 RFID Sensor with ESP32

Interfacing the MFRC522 sensor with ESP32 microcontroller is a simple process that can be accomplished using the MFRC522 library, which is freely available for download. This library provides an easy-to-use set of functions for accessing the sensor’s functionality, including functions for reading and writing data to contactless cards and tags.

Once the library is installed, an example sketch can be found in the examples menu which demonstrates how to initialize the sensor and communicate with a card or tag. In the sketch, it’s important to set the correct pin connections between the ESP32 and the MFRC522 sensor, such as the SPI pins, reset pin and others, according to the model of the ESP32 board being used.

With the correct wiring and the library properly installed, the ESP32 will be able to communicate with the MFRC522 sensor and perform the desired actions such as reading and writing to cards and tags.

For more details on ESP32 SPI protocol and working read the article ESP32 SPI Pins.

3.1: Schematic

RC522 schematic image with ESP32 is shown below:

3.2: Installing the Required Libraries

MFRC522 library is needed for reading the RFID card and tags UID. Open IDE, go to Library Manager and search for the MFRC522 library. Install the library in Arduino IDE.

After installing the MFRC522 library we will read the UID for RFID tags and cards.

3.3: Getting the UID for RFID Card/Tag

Open the DumpInfo example for MFRC522 sensor. Go to: File>Examples>MFRC522>DumpInfo:

Graphical user interface, text, application Description automatically generated

Following code will open in a new IDE window. Upload the code to ESP32. Remember to set the Reset and Slave select pin according to your board. Any of the ESP32 digital pins can be set as RST and SS:

After uploading code to ESP32. Touch and hold the RFID card/tag with MFRC522 sensor:

Sensor will read the data saved inside the RFID tag and display it on the serial monitor. Here we can see the UID for RFID tag stating “02 DC B4 C3”.

Total of 16 (0-15) sectors in which RFID card/tag 1K memory is organized. Four (0-3) blocks are included in each of these 16 sectors. Each block has capacity to store 16 (0-15) bytes of data.

This data represents that:

16 sectors x 4 blocks x 16 bytes of data = 1024 bytes = 1K memory

The Arduino IDE serial monitor shows us distribution of 1K memory of RFID tag. This distribution also contains the sectors, blocks, and data information in rows and columns of the output data:

You can also read the Unique ID (UID) for the card at the end of output:

Table Description automatically generated with medium confidence

4: Reading an RFID Tag Using ESP32

Now we have read the Unique ID (UID) for RFID tag. We will write an Arduino code that saves this card information and grants access to the user if the RFID tag with the same UID is tapped with the MFRC522 sensor.

4.1: Code

Open IDE select ESP32 board and upload given code.

/*
****************
Linuxhint.com
****************
Linuxhint.com
****************
*/

#include
#include
#define SS_PIN  21  /*Slave Select Pin*/
#define RST_PIN 22  /*Reset Pin for RC522*/
#define LED_G   12   /*Pin 8 for LED*/
MFRC522 mfrc522(SS_PIN, RST_PIN);   /*Create MFRC522 initialized*/
void setup()
{
  Serial.begin(9600);   /*Serial Communication begin*/
  SPI.begin();          /*SPI communication initialized*/
  mfrc522.PCD_Init();   /*RFID sensor initialized*/
  pinMode(LED_G, OUTPUT);  /*LED Pin set as output*/
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop()
{
  /*Look for the RFID Card*/
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  /*Select Card*/
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  /*Show UID for Card/Tag on serial monitor*/
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "02 DC B4 C3") /*UID for the Card/Tag we want to give access Replace with your card UID*/
  {
    Serial.println("Authorized access");  /*Print message if UID match with the database*/
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);  /*Led Turn ON*/
    delay(2500);
    digitalWrite(LED_G, LOW);
  }
 else   {
    Serial.println(" Access denied"); /*If UID do not match print message*/
  }
}

Code started by including the SPI and MFRC522 library. Next, we defined the Reset and Slave select pin for the sensor. A LED at pin D12 is initialized as output.

The RFID card which we want to read is initialized by defining the UID. This is the same UID we got using the DumpInfo example code:

An IF condition will check the UID for the card which is tapped with the sensor. If the UID matches the one inside the code LED will turn on and Authorized Access message will be printed, else LED will remain OFF and Access denied message will appear if any other card is tapped.

4.2: Output

In the output we can see the RFID tag is not tapped with the MFRC522 sensor, so the LED is OFF.:

A picture containing text, electronics Description automatically generated

Touch or bring the RFID card/tag near the sensor following output will appear on the serial monitor displaying the card UID:

Graphical user interface, text, application Description automatically generated

LED is turned ON if the access is granted and UID matches with the one we defined inside the code:

Text Description automatically generated with low confidence

We have completed interfacing of RFID tag with RC522 sensor using ESP32 board and IDE.

Conclusion

ESP32 is an IoT board that features all necessary communication interfaces for exchanging data between different devices. ESP32 has several GPIO pins to read data from sensors. Using the SPI protocol ESP32 can read RFID sensor data and multiple projects can be designed. This article covers ESP32 interfacing with RC522 sensor and code required to read any RFID card/tag.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.