ESP32 S3 - RFID Door Lock System

Learn how to wire an RFID/NFC RC522 module and a relay-controlled electromagnetic or solenoid lock to an ESP32 S3 to build a fully functional RFID door lock system. By storing authorized tag UIDs directly in the firmware, the ESP32 S3 instantly grants or denies access every time a card or tag is tapped on the reader — no network connection required.

What you'll build:

  1. A circuit connecting the RFID-RC522 module, a relay, and an electromagnetic or solenoid lock to the ESP32 S3
  2. Firmware that reads the UID from any tapped RFID or NFC tag and compares it to a stored list of authorized UIDs
  3. A single-key variant that unlocks the door for one authorized tag and denies all others
  4. A multi-key variant that maps each authorized tag to a named user (for example, manager and secretary)

https://newbiely.com/images/cover/esp32-s3-rfid-door-lock-system.jpg

Overview of RFID/NFC RC522 Module and Electromagnetic Lock

The RFID-RC522 is a low-cost 13.56 MHz reader module based on the MFRC522 chip from NXP Semiconductors. It communicates with the ESP32 S3 over SPI, operates at 3.3 V logic (directly compatible with the ESP32 S3), and can detect ISO 14443A-compatible cards and tags at distances of up to about 5 cm. An electromagnetic or solenoid lock converts an electrical signal from the ESP32 S3 (routed through a relay) into mechanical bolt movement, making it the ideal actuator for a physical access-control system.

Key Specifications

We have dedicated tutorials covering each component in depth, including their pinout, working principle, and wiring to the ESP32 S3. Learn more about them at the following links:

System Components

A door lock system built with the ESP32 S3 consists of two main parts:

  • Door Lock System: ESP32 S3, RFID/NFC RC522 reader, relay, and solenoid or electromagnetic lock
  • Door Key: RFID/NFC tags (cards, key fobs, or stickers)

https://newbiely.com/images/tutorial/esp32-s3-rfid-door-lock-component.jpg

How RFID/NFC Door Lock Works

The UIDs of authorized tags are predefined in the ESP32 S3 firmware. When a user taps an RFID or NFC tag on the RC522 reader, the ESP32 S3 reads the UID returned by the module and compares it against the stored list. If the UID matches an authorized entry, the ESP32 S3 deactivates the relay to unlock the electromagnetic or solenoid lock; otherwise, access is denied and the lock stays engaged.

Wiring Diagram

Connect the RC522 module, relay, and electromagnetic or solenoid lock to the ESP32 S3 following the diagram below. Keep the SPI wires between the RC522 and the ESP32 S3 short to reduce noise, and always verify the 3.3 V power connection to the RC522 before applying power.

The wiring diagram between ESP32 S3 RFID RC522 Door Lock System

This image is created using Fritzing. Click to enlarge image

If you plan to keep this circuit, think about screw terminals. Simply plug the ESP32 S3 into a Screw Terminal Block for ESP32 S3. Below is the wiring diagram with the screw terminal board.

The wiring diagram between ESP32 S3 RFID RC522 Door Lock System 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 Door Lock System GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The RFID-RC522 module operates at 3.3 V — connecting its VCC pin to 5 V will permanently damage the module, so double-check the power rail before energising the circuit. The electromagnetic or solenoid lock runs on 12 V DC and must be switched through the relay; never connect it directly to a GPIO pin of the ESP32 S3. Keep the 12 V wiring on the relay's load side physically separated from the low-voltage SPI wiring to avoid noise-induced read errors.

※ NOTE THAT:

The pin order on the RC522 module can vary between manufacturers. Always use the labels printed on the module itself. The image above shows the pinout for modules from the DIYables manufacturer.

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 GPIO9
VCC pin 3.3V

ESP32 S3 Code - Single Key

The following code initializes the SPI bus and the MFRC522 library, then continuously polls for a new tag. When a tag is detected, its UID is read and compared against the single authorized UID stored in the firmware — if they match, the relay is briefly activated to unlock the door; otherwise, the UID is printed to the Serial Monitor and access is denied.

/* * 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-door-lock-system */ /* * 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-door-lock-system */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 // ESP32-S3 pin GPIO10 #define RST_PIN 9 // ESP32-S3 pin GPIO9 #define RELAY_PIN 7 // ESP32-S3 pin GPIO7 connects to relay MFRC522 rfid(SS_PIN, RST_PIN); byte keyTagUID[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, HIGH); // lock the door 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] == keyTagUID[0] && rfid.uid.uidByte[1] == keyTagUID[1] && rfid.uid.uidByte[2] == keyTagUID[2] && rfid.uid.uidByte[3] == keyTagUID[3] ) { Serial.println("Access is granted"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.print("Access denied, 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 components following the wiring diagram shown 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 Arduino IDE.
  7. Type "MFRC522" in the search box, then look for the library by GithubCommunity.
  8. Click Install 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 Upload 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 or NFC tag that you want to authorize on the RC522 reader and note the UID printed in the Serial Monitor.
∞
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:14:22] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:14:35] Access denied, UID: 2B B8 59 B1
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Update the UID on line 18 of the above code. For example, change byte keytagUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; to byte keytagUID[4] = {0x2B, 0xB8, 0x59, 0xB1};
  2. Upload the updated code to the ESP32 S3 again.
  3. Tap the authorized RFID/NFC tag on the RC522 reader and check the Serial Monitor.
∞
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:15:01] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:15:10] Access is granted
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Verify that the electromagnetic or solenoid lock deactivates (unlocks) when access is granted.
  2. Tap a different (unauthorized) tag and confirm the lock stays engaged.
∞
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:15:28] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:15:35] Access denied, UID: 1C 2A 52 E3
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Pro Tip: For easier testing the unlock duration is set to 2 seconds in the code — increase this value before deploying the system in a real installation.

ESP32 S3 Code - Multiple Keys

The following code extends the single-key example to support multiple authorized users, each identified by a unique UID and a human-readable name stored in the firmware. When a matching tag is tapped, the ESP32 S3 prints the user's name to the Serial Monitor and unlocks the door; any unrecognized tag is denied and its UID is logged.

/* * 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-door-lock-system */ /* * 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-door-lock-system */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 // ESP32-S3 pin GPIO10 #define RST_PIN 9 // ESP32-S3 pin GPIO9 #define RELAY_PIN 7 // ESP32-S3 pin GPIO7 connects to relay MFRC522 rfid(SS_PIN, RST_PIN); byte managerKeyUID[4] = {0x3A, 0xC9, 0x6A, 0xCB}; byte secretaryKeyUID[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, HIGH); // lock the door 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] == managerKeyUID[0] && rfid.uid.uidByte[1] == managerKeyUID[1] && rfid.uid.uidByte[2] == managerKeyUID[2] && rfid.uid.uidByte[3] == managerKeyUID[3] ) { Serial.println("The access is granted to manager"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else if (rfid.uid.uidByte[0] == secretaryKeyUID[0] && rfid.uid.uidByte[1] == secretaryKeyUID[1] && rfid.uid.uidByte[2] == secretaryKeyUID[2] && rfid.uid.uidByte[3] == secretaryKeyUID[3] ) { Serial.println("The access is granted to secretary"); digitalWrite(RELAY_PIN, LOW); // unlock the door for 2 seconds delay(2000); digitalWrite(RELAY_PIN, HIGH); // lock the door } else { Serial.print("Access denied, 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. Follow the same library installation and wiring steps described in the single-key section above.
  3. Discover the UID of each tag you want to authorize by running the single-key code first (see step 12 above).
  4. Update the two authorized UIDs in the code with the UIDs of your manager and secretary tags.
  5. Upload the updated code to the ESP32 S3.
  6. Tap the manager tag on the RC522 reader, then tap the secretary tag, and observe the Serial Monitor.
∞
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:20:05] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:20:12] The access is granted to manager [2026-06-16 09:20:20] Tap an RFID/NFC tag on the RFID-RC522 reader [2026-06-16 09:20:28] The access is granted to secretary
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Pro Tip: You can add three, four, or more authorized tags by extending the UID array in the code — each additional entry requires one more UID and one more name string.

※ NOTE THAT:

In this tutorial the authorized UIDs are compiled into the firmware. For a production system you would typically store UIDs in non-volatile memory (such as EEPROM or flash) so they can be added or removed at runtime without reflashing. If you need help implementing that feature, feel free to contact us through our coding service.

Applications and Project Ideas

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

  1. Office door lock: Assign one RFID card per employee and restrict entry to authorized staff only, with access attempts logged to the Serial Monitor.
  2. Home smart lock: Replace a traditional key with an RFID card or key fob that only family members carry.
  3. Server rack security: Protect sensitive IT equipment inside a locked cabinet that only opens for authorized service technicians.
  4. Locker room system: Give each locker its own RFID-controlled solenoid bolt driven by a dedicated ESP32 S3 for stand-alone operation.
  5. Shared tutorial access: Grant RFID access to registered members and automatically log every entry for audit purposes.
  6. Prototype security lab: Build a test rig that logs every access attempt with a timestamp to an SD card or cloud database.
  7. Parking gate controller: Replace key-card readers at parking barriers with an RC522 and ESP32 S3 to authorize vehicles by their windshield RFID stickers.

Video Tutorial

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

Challenge Yourself

Now that your ESP32 S3 is successfully controlling a door lock with RFID cards, push the project further with these progressively harder challenges:

  1. Beginner: Add a green LED that lights up for 2 seconds when access is granted and a red LED that flashes when access is denied.
  2. Beginner: Connect a passive buzzer and play a short beep on access granted and a longer double-beep on access denied.
  3. Intermediate: Implement a lockout that disables the RFID reader for 30 seconds after three consecutive unauthorized tag attempts.
  4. Intermediate: Store authorized UIDs in EEPROM so they persist across power cycles and can be updated without reflashing the ESP32 S3.
  5. Advanced: Add a web interface over the ESP32 S3's Wi-Fi so an administrator can add or revoke authorized UIDs from a browser without touching the hardware.
  6. Advanced: Combine the RC522 reader with a keypad so the door only opens when both a valid RFID tag and the correct 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!