ESP32

Interfacing RFID RC522 module with ESP32

Description:

In today’s world, RFID technology has become an essential part of our daily lives. Whether it’s for inventory management, access control, or security systems, RFID tags and readers are used everywhere. In this article, we will explore how to use the popular RFID RC522 module with ESP32 to build a few practical applications.

The first example demonstrates how to read an RFID card and tag using the RC522 module. We will walk through the process of connecting the module to an ESP32 board and writing the code to read the card’s data.

In the second example, we will take it a step further and use an RFID card to control an LED. This example shows how to use the data from the RFID card and tag to control the LED.

Finally, in the third and final example, we will control an electric door lock solenoid using an RFID card. We will go over the wiring and programming required to activate the solenoid when a valid RFID card is detected.

By the end of this article, you will have a good understanding of how to use RFID technology with ESP32 board to build various practical applications.

The RC522 module is a low-cost and easy-to-use RFID reader module that operates on the 13.56 MHz frequency. It supports various types of RFID tags and cards, including ISO/IEC 14443 Type A/B and MIFARE.

By combining the RC522 module with an ESP32 microcontroller, we can build a project that can read RFID tags and display their data on a screen or perform specific actions. This project is an excellent starting point for beginners who want to learn how to work with RFID technology and ESP32 microcontrollers.

To get started with this project, you will need an ESP32 development board, an RC522 module, some jumper wires, and a breadboard. With the help of the Arduino IDE, you can easily program the ESP32 to communicate with the RC522 module and read data from RFID tags.

Overall, this project is an exciting and practical way to learn about RFID technology and microcontroller programming using the ESP32 and RC522 module.




Amazon links:

Esp32 board

Rfid Rc522 module

Breadboard

Jumper wires

LED

electric door lock solenoid

power supply for the door lock

330 Ohm Resistor

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

About ESP32 board:

The ESP32 is a powerful microcontroller developed by Espressif Systems that is designed for low-power, wireless, and IoT (Internet of Things) applications. It is based on the Xtensa LX6 processor, which is a dual-core CPU that can run up to 240 MHz. It also has integrated Wi-Fi and Bluetooth capabilities, making it ideal for IoT applications that require wireless communication.

The ESP32 microcontroller features a wide range of peripherals, including digital and analog I/O, SPI, I2C, UART, PWM, ADC, and DAC. It also has a built-in hall-effect sensor and temperature sensor. The digital I/O pins can be configured as input or output, and can handle voltages up to 3.3V.

One of the key features of the ESP32 is its low-power consumption. It has a deep-sleep mode that allows it to consume as little as 0.1 µA of power, making it ideal for battery-powered applications. It also has an RTC (real-time clock) that can wake up the microcontroller at specified times, further reducing power consumption.

The ESP32 is highly programmable and supports multiple programming languages, including C, C++, MicroPython, and Arduino. It can be programmed using the Espressif SDK or the Arduino IDE, which makes it easy for developers to get started. There are also a number of third-party libraries and tools available that make programming even easier.

The ESP32 has a range of connectivity options, including Wi-Fi, Bluetooth, BLE (Bluetooth Low Energy), and NFC (Near Field Communication). It supports Wi-Fi standards 802.11b/g/n, and can act as an access point or a station. It also supports Bluetooth 4.2 and BLE, which makes it ideal for building low-power Bluetooth devices.

The ESP32 is also highly scalable, and can be used in a variety of applications, including smart homes, wearables, industrial automation, and more. It can be used in both consumer and commercial applications, and is ideal for building connected devices that require low-power, wireless communication.

Just a quick reminder, below you can find the pinout diagram of the ESP32 board.

interfacing RFID RC522 module with ESP32



About RFID RC522 Module:

interfacing RFID RC522 module with ESP32

The RFID RC522 module is a low-cost, easy-to-use device that is designed to read and write RFID tags. It is widely used in various applications, including access control, attendance systems, and inventory management.

The RC522 module uses radio frequency identification (RFID) technology, which is a wireless communication technology that uses radio waves to read and write data from tags or cards. The RFID tags or cards contain a small chip and an antenna that communicate with the RC522 module using electromagnetic waves.

The RC522 module has a compact design and is easy to use. It can be connected to a microcontroller, such as an Arduino or ESP32, through SPI (Serial Peripheral Interface) or I2C (Inter-Integrated Circuit) communication protocols. The module requires a 3.3V power supply and can operate at a frequency of 13.56 MHz.

The RC522 module has a number of features that make it an ideal choice for RFID applications. It has a built-in antenna that can read and write tags up to a distance of 10 cm. It also supports multiple types of RFID tags, including ISO/IEC 14443 Type A/B, MIFARE, and NTAG.

The RC522 module has a range of operating modes, including idle mode, transmit mode, receive mode, and power-down mode. In idle mode, the module is waiting for a tag to be detected. In transmit mode, the module sends data to the tag. In receive mode, the module receives data from the tag. In power-down mode, the module is in a low-power state and consumes very little power.

The RC522 module has a number of registers that can be programmed to control its operation. These registers include the command register, the status register, the control register, and the FIFO (First In, First Out) buffer register. These registers can be accessed using SPI or I2C communication protocols.

The RC522 module also has a built-in EEPROM (Electrically Erasable Programmable Read-Only Memory) that can store up to 1 KB of data. This EEPROM can be used to store data related to the RFID tags, such as access codes or user data.

One of the key features of the RC522 module is its ease of use. It comes with a library for Arduino that makes it easy to read and write RFID tags. The library provides functions for initializing the module, detecting tags, reading tag data, and writing tag data. It also includes example code that can be used as a starting point for developing RFID applications.



RFID RC522 Module PinOut:

interfacing RFID RC522 module with ESP32

The RFID RC522 module has a total of 8 pins, each with its own specific function. Here is a detailed description of each pin:

  • VCC: This pin is used to provide power to the module. It should be connected to a 3.3V or 5V power source, depending on the module’s specifications.
  • GND: This pin is used as the ground reference for the module. It should be connected to the ground of the power source.
  • RST: This pin is used to reset the module. When this pin is pulled low, the module is reset and all the registers are set to their default values.
  • IRQ: This pin is used to receive interrupts from the module. When a tag is detected or an error occurs, the module sends an interrupt to the microcontroller through this pin.
  • MISO: This pin is used for Master In Slave Out communication. It is used to transmit data from the module to the microcontroller.
  • MOSI: This pin is used for Master Out Slave In communication. It is used to transmit data from the microcontroller to the module.
  • SCK: This pin is used as the clock signal for the SPI communication protocol. It synchronizes the data transfer between the microcontroller and the module.
  • NSS/SDA/CS: This pin is used as the chip select signal for the SPI communication protocol. It is used to enable and disable the module during data transfer.

It is important to note that the pin configuration of the RC522 module may differ between different manufacturers and versions of the module. It is always recommended to refer to the datasheet or user manual of the specific module being used for the correct pin configuration. Additionally, the RC522 module should only be powered using a regulated power supply to prevent damage to the module.



RFID RC522 module Features and Specifications:

The RC522 is a contactless smart card reader module that operates at 13.56 MHz. It is based on the MFRC522 chip from NXP Semiconductors and is widely used in a variety of applications such as access control, time and attendance systems, and payment systems.

Here is a brief overview of the RC522’s features and specifications:

Features:

  • Supports ISO/IEC 14443 A/MIFARE and NTAG cards
  • Operates at 13.56 MHz
  • Fast data transfer up to 424 kbps
  • On-board voltage regulator and level shifters for easy integration with microcontrollers
  • Supports SPI interface
  • Integrated antenna for maximum range
  • Built-in anti-collision feature to handle multiple cards in the field
  • Supports encryption and authentication for secure communication

Specifications:

  • Operating voltage: 2.5V to 3.6V
  • Operating temperature range: -20°C to +85°C
  • Dimensions: 40mm x 60mm
  • Communication protocol: SPI
  • Maximum communication distance: 10cm (depending on the antenna size)
  • Maximum card read rate: 50 tags/second
  • Typical current consumption: 13 mA
  • Sleep current consumption: < 80 uA

For more detailed information, you can refer to the MFRC522 datasheet available on the NXP Semiconductors website.

Cards Reading Using RFID RC522 Module with esp32:

The RFID RC522 module is a popular low-cost RFID reader/writer that can be easily integrated with the ESP32 microcontroller. The ESP32 is a powerful and versatile microcontroller that can be programmed using the Arduino IDE and other development platforms. The combination of the RC522 module and the ESP32 offers a cost-effective and easy-to-use solution for a wide range of RFID applications, including access control, inventory management, and asset tracking.

To use the RFID RC522 module with ESP32, you will need to connect the two devices using the SPI communication protocol. The RC522 module has six pins: VCC, GND, RST, IRQ, MISO, MOSI, and SCK. The VCC and GND pins are used to provide power to the module, while the RST pin is used to reset the module. The IRQ pin is used to receive interrupts from the module, while the MISO, MOSI, and SCK pins are used for SPI communication.

The ESP32 also has six pins that are used for SPI communication: SCK, MOSI, MISO, CS, VCC, and GND. The SCK, MOSI, and MISO pins are used for SPI communication, while the CS pin is used to enable and disable the RC522 module. The VCC and GND pins are used to provide power to the module.




RFID RC522 module with ESP32 Circuit diagram:

interfacing RFID RC522 module with ESP32

The circuit diagram describes the connections between an RFID RC522 module and an ESP32 microcontroller. Here’s a detailed explanation of each connection:

  • RC522 VCC -> ESP32 3.3V: This connection provides power to the RFID RC522 module from the ESP32’s 3.3V pin. It’s important to note that the RC522 module requires 3.3V power, so connecting it directly to the ESP32’s 5V pin could damage the module.
  • RC522 GND -> ESP32 GND: This connection provides ground to the RFID RC522 module from the ESP32’s ground pin. It’s important to connect the ground pins of both the module and the microcontroller to ensure a stable circuit.
  • RC522 RST -> ESP32 GPIO0: This connection connects the RST pin of the RFID RC522 module to GPIO0 of the ESP32. This pin is used to reset the module and prepare it for communication with the microcontroller.
  • RC522 IRQ -> Not connected (can be left floating): The IRQ pin of the RC522 module can be used to signal the microcontroller when a card is present or when data is ready to be read. However, it’s not always necessary to use this pin and it can be left unconnected.
  • RC522 MISO -> ESP32 GPIO19: This connection connects the MISO (Master In Slave Out) pin of the RC522 module to GPIO19 of the ESP32. This pin is used for data transfer from the module to the microcontroller.
  • RC522 MOSI -> ESP32 GPIO23: This connection connects the MOSI (Master Out Slave In) pin of the RC522 module to GPIO23 of the ESP32. This pin is used for data transfer from the microcontroller to the module.
  • RC522 SCK -> ESP32 GPIO18: This connection connects the SCK (Serial Clock) pin of the RC522 module to GPIO18 of the ESP32. This pin is used to synchronize data transfer between the module and the microcontroller.
  • RC522 CS/SDA -> ESP32 GPIO5: This connection connects the CS (Chip Select) pin of the RC522 module to GPIO5 of the ESP32. This pin is used to select the module for communication with the microcontroller.

Overall, this circuit diagram shows the necessary connections for the RFID RC522 module and ESP32 microcontroller to communicate with each other. By making these connections and writing code to read and write data to the module, you can build your own RFID-based projects with an ESP32.



RFID RC522 module Library installation:

Installing the RFID RC522 module library using the library manager in the Arduino IDE is a straightforward process. Here are the steps to follow:

First, open the Arduino IDE on your computer.

interfacing RFID RC522 module with ESP32

Click on the Sketch menu, navigate to Include Library, and select Manage Libraries.

interfacing RFID RC522 module with ESP32

In the Library Manager, search for “MFRC522”. The library should appear in the search results. Click on the library and then click the “Install” button.

interfacing RFID RC522 module with ESP32

Wait for the installation process to complete.

interfacing RFID RC522 module with ESP32

After the installation is complete, close the Library Manager and upload the following code.



Card Reading using RFID RC522 module with esp32 Code:

Code explanation:

This code includes the necessary libraries for using the MFRC522 RFID (Radio Frequency Identification) reader with an Arduino board using SPI (Serial Peripheral Interface) communication protocol. The code defines two constants – “RST_PIN” and “SDA_PIN” which represent the reset and slave select pins of the RFID module respectively.

this code creates an instance of the “MFRC522” class with the given “SDA_PIN” and “RST_PIN” values. The constructor for the MFRC522 class takes two parameters: the slave select pin and the reset pin of the RFID module. In this case, the “rfid” object is created with “SDA_PIN” and “RST_PIN” as parameters.

Once the “rfid” object is created, it can be used to communicate with the MFRC522 RFID module and perform various operations like reading the UID (unique identifier) of RFID tags, writing data to RFID tags, etc.

This code is the “setup()” function, which is a part of an Arduino sketch that runs once at the beginning of the program.

The first line initializes serial communication with the “Serial.begin()” function. This function sets up the serial communication protocol and starts communication with the specified baud rate of 9600 bits per second. This allows the ESP32 to communicate with a computer or other serial devices, and send or receive data over the serial interface.

The second line initializes SPI communication with the “SPI.begin()” function. This function sets up the SPI communication protocol and initializes the SPI pins of the ESP32 board for communication with the MFRC522 RFID (Radio Frequency Identification) module.

The third line initializes the RC522 module with the “rfid.PCD_Init()” function. This function initializes the RFID module and prepares it for communication with the ESP32 board.

Overall, this code sets up the necessary communication protocols between the ESP32 board and the MFRC522 RFID module, and initializes the RC522 module, preparing it for reading RFID tags.

This code is the “loop()” function, which is a part of an Arduino sketch that runs repeatedly in a loop after the “setup()” function is executed.

The “if” statement checks whether a new RFID card is present and successfully read by the RC522 module. If a card is present and read, the code inside the “if” statement is executed.

Inside the “if” statement, the UID (Unique Identification) of the card is printed on the serial monitor using the “Serial.print()” function. A “for” loop is used to iterate through the bytes of the UID and print them in decimal format using the “Serial.print()” function with the second argument as “DEC”.

A couple of lines of text are also printed on the serial monitor using the “Serial.println()” function to provide additional information or feedback to the user.

Finally, a delay of 1 second is added using the “delay()” function to prevent repeated scanning of the same card too quickly.

Overall, this code continuously checks for the presence of a new RFID card, reads its UID, and prints it on the serial monitor in decimal format.

Practical demonstration:

interfacing RFID RC522 module with ESP32



How to control Led with rfid rc522 using esp32

In this section, we will guide you through the process of setting up the hardware to control an LED using an RFID rc522 module and esp32.

Hardware Requirements:

To complete this project, you will need the following hardware components:

  • ESP32 Development Board
  • RFID-RC522 Module
  • LED
  • 330 Ohm Resistor
  • Breadboard and jumper wires

The RFID-RC522 module is a popular RFID reader and writer that works on the 13.56 MHz frequency. The module communicates with the ESP32 via the Serial Peripheral Interface (SPI) protocol. The LED will serve as the output that is controlled by the RFID card or tag.

Controlling led with RFID RC522 with ESP32 Circuit diagram:

interfacing RFID RC522 module with ESP32

Firstly, the LED is connected to the ESP32 GPIO25 pin. This means that the ESP32 can control the LED by sending a signal to this pin. When the pin is set to high, the LED will turn on, and when it is set to low, the LED will turn off.

Secondly, the RFID RC522 module is connected to the ESP32. The 3.3V pin on the RC522 module is connected to the 3.3V pin on the ESP32, which provides power to the module. The RST pin on the RC522 module is connected to the GPIO0 pin on the ESP32, which is used to reset the module. The GND pin on the RC522 module is connected to the GND pin on the ESP32, which provides a common ground for the circuit.

Thirdly, the SPI communication pins on the RC522 module are connected to the appropriate GPIO pins on the ESP32. The MISO pin on the RC522 module is connected to the GPIO19 pin on the ESP32, which is used for data input. The MOSI pin on the RC522 module is connected to the GPIO23 pin on the ESP32, which is used for data output. The SCK pin on the RC522 module is connected to the GPIO18 pin on the ESP32, which is used for clock synchronization. Finally, the SDA pin on the RC522 module is connected to the GPIO19 pin on the ESP32, which is used for chip select.




Controlling Led Using Rfid RC522 and ESP32 Program:

Program Explanation:

These two lines import the required libraries for the SPI interface and the MFRC522 RFID module.

These lines define the pins to be used for the reset, slave select, and LED. In this code, the reset pin is connected to gpio 0, the slave select pin is connected to pin 5, and the LED pin is connected to gpio 25. These gpio can be changed based on the actual connections.

This line creates an instance of the MFRC522 class with the specified slave select (SDA_PIN) and reset (RST_PIN) pins. The rfid object is used to interact with the RFID module in the code.

This line sets the LED pin, which was defined as gpio 25 in the previous code, as an output pin. This means that the pin can be used to control an LED.

This line initializes the serial communication with a baud rate of 9600. This means that data can be sent and received through the serial port, which can be viewed using the Serial Monitor in the Arduino IDE.

This line initializes the SPI interface for communication with the RFID module.

This line initializes the RFID module with the settings specified in the MFRC522.h library. The PCD_Init() function sets up the hardware and enables the antenna of the RFID module.

These lines print messages to the serial monitor indicating that the RFID module is ready to scan and read the RFID tags. The empty Serial.println(“”) statement is used to insert a blank line after the message is printed.

This line checks if a new card is present on the RFID reader and reads its serial number. The PICC_IsNewCardPresent() function checks if there is a new card present on the RFID reader, and PICC_ReadCardSerial() reads the serial number of the card.

This line prints a message to the serial monitor indicating that the RFID card’s unique identifier (UID) is being printed.

This line initializes a string variable called uid to store the card’s UID.

This loop reads the bytes of the card’s UID and stores them in the uid string variable. It also prints each byte to the serial monitor. The uidByte array contains the individual bytes of the card’s UID, and size is the number of bytes in the UID.

This code block checks if the card’s UID matches any of the predefined UIDs. If the UID matches, it prints a message to the serial monitor indicating the name of the user associated with the UID, and turns on the LED connected to the microcontroller for 5 seconds. If the UID does not match any of the predefined UIDs, it prints a message to the serial monitor indicating that the user is not registered.

These lines halt the RFID communication and stop the encryption process to free up the resources and prepare the RFID module for the next scan.

Practical Demonstration:

interfacing RFID RC522 module with ESP32



How to control electric door lock solenoid with rfid rc522 using esp32

RFID (Radio Frequency Identification) technology is widely used for access control in various applications, such as home security, office buildings, and industrial sites. In this example, we will show you how to use an RFID RC522 reader with an ESP32 microcontroller to control an electric door lock solenoid.

The basic idea is to use the RFID RC522 reader to read the RFID tags, and if the tag matches with the authorized users, the ESP32 will send a signal to the electric door lock solenoid to open or close the door.

Required components:

  • An ESP32 development board
  • An RFID RC522 reader module
  • An electric door lock solenoid
  • A power supply for the door lock
  • Jumper wires
  • Breadboard

About electric door lock solenoid:

An electric door lock solenoid is a type of locking mechanism that is commonly used in electronic access control systems. It consists of an electromagnetic coil that is energized when an electrical current is applied, which creates a magnetic field that moves a plunger or pin to either lock or unlock a door.

The solenoid is typically mounted on the door frame or inside the door itself, and it is connected to a locking mechanism that secures the door in place. When the solenoid is energized, the plunger or pin is drawn into the coil, which moves the locking mechanism to unlock the door. Conversely, when the solenoid is de-energized, the plunger or pin is released, and the locking mechanism returns to its locked position, securing the door.

Electric door lock solenoids offer several advantages over traditional locks. They are easy to install and can be integrated into existing access control systems, making them an ideal choice for retrofitting older buildings. They also provide quick and reliable access control, as the solenoid can be activated and deactivated in a matter of milliseconds.

Another benefit of electric door lock solenoids is that they can be controlled remotely, which allows for greater flexibility and convenience. They can be connected to a variety of electronic devices, such as keypads, card readers, or biometric scanners, which provide secure and convenient access control.



Electric door lock solenoid with esp32 Circuit diagram:

interfacing RFID RC522 module with ESP32

All the other connections are exactly the same, this time round, I connected a relay and I am using GPIO pin 27 to control this 1-channel relay module. You can clearly see, I have connected 12V wire from the power supply to the common contact of the relay module. The GND wire of the Electronic Lock is connected directly to the GND wire of the 12V power supply, while the Normally Open “ NO” contact of the relay module is connected to the +ve wire of the E-Lock or Electronic Door Lock. So, by turning ON and turning OFF this relay module we can open and close this electronic door lock.

Controlling Electric door lock solenoid Esp32 Code:

Code explanation:

This is an Arduino sketch written in C++ that uses the MFRC522 library to read data from an RFID reader and control an electronic lock.

First, two libraries are included: SPI.h and MFRC522.h. The SPI.h library is used for communicating with the RC522 module, while MFRC522.h provides the functions for reading from the RFID tag.

The #define statements define two constant values RST_PIN and SDA_PIN which are the pin numbers used to communicate with the RC522 module. The ELock is defined as 27, which will be used as the output pin for the electric door lock solenoid.

Next, an instance of the MFRC522 class is created with the SDA_PIN and RST_PIN values passed as arguments to the constructor.

In the setup() function, the ELock pin is set as an output, the serial communication is started at a baud rate of 9600, the SPI communication is initialized and the PCD_Init() function is called to initialize the RC522 module.

The loop() function is where the main functionality of the program resides. The program uses the PICC_IsNewCardPresent() function to check if a new card has been placed near the reader, and the PICC_ReadCardSerial() function to read the data from the card. If a new card is present and data is successfully read, the program checks if the UID of the card matches one of the pre-defined UIDs, “667117630” or “57011182176”.

If the UID matches one of the pre-defined values, the program sets the ELock gpio high to unlock the electronic lock, waits for 5 seconds with the delay() function and sets the ELock gpio low to lock the electronic lock again. If the UID does not match any pre-defined value, the program prints “unregistered user” to the serial monitor.

Finally, the PICC_HaltA() and PCD_StopCrypto1() functions are called to stop reading the card and disable encryption. The program then repeats the loop, waiting for a new card to be placed near the reader.



Practical Demonstration:

interfacing RFID RC522 module with ESP32

Watch Video Tutorial:

 

Related Article:

PN532 RFID NFC Module with Arduino, How to use HSU UART, SPI, & I2C

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button