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...
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.
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:
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.

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.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 5
#define RST_PIN 27
#define RELAY_PIN 22
MFRC522 rfid(SS_PIN, RST_PIN);
byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) {
if (rfid.PICC_ReadCardSerial()) {
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (rfid.uid.uidByte[0] == authorizedUID[0] &&
rfid.uid.uidByte[1] == authorizedUID[1] &&
rfid.uid.uidByte[2] == authorizedUID[2] &&
rfid.uid.uidByte[3] == authorizedUID[3] ) {
Serial.println("Authorized Tag");
digitalWrite(RELAY_PIN, HIGH);
delay(2000);
digitalWrite(RELAY_PIN, LOW);
}
else
{
Serial.print("Unauthorized Tag with UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
}
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 on the RFID-RC522 reader
Unauthorized Tag with UID: 2B B8 59 B1
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 RFID/NFC tag on the RFID-RC522 reader
Authorized Tag
Tap an RFID/NFC tag on the RFID-RC522 reader
Authorized Tag
Unauthorized Tag with UID: 1C 2A 52 E3
The below ESP32 code allow multiple authorized RFID/NFC tags to activate the relay. The code takes two tags for example. you can add more
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 5
#define RST_PIN 27
#define RELAY_PIN 22
MFRC522 rfid(SS_PIN, RST_PIN);
byte authorizedUID1[4] = {0x3A, 0xC9, 0x6A, 0xCB};
byte authorizedUID2[4] = {0x30, 0x01, 0x8B, 0x15};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) {
if (rfid.PICC_ReadCardSerial()) {
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (rfid.uid.uidByte[0] == authorizedUID1[0] &&
rfid.uid.uidByte[1] == authorizedUID1[1] &&
rfid.uid.uidByte[2] == authorizedUID1[2] &&
rfid.uid.uidByte[3] == authorizedUID1[3] ) {
Serial.println("Authorized Tag 1");
digitalWrite(RELAY_PIN, HIGH);
delay(2000);
digitalWrite(RELAY_PIN, LOW);
}
else
if (rfid.uid.uidByte[0] == authorizedUID2[0] &&
rfid.uid.uidByte[1] == authorizedUID2[1] &&
rfid.uid.uidByte[2] == authorizedUID2[2] &&
rfid.uid.uidByte[3] == authorizedUID2[3] ) {
Serial.println("Authorized Tag 2");
digitalWrite(RELAY_PIN, HIGH);
delay(2000);
digitalWrite(RELAY_PIN, LOW);
}
else
{
Serial.print("Unauthorized Tag with UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
}
Updated two authorized RFID/NFC tag's UUID to the code
Upload the code to ESP32
Tap the authorized tags one-by-one on RFID-RC522 module.
The result on Serial Monitor:
Tap an RFID/NFC tag on the RFID-RC522 reader
Authorized Tag 1
Authorized Tag 2
You can modify the code to add three, four, or more tags.
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.
Follow Us
Share with your friends!