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

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×RFID Card
1×Relay
10×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.

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:

ESP32 ESP32 rfid relay component

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

ESP32 RFID RC522 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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 // ESP32 pin GPIO5 #define RST_PIN 27 // ESP32 pin GPIO27 #define RELAY_PIN 32 // ESP32 pin GPIO32 connects to relay MFRC522 rfid(SS_PIN, RST_PIN); byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // deactivate the relay Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed 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); // activate the relay for 2 seconds delay(2000); digitalWrite(RELAY_PIN, LOW); // deactivate the relay } 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(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }

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.
ESP32 MFRC522 library
  • 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
    How to 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
    COM6
    Send
    Tap an RFID/NFC tag on the RFID-RC522 reader Unauthorized Tag with UID: 2B B8 59 B1
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
    • 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
    COM6
    Send
    Tap an RFID/NFC tag on the RFID-RC522 reader Authorized Tag
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
    • Tap an unauthorized RFID/NFC tag on RFID-RC522 module
    • See the result on Serial Monitor
    COM6
    Send
    Tap an RFID/NFC tag on the RFID-RC522 reader Authorized Tag Unauthorized Tag with UID: 1C 2A 52 E3
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

ESP32 Code - Multiple RFID/NFC Tags

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

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 // ESP32 pin GPIO5 #define RST_PIN 27 // ESP32 pin GPIO27 #define RELAY_PIN 32 // ESP32 pin GPIO32 connects to relay 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(); // init SPI bus rfid.PCD_Init(); // init MFRC522 pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // deactivate the relay Serial.println("Tap an RFID/NFC tag on the RFID-RC522 reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed 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); // activate the relay for 2 seconds delay(2000); digitalWrite(RELAY_PIN, LOW); // deactivate the relay } 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); // activate the relay for 2 seconds delay(2000); digitalWrite(RELAY_PIN, LOW); // deactivate the relay } 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(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }

Quick Instructions

  • 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:
COM6
Send
Tap an RFID/NFC tag on the RFID-RC522 reader Authorized Tag 1 Authorized Tag 2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can modify the code to add three, four, or more tags.

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.

※ OUR MESSAGES