ESP32 PN532 NFC Module
The PN532 NFC module provides a powerful and flexible platform for integrating NFC and RFID capabilities into your projects. Its multi-protocol support and versatile interfaces make it suitable for various use cases, including access control, contactless payment, and data exchange.
Jump to Code Examples
Quick Links
PN532 Price
About PN532 NFC Module
The PN532 is a powerful NFC module widely used in proximity RFID and NFC applications. It supports communication in various modes, including I2C, SPI, and UART, making it highly versatile for different projects. With compatibility for both ISO/IEC 14443 Type A and B cards, as well as NFC peer-to-peer communication, the PN532 module is ideal for access control, contactless payment systems, and data exchange.PN532 Sensor Technical Specifications
Below you can see the PN532 NFC Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C/SPI/UART
- Communication Protocols: I2C, SPI, UART
- Operating Voltage: 3.3V or 5V
- Supported Standards: ISO/IEC 14443 Type A & B, NFC
- Operating Temperature: -25°C to +85°C
- Dimensions: 40mm x 40mm x 4mm
PN532 Sensor Pinout
Below you can see the pinout for the PN532 NFC Module. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The PN532 pinout includes:
- VCC: Power supply input (3.3V or 5V).
- GND: Ground connection.
- SDA: I2C data line.
- SCL: I2C clock line.
- MISO: SPI Master In Slave Out.
- MOSI: SPI Master Out Slave In.
- SCK: SPI clock line.
- SS: SPI slave select.
- RXD: UART receive line.
- TXD: UART transmit line.
- IRQ: Interrupt signal output.
PN532 Wiring with ESP32
Below you can see the wiring for the PN532 NFC Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
- Connect
VCC
to a stable power supply (3.3V or 5V). - Connect
GND
to the system ground. - For I2C: Connect
SDA
to the microcontroller's SDA pin andSCL
to the SCL pin. - For SPI: Connect
MOSI
,MISO
,SCK
, andSS
to corresponding SPI pins on the microcontroller. - For UART: Connect
RXD
to the microcontroller's TX pin andTXD
to the RX pin. - Optionally, connect the
IRQ
pin for interrupt-driven applications.
Code Examples
Below you can find code examples of PN532 NFC Module with ESP32 in several frameworks:
If you encounter issues while using the PN532 NFC Module, check the Common Issues Troubleshooting Guide.
ESP32 PN532 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the PN532 NFC Module:
#include <Wire.h>
#include <Adafruit_PN532.h>
#define SDA_PIN 21
#define SCL_PIN 22
Adafruit_PN532 nfc(SDA_PIN, SCL_PIN);
void setup() {
Serial.begin(115200);
Serial.println("Initializing PN532...");
nfc.begin();
uint32_t version = nfc.getFirmwareVersion();
if (!version) {
Serial.println("Didn't find PN532 board");
while (1);
}
nfc.SAMConfig();
Serial.println("PN532 initialized!");
}
void loop() {
Serial.println("Waiting for NFC tag...");
uint8_t success;
uint8_t uid[] = {0};
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
Serial.print("Found NFC tag with UID: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(uid[i], HEX);
Serial.print(" ");
}
Serial.println();
}
delay(1000);
}
This Arduino sketch interfaces with the PN532 NFC module over I2C using the Adafruit PN532 library. The `setup` function initializes the module, and the firmware version is retrieved for verification. In the `loop`, the module continuously scans for NFC tags and prints the UID of any detected tag. Additional features, such as writing to tags or handling other NFC modes, can be implemented as needed.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 PN532 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the PN532 NFC Module, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#include "pn532.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_FREQ_HZ 100000
void app_main() {
ESP_LOGI("PN532", "Initializing I2C");
i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ
};
i2c_param_config(I2C_NUM_0, &i2c_conf);
i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, 0);
PN532 pn532(I2C_NUM_0);
if (!pn532.begin()) {
ESP_LOGE("PN532", "Failed to initialize PN532 module");
return;
}
ESP_LOGI("PN532", "Waiting for NFC tag...");
while (1) {
uint8_t uid[7];
uint8_t uid_length;
if (pn532.readPassiveTargetID(uid, &uid_length)) {
ESP_LOGI("PN532", "Found NFC tag with UID: ");
for (int i = 0; i < uid_length; i++) {
printf("%02X ", uid[i]);
}
printf("\n");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This ESP-IDF code initializes the PN532 NFC module over I2C. The module scans for NFC tags and logs their UIDs. The code configures the I2C master interface, connects to the PN532, and uses a continuous loop to detect tags. Additional functionalities, such as tag writing, can be implemented using the PN532 driver.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 PN532 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the PN532 NFC Module
uart:
tx_pin: GPIO17
rx_pin: GPIO16
baud_rate: 115200
pn532:
update_interval: 2s
sensor:
- platform: pn532
uid:
name: "NFC Tag UID"
log:
name: "NFC Log"
This ESPHome configuration sets up the PN532 NFC module. The module is updated every 2 seconds to scan for NFC tags. Detected tag UIDs are logged and available as a sensor output. The configuration can be extended for writing to tags or using NFC data in automations.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 PN532 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:pn532]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
ESP32 PN532 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the PN532 NFC Module:
#include <Wire.h>
#include <Adafruit_PN532.h>
Adafruit_PN532 nfc(21, 22);
void setup() {
Serial.begin(115200);
nfc.begin();
if (!nfc.getFirmwareVersion()) {
Serial.println("Failed to find PN532!");
while (1);
}
nfc.SAMConfig();
Serial.println("Waiting for NFC tag...");
}
void loop() {
uint8_t uid[7];
uint8_t uidLength;
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) {
Serial.print("Found NFC tag with UID: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(uid[i], HEX);
Serial.print(" ");
}
Serial.println();
}
delay(1000);
}
This PlatformIO example uses the Adafruit PN532 library to interface with the PN532 module over I2C. It initializes the module, scans for NFC tags, and logs their UIDs to the Serial Monitor. The code can be extended to include additional NFC features like tag writing or peer-to-peer communication.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 PN532 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the PN532 NFC Module with your ESP32.
from machine import I2C, Pin
import time
from pn532_i2c import PN532_I2C
# Initialize I2C
i2c = I2C(1, scl=Pin(22), sda=Pin(21))
pn532 = PN532_I2C(i2c, debug=False)
pn532.SAM_configuration()
while True:
print("Waiting for NFC tag...")
uid = pn532.read_passive_target()
if uid:
print("Found NFC tag with UID:", [hex(i) for i in uid])
time.sleep(1)
This MicroPython code communicates with the PN532 module over I2C. The PN532_I2C library initializes the module and enables scanning for NFC tags. When a tag is detected, its UID is printed. Additional NFC functionalities like writing or peer-to-peer communication can be implemented.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
PN532 NFC Module Troubleshooting
This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.
Module Fails to Power On
Issue: The PN532 module does not power up or respond to commands.
Possible causes include insufficient power supply, incorrect wiring, or faulty hardware.
Solution: Ensure the module is connected to a stable power source within the recommended voltage range of 3.3V to 5V. Verify that all connections are secure and correctly configured. If the problem persists, consider testing the module with a different power source or replacing it.
Communication Interface Not Working
Issue: The module fails to communicate with the microcontroller over the chosen interface (I2C, SPI, or UART).
Possible causes include incorrect wiring, improper interface selection, or incompatible voltage levels.
Solution: Double-check the wiring to ensure correct connections for the chosen interface. For I2C, ensure that the SDA and SCL lines are properly connected, and for SPI, verify the MOSI, MISO, SCK, and SS lines. If using I2C, ensure that pull-up resistors are present on the SDA and SCL lines.
Unable to Read Tags
Issue: The module initializes correctly but fails to read NFC or RFID tags.
Possible causes include incorrect antenna orientation, insufficient power supply, or interference from nearby electronic devices.
Solution: Ensure the module's antenna is properly oriented and positioned near the tags. Verify that the power supply provides adequate current for the module's operation. Keep the module away from sources of electromagnetic interference.
Inconsistent Tag Detection
Issue: The module detects tags intermittently or with delays.
Possible causes include low-quality tags, environmental interference, or firmware issues.
Solution: Test with different tags to rule out tag quality issues. Ensure the operating environment is free from strong electromagnetic interference. Update the module's firmware to the latest version to benefit from bug fixes and improvements.
Library or Software Issues
Issue: The module operates erratically or produces errors during operation.
Possible causes include outdated or incompatible libraries, incorrect initialization, or software bugs.
Solution: Ensure that the latest version of the PN532 library is installed and compatible with your development environment. Review the initialization code to confirm that the module is set up correctly. Consult the module's documentation and community forums for guidance on proper usage.
Conclusion
We went through technical specifications of PN532 NFC Module, its pinout, connection with ESP32 and PN532 NFC Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.