The NFC/RFID Reader PN532 is a versatile and user-friendly module capable of reading Near Field Communication (NFC) tags and Radio-Frequency Identification (RFID) cards. Manufactured by Arduino, this module is based on the PN532 v.3 chip and is widely used in applications such as access control, contactless payment systems, and data exchange between devices. Its popularity stems from its ease of use, reliability, and compatibility with a wide range of microcontrollers, including the Arduino UNO.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (3.3V to 5.5V) |
2 | GND | Ground |
3 | SDA | Serial Data (I2C interface) |
4 | SCL | Serial Clock (I2C interface) |
5 | MISO | Master In Slave Out (SPI interface) |
6 | MOSI | Master Out Slave In (SPI interface) |
7 | SCK | Serial Clock (SPI interface) |
8 | SS | Slave Select (SPI interface) |
9 | RXD | Receive Data (HSU interface) |
10 | TXD | Transmit Data (HSU interface) |
11 | IRQ | Interrupt Request (optional use) |
12 | RSTO | Reset Output (optional use) |
To use the PN532 module with an Arduino UNO, connect the module's power pins (VCC and GND) to the Arduino's 5V and GND pins, respectively. For communication, you can choose between I2C, SPI, or HSU interfaces. I2C is recommended for simplicity and ease of use.
#include <Wire.h>
#include <Adafruit_PN532.h>
// If using the I2C interface, define the pins for SDA and SCL
#define SDA_PIN A4
#define SCL_PIN A5
Adafruit_PN532 nfc(SDA_PIN, SCL_PIN);
void setup(void) {
Serial.begin(115200);
Serial.println("Hello! Scan a NFC/RFID tag to see the UID!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (!versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Configure board to read RFID tags
nfc.SAMConfig();
}
void loop(void) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if it's a 4-byte or 7-byte UID
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++) {
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
// Wait 1 second before continuing
delay(1000);
}
}
Remember to include the Adafruit PN532 library in your Arduino IDE before uploading the code to your Arduino UNO. This example demonstrates how to initialize the module, read a card's UID, and print it to the serial monitor.