Projects / NFC tags to trigger home scenes

NFC tags to trigger home scenes

Beginner1 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP32

    Set the board to I2C

    Set the two switches as shown in its documentation.

  2. ESP32

    Wire the module

    Connect I2C, IRQ and reset.

  3. ESP32

    Read a tag

    Upload with an empty list and note the ID.

  4. ESP32

    Fill in the scenes

    Add each ID and a scene name.

  5. ESP32

    Fill in the placeholders

    Add Wi-Fi and broker details.

  6. ESP32

    Check the messages

    Subscribe to home/scene and tap each tag.

  7. ESP32

    Connect it to something

    Make a scene switch a light or fan.

  8. ESP32RFID

    Stick the tags up

    Place them beside the bed and by the door.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
PN532 VCCESP32 3V3Power
PN532 GNDESP32 GNDGround
PN532 SDAESP32 GPIO21I2C data, set the module switches to I2C
PN532 SCLESP32 GPIO22I2C clock
PN532 IRQESP32 GPIO4Interrupt line
PN532 RSTOESP32 GPIO5Reset 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.

CheckExpected
Unknown tagID printed, no scene
Known tagScene name published
Tag held on the readerOne message, not many
Wi-Fi downTags 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 →

Ask Lark AI

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.