NFC tags to trigger home scenes
Build it step by step
0 of 8 doneSet the board to I2C
Set the two switches as shown in its documentation.
Wire the module
Connect I2C, IRQ and reset.
Read a tag
Upload with an empty list and note the ID.
Fill in the scenes
Add each ID and a scene name.
Fill in the placeholders
Add Wi-Fi and broker details.
Check the messages
Subscribe to home/scene and tap each tag.
Connect it to something
Make a scene switch a light or fan.
Stick the tags up
Place them beside the bed and by the door.
How do you build an NFC tags to trigger home scenes?
Tap an NFC sticker by the bed or door to run a scene such as lights off or fan on. You need PN532 NFC module, RFID tags 13.56 MHz (MIFARE Classic 1K) × 3 and ESP32 DevKit V1. It takes 8 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 3
Parts list
- WirelessBuyPN532 NFC module × 1
- WirelessBuy
- Development BoardsBuyESP32 DevKit V1 × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| PN532 VCC | ESP32 3V3 | Power |
| PN532 GND | ESP32 GND | Ground |
| PN532 SDA | ESP32 GPIO21 | I2C data, set the module switches to I2C |
| PN532 SCL | ESP32 GPIO22 | I2C clock |
| PN532 IRQ | ESP32 GPIO4 | Interrupt line |
| PN532 RSTO | ESP32 GPIO5 | Reset line |
Code
Each NFC tag has a unique ID, so the sketch simply looks the ID up in a list and publishes the scene name to an MQTT topic that your home system (Home Assistant, Node-RED or your own relay controller) can listen to. First run it with no tags in the list and write down each tag's ID from the serial monitor, then fill in the table. Mifare Classic tags are fine for convenience triggers, but their IDs can be copied, so never use them to unlock a door or for payments. Set the switches on the PN532 board to I2C mode. Use placeholders for your Wi-Fi and broker details and keep a password on the broker.
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).
// Libraries: Adafruit PN532, PubSubClient. Reads a tag's ID and publishes the matching scene over MQTT.
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_PN532.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* MQTT_HOST = "YOUR_BROKER_ADDRESS";
Adafruit_PN532 nfc(4, 5); // IRQ, RESET
WiFiClient net;
PubSubClient mqtt(net);
struct Scene { const char* uid; const char* name; };
const Scene SCENES[] = { // replace with your own tags' IDs, printed by this sketch
{"04A1B2C3", "bedtime"},
{"04D4E5F6", "leaving"},
{"0411AA22", "fan_on"},
};
String uidToString(uint8_t *u, uint8_t len) {
String s = "";
for (uint8_t i = 0; i < len; i++) { if (u[i] < 16) s += "0"; s += String(u[i], HEX); }
s.toUpperCase();
return s;
}
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
nfc.begin();
if (!nfc.getFirmwareVersion()) { Serial.println("PN532 not found"); while (1) delay(1000); }
nfc.SAMConfig();
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
mqtt.setServer(MQTT_HOST, 1883);
}
void loop() {
if (!mqtt.connected()) mqtt.connect("nfc-reader");
mqtt.loop();
uint8_t uid[7], len;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &len, 200)) {
String id = uidToString(uid, len);
Serial.println("Tag " + id); // use this to learn each tag's ID
for (auto &s : SCENES) {
if (id == s.uid) { mqtt.publish("home/scene", s.name); Serial.println(String("Scene ") + s.name); }
}
delay(1500); // avoid repeats while the tag is still in range
}
}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 |
|---|---|
| Unknown tag | ID printed, no scene |
| Known tag | Scene name published |
| Tag held on the reader | One message, not many |
| Wi-Fi down | Tags still read, messages resume when back |
If it does not work
PN532 not found
Check the I2C mode switches and wiring.
No tags read
Hold the tag flat against the module.
Scene not sent
Check the ID in the list matches the printed one in capitals.
MQTT silent
Check the broker address.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the NFC tags to trigger home scenes?
PN532 NFC module, RFID tags 13.56 MHz (MIFARE Classic 1K) × 3 and ESP32 DevKit V1.
How long does the NFC tags to trigger home scenes take to build?
About 60 minutes for a beginner maker, in 8 steps.
How do I know the NFC tags to trigger home scenes works?
Unknown tag: ID printed, no scene. Known tag: Scene name published. Tag held on the reader: One message, not many.
PN532 not found: what should I check?
Check the I2C mode switches and wiring.
No tags read: what should I check?
Hold the tag flat against the module.
Scene not sent: what should I check?
Check the ID in the list matches the printed one in capitals.
MQTT silent: what should I check?
Check the broker address.
