ESP32 - RFID/NFC
In this tutorial, we are going to learn how to use RFID/NFC with ESP32. The RFID/NFC system includes two components: reader and tag. There are two popular RFID/NFC readers: RC522 and PN532 RFID/NFC reader. This tutorial focuses on RC522 RFID/NFC reader. PN532 RFID/NFC reader will be presented in an upcoming tutorial.
RC522 RFID/NFC reader (also called RFID-RC522 Module) can:
- Read the UID of RFID/NFC tag
- Change the UID of RFID/NFC tag (only if the tag is UID-writable)
- Write data to RFID/NFC tag
- Read data from RFID/NFC tag
In above capabilities, for ESP32, reading the UID is the most widely-used. This tutorial focuses on reading the UID of RFID/NFC tag. The other will be present in next tutorials
|1||×||ESP-WROOM-32 Dev Module|
|1||×||Micro USB Cable|
|1||×||RFID/NFC RC522 Kit (reader + tags)|
About RFID-RC522 Module
RFID-RC522 has 8 pins, some of them are common pins, the others are shared among three communication modes: SPI, I2C, UART. At a time, only one communication mode can be used. The pin are:
- GND pin needs to be connected to GND (0V)
- VCC pin needs to be connected to VCC (3.3)
- RST pin is a pin for reset and power-down. When this pin goes low, hard power-down is enabled. On the rising edge, the module is reset.
- IRQ pin is an interrupt pin that can alert the microcontroller when RFID tag comes into its vicinity.
- MISO/SCL/TX pin acts as MISO when SPI interface is enabled, acts as SCL when I2C interface is enabled and acts as TX when UART interface is enabled.
- MOSI pin acts as MOSI when SPI interface is enabled.
- SCK pin acts as SCK when SPI interface is enabled
- SS/SDA/RX pin acts as SS when SPI interface is enabled, acts as SDA when I2C interface is enabled and acts as RX when UART interface is enabled.
※ NOTE THAT:
- Do not connect VCC pin to 5V pin. This will likely destroy your module.
- MFRC522 library supports only SPI mode. Therefore, this tutorial uses only SPI communication.
How RFID/NFC Works
RFID/NFC includes two components: reader and tag.
- The reader consists of a radio frequency module and an antenna which generates high frequency electromagnetic field.
- The tag is usually a passive device, which doesn’t need to have power source. The tag contains a microchip that stores and processes information, and an antenna to receive and transmit a signal. The tag is used to store the information: UID (Unique ID) and data.
To read the information on a tag, the tag must be in close proximity to the reader (does not require the direct line-of-sight). The reading processes:
- The reader generates an electromagnetic field which causes electrons to move through the tag’s antenna and subsequently power the chip.
- The chip inside the tag then responds by sending the requested information back to the reader in the form of another radio signal.
- The reader detects the signal and transform the signal into data
- ESP32 reads the data from reader.
Image is developed using Fritzing. Click to enlarge image
If you use the male-to-female jumper wires, you can connect ESP32 UNO directly to RFID-RC522 module. If you use the male-to-male jumper wires, you need to connect ESP32 UNO to RFID-RC522 module via a breadboard
ESP32 RFID/NFC Code
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
- On Arduino IDE, Go to Tools Manage Libraries
- Search “MFRC522”, then find the library by GithubCommunity
- Click Install button to install keypad library.
- Copy the above code and paste it to Arduino IDE
- Click Upload button on Arduino IDE to compile and upload code to ESP32 board
- Open Serial Monitor on Arduino IDE
- Tap several RFID/NFC tags on RFID-RC522 module
- See UID on Serial Monitor
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.