readme.md
Markdown · 171 lines
# PN532 NFC Reader (SPI) – ESP32
### Read NFC / RFID tags via PN532 (13.56 MHz) using SPI and display data in Serial Monitor
This project demonstrates how to use an **ESP32** with an **PN532 NFC reader** in **SPI mode** to detect NFC / RFID tags and print their **UID data** to the Serial Monitor.
The PN532 communicates with the ESP32 using **MOSI, MISO, SCK and CS** pins (SPI), not I2C.
The sketch initializes the PN532, verifies the firmware version, and continuously waits for NFC tags in range.
---
## ⭐ Features
* 📡 **PN532 NFC reader (13.56 MHz)**
* 🔁 **SPI communication** (MOSI / MISO / SCK / CS)
* 🆔 **Reads NFC tag UID**
* MIFARE Classic
* MIFARE Ultralight
* NTAG / ISO14443A
* 🖥 **Serial Monitor output**
* ⚡ Works with **ESP32 Dev Module**
* 🧪 Firmware detection for easy debugging
---
## 📦 Hardware Requirements
* **ESP32** (ESP32 Dev Module / ESP32-WROOM)
* **PN532 NFC reader module** (SPI-capable)
* NFC cards or tags (13.56 MHz)
* Jumper wires
---
## 🔌 Wiring (SPI)
| PN532 | ESP32 |
| ------- | ------ |
| VCC | 3.3V |
| GND | GND |
| SCK | GPIO18 |
| MISO | GPIO19 |
| MOSI | GPIO23 |
| SS / CS | GPIO5 |
> ⚠️ **Important**
> PN532 must be powered with **3.3V** when used with ESP32.
---
## 🔧 PN532 Interface Selection
The PN532 supports multiple communication modes:
* I2C
* SPI
* HSU (UART)
### ✅ SPI Mode (required)
Most PN532 modules use **DIP switches or solder jumpers** to select the interface.
Typical configuration for SPI:
```
SW1 = OFF
SW2 = ON
```
* Switch **UP** → `ON`
* Switch **DOWN** → `OFF`
> After changing switches, **disconnect and reconnect power** to the PN532.
---
## 🔧 Software Requirements
* **Arduino IDE**
* **ESP32 board package**
```
Boards Manager → esp32 by Espressif Systems
```
### Required Libraries
Install via **Arduino Library Manager**:
* `Adafruit PN532`
Core libraries used:
* `SPI`
* `WiFi` (ESP32 core dependency)
---
## 🚀 Getting Started
1. Open the sketch in **Arduino IDE**
2. Select your board:
```
ESP32 Dev Module
```
3. Select the correct **COM port**
4. Upload the sketch
5. Open **Serial Monitor**
6. Set baud rate to:
```
115200
```
---
## 📶 Usage
1. Power the ESP32 and PN532
2. Open the Serial Monitor
3. Bring an NFC card or tag close to the reader
4. The UID will be printed:
```
NFC card detected!
UID Length: 4 bytes
UID Value: 04 A1 B2 C3
```
---
## 🧪 Debugging Tips
* If nothing appears in Serial Monitor:
* Check baud rate (115200)
* Check COM port
* Verify PN532 is in **SPI mode**
* Ensure **CS pin is HIGH before initialization**
* Confirm **3.3V power**, not 5V
* If firmware version is not detected:
* Recheck DIP switch orientation
* Shorten SPI wires
* Power-cycle PN532
---
## 🛠 Customization Ideas
You can extend this project by adding:
* 📖 Reading MIFARE Classic blocks
* ✍️ Writing data to NFC tags
* 🧾 JSON output over Serial
* 🌐 Sending NFC data over WiFi / MQTT
* 🔘 Button-triggered scans
* 🔒 Access control systems
---
## 📄 License
This project is released under the **MIT License**.
You are free to use, modify, and distribute it for personal and commercial projects.