ESP32 - RFID/NFC - Relay

In this tutorial, we are going to learn how to use RFID/NFC tag to activate relay using ESP32. You can extend this tutorial by using the relay to control motor, actuator...

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×RFID/NFC RC522 Kit (reader + tags)
1×Relay
10×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About RFID/NFC RC522 Module and Relay

If you do not have knowledge of RFID/NFC RC522 Module and relay (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:

How It Works

  • The UIDs of some RFID/NFC tags are preset in ESP32 code
  • User taps an RFID/NFC tag on RFID/NFC reader
  • The reader reads UID from the tag.
  • ESP32 gets the UID from the reader
  • ESP32 compares the read UID with the predefined UIDs
  • If the UID is matched with one of the predefined UIDs, ESP32 activates the relay.

Wiring Diagram

ESP32 RFID RC522 relay wiring diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 Code - Single RFID/NFC Tag

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 // ESP32 pin GIOP5 #define RST_PIN 27 // ESP32 pin GIOP27 #define RELAY_PIN 22 // ESP32 pin GIOP22 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 RFID/NFC Tag on 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 Steps

Because UID is usually not printed on RFID/NFC tag, The first step we need to do is to find out the tag's UID. This can be done by:

  • 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
  • How to open serial monitor on Arduino IDE
  • Tap an RFID/NFC tag on RFID-RC522 module
  • Get UID on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Unauthorized Tag with UID: 3A C9 6A CB
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

After having UID:

  • Update UID in the line 18 of the above code. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; TO byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Upload the code to ESP32 again
  • Tap an RFID/NFC tag on RFID-RC522 module
  • See output on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Authorized Tag
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • Tap another RFID/NFC tag on RFID-RC522 module
  • See output on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader Authorized Tag Unauthorized Tag with UID: BD 1E 1D 00
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

※ NOTE THAT:

  • To make it easy to test, The active time is 2 seconds, it should be increased in practical use/demonstration
  • It need to install MFRC522 library. See ESP32 - RFID/NFC RC522 tutorial

ESP32 Code - Multiple RFID/NFC Tags

We can allow multiple RFID/NFC tags to activate the relay. The below code uses two tags as an example.

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 5 // ESP32 pin GIOP5 #define RST_PIN 27 // ESP32 pin GIOP27 #define RELAY_PIN 22 // ESP32 pin GIOP22 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 RFID/NFC Tag on 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 } } }

Do the similar steps as the above, and then tap one by one tag on RFID-RC522 module. The result on Serial Monitor looks like below:

COM6
Send
Tap RFID/NFC tag on reader Authorized Tag 2 Authorized Tag 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can extend the above code for three, four, or more tags.

Video Tutorial

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.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere