Skip to content

PN532 NFC reader~

This feature is included only in tasmota-sensors and tasmota32 binaries

When compiling your build add the following to user_config_override.h:

#ifndef USE_PN532_HSU 
#define USE_PN532_HSU                            // Add support for PN532 using HSU (Serial) interface (+1k8 code, 140 bytes mem)
// Optional defines, uncomment (remove //) as needed
//  #define USE_PN532_DATA_FUNCTION                // Add sensor40 command support for erase, setting data block content (+1k7 code, 388 bytes mem)
#endif

The PN532 is a highly integrated transceiver module for contactless communication at 13.56 MHz based on the 80C51 microcontroller core.

The datasheet for the PN532 chip is available here.

Please note that although the datasheet mentions that the PN532 can be used on SPI, I2C and HSUART that only the HSU interface is implemented in the Tasmota driver.

Configuration~

#define USE_PN532_DATA_FUNCTION
This function is experimental. There are limitations because it seems not all cards are supported by this driver and/or the PN532 module. See issue 4941 for more information. We are still researching the

Wiring~

As mentioned earlier the PN532 breakout boards usually have pins broken out for all three protocols supported by the PN532 but we are only interested in the HSU interface as that is all the driver currently supports.

For this reason breakout boards have either micro dip switches as shown in the image below, or they have pads on the PC board which you need to bridge out with solder to select which mode the PN532 will operate in.

pn532_nfc_hsu_config

After selecting the correct protocol mode and connecting the HSU TX/RX pins of the PN532 to the pins you configured on your ESP8266 board you can power it up and the PN532 should be detected automatically.

PN532 ESP
GND GND
VCC 3.3V
SDA GPIOx
SCL GPIOy

Tasmota Settings~

In the Configuration -> Configure Module page assign:

  • GPIOx to PN532 Rx (139)
  • GPIOy to PN532 Tx (138)

The module will reboot when you save this configuration. During start-up the following information should be visible in your console output:

00:00:00 NFC: PN532 NFC Reader detected V1.6
If the device was not found please check your wiring and configuration and confirm that everything is as it should be.

Example

Configured using Wemos D1 mini on pins: D1 (connected to PN532 SCL) and D2 (connected to PN532 SDA)

PN532 Software Configuration

Usage~

Tasmota will scan for a new card detect 4 times per second and if found will report it via immediate telemetry.

The output on the console will look similar to the below when a new card is detected

18:23:24 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:23:24","PN532":{"UID":"94D8FC5F", "DATA":""}}
18:23:24 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:23:25 MQT: stat/tasmota/RESULT = {"Event":"Done"}

The UID of the card/tag is reported and any text stored in BLOCK 1 of a Mifare Classic card or PAGE4-7 of a NTAG card (up to 16 characters in length) is reported in the DATA field of the JSON sent via telemetry. Please note that the DATA field field can contain only printable chars.

The content of the DATA on BLOCK 1 of a Mifare Classic (PAGE4-7 of a NTAG) card can be set as follows

Sensor40 WRITE,I LOVE TASMOTA

Once executed the very next card/tag that is presented to the reader will be programmed accordingly and the data will be retained on the card/tag until either changed or erased.

To erase the content of the DATA field the following command may be used

Sensor40 ERASE

Once executed the very next card/tag that is presented to the reader will have its BLOCK 1 (PAGE4-7 of a NTAG) erased.

For canceling previous command he following command may be used

Sensor40 CANCEL

Once executed will stop waiting next card/tag.

For NTAG213/215/216, NT3H2111/2211 or compatible are additionally available commands: AUTH, SET_PWD, UNSET_PWD for password operations.

Warning. The password is a unsigned 32 bit and acknowledge is a unsigned 16 bit values.

Sensor40 AUTH,password,ack

Set tasmota password phrase and password acknowledge for authentication.

Sensor40 SET_PWD or Sensor40 SET_PWD,new_password or Sensor40 SET_PWD,new_password,new_ack

Write current password and ack / new password and current ack / new password and new acknowledge to a NTAG21x tag.

Sensor40 UNSET_PWD

Clear password and ack in a NTAG21x tag.

Text logging of the above two actions are also presented during the process for information purposes.

Using the UID and DATA of a presented card~

When a card is presented to the PN532 under normal operating conditions up to 3 ways of using the data is possible.

The first is the immediate telemetry generated which looks as follows. For not NTAG21x:

18:31:39 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:31:39","PN532":{"UID":"94D8FC5F", "DATA":"ILOVETASMOTA"}}

For NTAG21x and correct password and acknowledge:

20:05:48 MQT: tele/tasmota/SENSOR = {"Time":"2023-02-24T20:05:48","PN532":{"UID":"53BB1F05110001","Data":"I love tasmota","Auth":"Ok"}}

For NTAG21x and correct password and not correct acknowledge:

20:05:48 MQT: tele/tasmota/SENSOR = {"Time":"2023-02-24T20:05:48","PN532":{"UID":"53BB1F05110001","Data":"I love tasmota","Auth":"NOk"}}

For NTAG21x and not password protection in a tag:

20:05:48 MQT: tele/tasmota/SENSOR = {"Time":"2023-02-24T20:05:48","PN532":{"UID":"53BB1F05110001","Data":"I love tasmota","Auth":"None"}}

Since this is an immediate telemetry generation as opposed to the sensor data you would normally be expected to be presented when the telemetry period occurs, this telemetry data is not directly usable on the device itself. It is generated and immediately transmitted over MQTT and the purpose of this is so that immediate action may be taken by any home automation software you are using with the data obtained from the card/tag as opposed to waiting for the telemetry period to expire and be sent with normal telemetry data.

For the purpose of using card/tag data on the device itself you will need to use rules along with the events that are caused.

Example

Example rule for responding to a specific UID on the device when a card/tag matching a specific UID is presented

rule1 on PN532#UID=94D8FC5F do power on endon

Example

Example rule for responding to a specific DATA content that was previously programmed to one or more cards using the Sensor40 write,xxxx command

Rule 1on EVENT#PN532_DATA=ILOVETASMOTA do power on endon
18:41:12 MQT: tele/tasmota/SENSOR = {"Time":"2019-01-10T18:41:12","PN532":{"UID":"94D8FC5F", "DATA":"ILOVETASMOTA"}}
18:41:13 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:41:13 MQT: stat/tasmota/RESULT = {"Event":"Done"}
18:41:13 RUL: EVENT#PN532_DATA=ILOVETASMOTA performs "power on"
18:41:13 MQT: stat/tasmota/RESULT = {"POWER":"ON"}
18:41:13 MQT: stat/tasmota/POWER = ON

Breakout Boards~

Since the PN532 chip itself is surface mount and requires some external components to operate the best is probably to obtain a breakout board similar to the one below from your favourite online supplier.

PN532 Breakout Board Module

Please make sure the breakout board you order has the HSU pins (SCL=TX and SDA=RX) broken out as it will be very difficult to add them manually - Usually they have them but for good measure just make sure.