I'm trying to get the PN532 NFC and RFID reader working with an ESP32. Currently I'm able to init the PN532 from the ESP32 but I can't read any tags.
There's a lot of counterfeit PN532's in the world but I don't think mine is one since I bought it from elechouse.com, a very reputable vendor (product link).
I'm using the reader in I2C mode, and set the jumper to I2C on the reader. I'm connected to the I2C pins of a Espressif ESP32-Wrover dev board (D21 and D22). Communication is successful since the ESP32 is able to read the firmware version off the reader. But it can't read any cards or RFID fobs.
I used this code as a starting point. Pasting below:
// Define the interface type
#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
#elif 0
#include <PN532_HSU.h>
#include <PN532.h>
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
#endif
// added
//#define SDA 21
//#define SDL 22
volatile bool connected = false;
void setup(void)
{
Serial.begin(115200);
Serial.println("*** Testing Module PN532 NFC RFID ***");
//Wire.begin(SDA, SDL); // added
delay(2000);
}
void loop(void)
{
boolean success;
// Buffer to store the UID
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
// UID size (4 or 7 bytes depending on card type)
uint8_t uidLength;
while (!connected) {
connected = connect();
}
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
// If the card is detected, print the UID
if (success)
{
Serial.println("Card Detected");
Serial.print("Size of UID: "); Serial.print(uidLength, DEC);
Serial.println(" bytes");
Serial.print("UID: ");
for (uint8_t i = 0; i < uidLength; i++)
{
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println("");
Serial.println("");
delay(1000);
connected = connect();
}
else
{
// PN532 probably timed out waiting for a card
// Serial.println("Timed out waiting for a card");
}
}
bool connect() {
nfc.begin();
// Connected, show version
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata)
{
Serial.println("PN53x card not found!");
return false;
}
//port
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware version: "); Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for card (ISO14443A Mifare)...");
Serial.println("");
return true;
}
The output is:
20:25:21.867 -> *** Testing Module PN532 NFC RFID ***
20:25:29.532 -> Found chip PN532
20:25:29.532 -> Firmware version: 1.6
20:25:29.532 -> Waiting for card (ISO14443A Mifare)...
20:25:29.532 ->
But it won't read any tags or fobs. I get the same behavior using the examples script from the Github repo, which is essentially the same as the code above.
I don't imagine anyone has any ideas?