ESP32 - RFID/NFC - Relay
This tutorial instructs you how to use ESP32, RFID/NFC and relay. In detail, we will learn how to activate a relay when an anthorized RFID/NFC tag is tapped on RFID reader.
This tutorial can be extended by connecting the relay to an electromagnetic lock, actuator...
Hardware Used In This Tutorial
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Introduction to RFID/NFC RC522 Module and Relay
We have specific tutorials about RFID/NFC RC522 Module and relay. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:
How RC522 Module Works
- Several authorized UIDs of RFID/NFC tags are predefined in ESP32 code
- When an RFID/NFC tag is tapped on a RFID/NFC reader
- ESP32 read the UID from the RFID/NFC reader
- ESP32 looks for the read UID in the predefined UIDs list
- If the predefined UIDs list contains the read UID, ESP32 activates the relay.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
The pins order can vary according to manufacturers. ALWAYS use the labels printed on the module. The above image shows the pinout of the modules from DIYables manufacturer.
If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.
ESP32 Code - Single RFID/NFC Tag
Quick Instructions
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Type “MFRC522” on the search box, then look for the library by GithubCommunity
- Install the library by clicking on Install button.
- Find out the tag's UID by doing the following steps:
- Copy the above code and paste it to Arduino IDE.
- Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
- Open Serial Monitor on Arduino IDE
- Tap an RFID/NFC tag that you want to give authorization on RFID-RC522 reader
- Write down the UID printed on Serial Monitor
- Update the UID in the line 18 of the above code. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte authorizedUID[4] = {0x2B, 0xB8, 0x59, 0xB1};
- Upload the code to ESP32 again
- Tap the authorized RFID/NFC tag on RFID-RC522 module
- See the result on Serial Monitor
- Tap an unauthorized RFID/NFC tag on RFID-RC522 module
- See the result on Serial Monitor
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.