ESP8266 - RFID - Relay

This tutorial instructs you how to use an RFID/NFC tag to trigger a relay with the help of ESP8266. You can further expand upon this tutorial by using the relay to control motors, actuators, and more.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×Relay
10×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of RFID/NFC RC522 Module and Relay

If you are not familiar with the RFID/NFC RC522 Module and relay (including pinout, functionality, and programming), then check out the following tutorials:

ESP8266 NodeMCU arduino rfid relay component

How It Works

  • The RFID/NFC reader reads the UID from a tag when it is tapped.
  • ESP8266 then takes this UID and compares it with the UIDs that have been preset in the code.
  • If the UID matches one of these predefined UIDs, the ESP8266 will activate the relay.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and RFID RC522 relay

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

※ NOTE THAT:

The sequence of pins may differ depending on the manufacturer. ALWAYS use the labels printed on the module. The image above displays the pinout of modules from DIYables producer.

ESP8266 Code - Single RFID/NFC Tag

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rfid-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN D8 // The ESP8266 pin D8 #define RST_PIN D2 // The ESP8266 pin D2 #define RELAY_PIN D1 // The ESP8266 pin 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 } } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.

In order to discover the UID of an RFID/NFC tag, the first step is to copy the code and open it with Arduino IDE. After that, click the Upload button on the Arduino IDE to upload the code to the ESP8266. Then, open the Serial Monitor and tap the RFID/NFC tag on the RFID-RC522 module. Finally, the UID will be displayed on the 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 obtaining the UID:

  • Replace the value of byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; with the UID in line 18 of the code. For example, byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Upload the code to ESP8266 again
  • Tap an RFID/NFC tag on the RFID-RC522 module
  • Check out the output on the Serial Monitor
COM6
Send
Tap RFID/NFC tag on reader Authorized Tag
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tap a different RFID/NFC tag on the RFID-RC522 module.
  • Check the output on the 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 facilitate testing, the active time is set to two seconds, however it should be increased for practical use/demonstration.
  • Installation of the MFRC522 library is required. Please refer to ESP8266 - RFID/NFC RC522 tutorial for more information.

ESP8266 Code - Multiple RFID/NFC Tags

We can enable the relay to be activated by multiple RFID/NFC tags. As an illustration, the code below uses two tags.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rfid-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN D8 // The ESP8266 pin D8 #define RST_PIN D2 // The ESP8266 pin D2 #define RELAY_PIN D1 // The ESP8266 pin 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 } } }

Repeat the same steps as before, and then press each tag on the RFID-RC522 module. The output on the Serial Monitor will be similar to the following:

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

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

Video Tutorial