Build it step by step
0 of 7 doneTest the reader
Print a tag number.
Test the lock
MOSFET and diode.
Choose a master tag
Copy its number into the sketch.
Add tags
Master tap, then new tag.
Open with a listed tag
Lock opens for 5 s.
Remove a tag
Master tap, then the tag again.
Add a mechanical override
Key or release.
How do you build an NFC tag door lock?
Tap a phone or tag to open a lock, with a list kept on the device. You need ESP32 DevKit V1, PN532 NFC module, Solenoid lock 12 V, MOSFET IRLZ44N, Rectifier Diode 1N4007, 12 V 2 A DC adapter and Active buzzer 5 V. It takes 7 steps and about 180 minutes.
- Level
- Intermediate
- Time
- About 180 min
- Parts
- 7
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- WirelessBuyPN532 NFC module × 1
- Motors & ActuatorsBuy
- SemiconductorsBuyMOSFET IRLZ44N × 1
- SemiconductorsBuy
- PowerBuy
- Motors & ActuatorsBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| PN532 SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C (set the module's switches to I2C) |
| PN532 VCC and GND | ESP32 3V3 and GND | Power |
| Solenoid lock plus | 12 V adapter plus | Lock power |
| Solenoid lock minus | MOSFET drain | Switched side |
| 1N4007 diode | Across the solenoid, stripe to plus | Absorbs the spike |
| MOSFET gate | ESP32 GPIO26 | Control (add 10 k from gate to GND) |
| MOSFET source and 12 V adapter minus | ESP32 GND | Common ground |
| Buzzer plus and minus | ESP32 GPIO27 and GND | Beep |
| ESP32 VIN | 5 V from a separate supply | Controller power |
Code
A tag or an NFC phone sends a short number when tapped, and the lock opens if that number is on the list. The list lives in the ESP32's own memory, so it survives a power cut, and a master tag lets you add or remove tags without a computer: tap the master, then the tag. The solenoid is switched by a MOSFET with a diode across it to absorb the spike, and the lock is only on for five seconds so it does not overheat. Be realistic about security: the number on a simple tag is not secret and can be copied, so this suits a cupboard, a workshop drawer or a hobby box, not a door that protects your home. Always keep a mechanical way to open it, since a power cut or a fault must never lock someone in or out of a place they need. Use a fail-secure lock only where that is acceptable, and never on an exit route. Keep the master tag somewhere safe. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// NFC tag lock: opens a solenoid lock for 5 seconds when a known tag is tapped. Tap the master tag, then another tag, to add or remove it.
// Libraries: Adafruit PN532, Preferences (built in). Board: ESP32 Dev Module.
// Set MASTER to your master tag's number (the sketch prints every tapped tag over Serial first).
#include <Wire.h>
#include <Adafruit_PN532.h>
#include <Preferences.h>
Adafruit_PN532 nfc(-1, -1); // I2C, no IRQ or reset pin
Preferences prefs;
const uint32_t MASTER = 0x00000000; // replace with the master tag's value from the Serial Monitor
const byte LOCK = 26, BUZZER = 27;
const int MAXTAGS = 10;
uint32_t tags[MAXTAGS]; int nTags = 0;
void loadTags() { prefs.begin("tags", true); nTags = prefs.getInt("n", 0); for (int i = 0; i < nTags; i++) tags[i] = prefs.getUInt(String("t" + String(i)).c_str(), 0); prefs.end(); }
void saveTags() { prefs.begin("tags", false); prefs.putInt("n", nTags); for (int i = 0; i < nTags; i++) prefs.putUInt(String("t" + String(i)).c_str(), tags[i]); prefs.end(); }
int findTag(uint32_t id) { for (int i = 0; i < nTags; i++) if (tags[i] == id) return i; return -1; }
void beep(int ms, int f) { tone(BUZZER, f, ms); delay(ms + 40); }
void openLock() { digitalWrite(LOCK, HIGH); beep(150, 1800); delay(4850); digitalWrite(LOCK, LOW); }
void setup() {
Serial.begin(115200);
pinMode(LOCK, OUTPUT); digitalWrite(LOCK, LOW); pinMode(BUZZER, OUTPUT);
nfc.begin();
if (!nfc.getFirmwareVersion()) { Serial.println("PN532 not found"); while (true) delay(1000); }
nfc.SAMConfig();
loadTags();
}
bool adding = false; unsigned long addUntil = 0;
void loop() {
uint8_t uid[7]; uint8_t len;
if (!nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len, 200)) return;
uint32_t id = 0; for (uint8_t i = 0; i < len && i < 4; i++) id = (id << 8) | uid[i]; // first four bytes
Serial.print("Tag 0x"); Serial.println(id, HEX);
if (id == MASTER && MASTER != 0) { adding = true; addUntil = millis() + 10000; beep(100, 1000); beep(100, 1400); }
else if (adding && millis() < addUntil) {
int i = findTag(id);
if (i >= 0) { tags[i] = tags[--nTags]; beep(300, 500); } // already known: remove
else if (nTags < MAXTAGS) { tags[nTags++] = id; beep(100, 2000); } // new: add
saveTags(); adding = false;
}
else if (findTag(id) >= 0) openLock();
else beep(400, 300);
delay(800);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
PN532 not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Listed tag | Lock opens for 5 s |
| Unknown tag | Low beep, lock stays shut |
| Master then new tag | Added |
| Power off and on | List is kept |
If it does not work
Reader not found
Check the I2C switches and wiring.
Lock does not move
Check the 12 V supply and the diode direction.
Adds the wrong tag
Tap the master first and wait for the double beep.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the NFC tag door lock?
ESP32 DevKit V1, PN532 NFC module, Solenoid lock 12 V, MOSFET IRLZ44N, Rectifier Diode 1N4007, 12 V 2 A DC adapter and Active buzzer 5 V.
How long does the NFC tag door lock take to build?
About 180 minutes for a intermediate maker, in 7 steps.
How do I know the NFC tag door lock works?
Listed tag: Lock opens for 5 s. Unknown tag: Low beep, lock stays shut. Master then new tag: Added.
Reader not found: what should I check?
Check the I2C switches and wiring.
Lock does not move: what should I check?
Check the 12 V supply and the diode direction.
Adds the wrong tag: what should I check?
Tap the master first and wait for the double beep.
