ESP32 S3 - RFID - Relay

Learn how to connect an RC522 RFID/NFC reader module and a relay to the ESP32 S3 so that only authorized RFID or NFC tags can trigger the relay. This tutorial walks you through everything from wiring the hardware to uploading a finished sketch, giving you a solid foundation for building access-control and automation projects.

What you'll build:

  1. A circuit connecting the RFID-RC522 module and a relay to the ESP32 S3
  2. Firmware that reads an RFID/NFC tag UID and compares it against a list of authorized UIDs
  3. A relay that activates when an authorized tag is detected and stays off for unauthorized tags
  4. A foundation you can extend into door locks, secure enclosures, and industrial access systems

https://newbiely.com/images/cover/esp32-s3-rfid-relay.jpg

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×RFID/NFC RC522 Kit (reader + tags)
1×Relay
10×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of RFID/NFC RC522 Module and Relay

The RFID-RC522 is a low-cost, 13.56 MHz RFID reader module based on the MFRC522 chip from NXP Semiconductors. It communicates with the ESP32 S3 over SPI and detects ISO 14443A-compatible cards and tags — including the popular MIFARE family — at distances of up to about 5 cm, operating at 3.3 V logic so no level-shifting circuitry is needed. A relay is a magnetically operated switch that lets the ESP32 S3's low-voltage GPIO pins safely control high-voltage or high-current loads, making the two components a natural pair for access-control projects.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

https://newbiely.com/images/tutorial/esp32-s3-rfid-nfc-relay-component.jpg

How RC522 Module Works

When an RFID/NFC tag is tapped on the RC522 reader, the ESP32 S3 reads the tag's unique identifier (UID) and checks whether that UID appears in a predefined list of authorized UIDs stored in the sketch. If the UID matches an authorized entry, the ESP32 S3 activates the relay; if the UID is not recognized, the relay remains off and the unauthorized UID is printed to the Serial Monitor for diagnostic purposes.

Key Specifications

  • Operating frequency: 13.56 MHz
  • Communication interface: SPI (default), I2C, UART
  • Operating voltage: 3.3 V (do not connect VCC to 5 V)
  • Read range: up to ~5 cm (depending on tag and antenna orientation)
  • Supported tag types: MIFARE 1KB, MIFARE Ultralight, MIFARE Classic, and other ISO 14443A cards

Wiring Diagram

Connecting the RFID-RC522 module and the relay to the ESP32 S3 requires only jumper wires because the module uses the hardware SPI bus. Keep all wires short to reduce noise on the SPI lines and double-check each connection before powering the board.

The wiring diagram between ESP32 S3 RFID RC522 relay

This image is created using Fritzing. Click to enlarge image

A screw terminal keeps each wire tight, even when the board shakes. A Screw Terminal Block for ESP32 S3 solves this. Plug the ESP32 S3 into it and screw the wires in. Below is the wiring diagram with the screw terminal board.

The wiring diagram between ESP32 S3 RFID RC522 relay Screw Terminal

This image is created using Fritzing. Click to enlarge image

A GPIO Breakout Board for ESP32 S3 works too. Besides one S, 3.3V and GND pin for each GPIO, it has extra 3.3V and 5V pins:

The wiring diagram between ESP32 S3 RFID RC522 relay GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The RFID-RC522 module runs on 3.3 V logic — connecting its VCC pin to the 5 V rail will immediately destroy the module, so always verify your power connection before applying power. The relay module is powered from the 5 V pin on the ESP32 S3 and its control signal comes from a GPIO pin, so keep the high-voltage side of the relay wiring physically separated from the low-voltage control wiring to avoid accidental contact.

RFID/NFC RC522 Module ESP32 S3 Pin
SS pin GPIO10 (SS/CS)
SCK pin GPIO12 (SCK)
MOSI pin GPIO11 (MOSI)
MISO pin GPIO13 (MISO)
IRQ pin (not connected)
GND pin GND
RST pin GPIO39
VCC pin 3.3V
Relay ESP32 S3 Pin
DC- GND
DC+ 5V
IN GPIO7

ESP32 S3 Code - Single RFID/NFC Tag

The following code initializes the SPI bus, the MFRC522 library, and the relay pin, then continuously polls for a new RFID or NFC tag. When a tag is detected, the ESP32 S3 reads its UID and compares it against a single predefined authorized UID — activating the relay on a match or printing an "Unauthorized" message if the tag is not recognized.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-rfid-relay */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 // ESP32-S3 pin GPIO10 #define RST_PIN 39 // ESP32-S3 pin GPIO39 #define RELAY_PIN 7 // ESP32-S3 pin GPIO7 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the RFID-RC522 module and relay to the ESP32 S3 following the wiring diagram above.
  3. Connect the ESP32 S3 to your computer using a USB Type-C cable.
  4. Open Arduino IDE on your computer.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Click the Libraries icon on the left bar of the Arduino IDE.
  7. Type "MFRC522" in the search box, then look for the library by GithubCommunity.
  8. Click the Install button to install the MFRC522 library.
  • Search for MFRC522 created by GithubCommunity and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
MFRC522 by GithubCommunity
Read/Write a RFID Card or Tag using the ISO/IEC 14443A/MIFARE interface. More info
1.4.11
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Copy the above code and paste it into Arduino IDE.
  2. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Open the Serial Monitor in Arduino IDE after uploading.
How to open serial monitor on Arduino IDE
  1. Tap an RFID/NFC tag that you want to authorize on the RC522 reader.
  2. Write down the UID printed in the Serial Monitor.
  3. Update the UID in the code. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; to byte authorizedUID[4] = {0x2B, 0xB8, 0x59, 0xB1}; using the UID from your tag.
  4. Upload the code to the ESP32 S3 again.
  5. Tap the authorized RFID/NFC tag on the RC522 module and observe the relay activating.
  6. Tap an unauthorized tag and observe the relay remaining off.
  7. Pro Tip: Hold each tag flat and parallel to the RC522 antenna coil at a distance of 1–3 cm for the most reliable reads.

Serial Monitor Output

Open the Serial Monitor at 9600 baud after uploading the code, then tap RFID or NFC tags onto the RC522 module to see the authorization result printed for each scan.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 09:22:10] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:22:18] Unauthorized Tag with UID: 2B B8 59 B1 [2026-06-16 09:22:31] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:22:39] Authorized Tag - Relay ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

ESP32 S3 Code - Multiple RFID/NFC Tags

The following code allows multiple authorized RFID/NFC tags to activate the relay. It uses two tags as an example, but you can add as many authorized UIDs as your project requires by extending the array in the sketch.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-rfid-relay */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-rfid-nfc-relay */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 // ESP32-S3 pin GPIO10 #define RST_PIN 39 // ESP32-S3 pin GPIO39 #define RELAY_PIN 7 // ESP32-S3 pin GPIO7 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 } } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Find the UIDs of all tags you want to authorize using the single-tag code from the previous section.
  3. Update the two authorized RFID/NFC tag UIDs in the code above.
  4. Upload the code to the ESP32 S3.
  5. Tap the authorized tags one-by-one on the RC522 module and observe the relay activating for each.
  6. Pro Tip: You can modify the code to authorize three, four, or more tags by adding additional UID entries to the authorized list array.

Serial Monitor Output

Open the Serial Monitor at 9600 baud and tap each authorized tag to verify that all recognized tags activate the relay.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 09:35:04] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:35:12] Authorized Tag 1 - Relay ON [2026-06-16 09:35:25] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:35:33] Authorized Tag 2 - Relay ON [2026-06-16 09:35:46] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:35:54] Unauthorized Tag with UID: 1C 2A 52 E3
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Applications and Project Ideas

The ESP32 S3 paired with an RC522 RFID module and a relay opens up a wide range of access-control and automation applications that can be deployed with minimal extra hardware. Here are practical ways to put this circuit to work:

  1. Electronic door lock: Activate an electromagnetic or solenoid lock only when an authorized card is tapped, replacing physical keys with RFID credentials.
  2. Secure equipment access: Restrict power to machinery or tools so that only personnel with authorized RFID badges can operate them.
  3. Locker system: Build a keyless locker where each student or employee carries a personal RFID card to open their assigned unit.
  4. Attendance-triggered automation: Combine RFID authorization with a relay to automatically start a workstation, coffee machine, or lighting scene when an employee arrives.
  5. Time-limited access control: Grant relay activation only during certain hours by combining RFID authentication with an RTC module for scheduled access.
  6. Visitor management system: Issue temporary RFID tags to guests that activate a relay-controlled gate or turnstile for a defined number of uses.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that your ESP32 S3 can activate a relay with authorized RFID and NFC tags, push the project further with these progressively harder challenges:

  1. Beginner: Add an LED indicator that turns green when an authorized tag is scanned and red when an unauthorized tag is presented.
  2. Beginner: Modify the code so the relay stays ON for exactly five seconds after an authorized tag is detected, then turns off automatically.
  3. Intermediate: Connect a piezo buzzer and play a short success tone for authorized tags and a longer warning tone for unauthorized tags.
  4. Intermediate: Log every scan — authorized and unauthorized — with a timestamp to a Micro SD Card for a persistent audit trail.
  5. Advanced: Build a web-based admin panel using the ESP32 S3's Wi-Fi to add and remove authorized UIDs at runtime without re-flashing the firmware.
  6. Advanced: Combine the RC522 reader with a keypad so the relay activates only when both the correct RFID card and a matching PIN are presented together.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!