A robust solution for reading NFC cards (ISO14443A) using the PN5180 module, specifically addressing a critical bug in standard libraries that causes duplicated UID readings.
This implementation ensures that each NFC card always returns its correct, unique UID.
When using standard PN5180 libraries, users often encounter critical issues:
-
Different cards showing the same UID
Multiple distinct cards return identical UID values -
Same card showing different UIDs
A single card returns varying UIDs on consecutive reads -
Invalid UIDs
Readings of all zeros (0x00) or all ones (0xFF) that do not correspond to real cards
The core problem lies in the standard PN5180 library’s insufficient handling of internal buffers and validation:
-
Insufficient FIFO buffer clearing
readCardSerial()does not properly reset the PN5180 internal FIFO before new reads -
Missing data validation
No validation of ATQA and SAK responses before processing UID data -
Incomplete error checking
Manufacturer code and UID structure are not verified, allowing corrupted data
Instead of relying on the flawed readCardSerial() function, this project uses a direct approach based on activateTypeA() with strict validation.
- Direct FIFO buffer management
- Comprehensive ATQA and SAK validation
- UID structure and manufacturer code verification
- Enhanced error detection with explicit status handling
- Periodic hardware reset to ensure a clean PN5180 state
- PN5180-NFC-Reader/
- ├── src/
- │ └── PN5180_reader.ino # Main application
- ├── inc/
- │ └── PN5180/ # PN5180 library
- └── README.md # This file
- PN5180 NFC Reader Module
- ESP32 Development Board (or Arduino with sufficient GPIOs)
- ISO14443A NFC Cards/Tags, including:
- MIFARE Classic
- MIFARE Ultralight
- NTAG series
| PN5180 Pin | ESP32 Pin |
|---|---|
| NSS | GPIO 16 |
| BUSY | GPIO 5 |
| RST | GPIO 17 |
| SCK | GPIO 18 |
| MOSI | GPIO 23 |
| MISO | GPIO 19 |
| VCC | 3.3V |
| GND | GND |
⚠ Note:
Never swap NSS (GPIO 16) and BUSY (GPIO 5) – this will cause PN5180 communication failure.
- Install Arduino IDE 1.8.x or later
- Install ESP32 Board Support Package
-
Install PN5180 library by tueddy
🔗 https://github.com/tueddy/PN5180-Library -
Add library via: Sketch → Include Library → Add .ZIP Library
- Clone or download this repository
- Open
PN5180_reader.inoin Arduino IDE - Select:
- Board: ESP32 Dev Module
- Correct COM Port
- Upload the sketch
Contributions are welcome!
Please open an issue or submit a pull request.
