Connecting 2 PN532 modules

Hey there, my friend and I have to connect multiple PN532 NFC Reader modules to an Arduino at once and scan NFC tags with them for a school project. We tried connecting one and it works perfectly fine and now we started making the code more complex with adding a second one and it doesn't work.. We don't see any problem in the code, but we are kinda newbies in this so maybe one of you guys can help us :slight_smile:

Here is the code:

#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532.h>
SoftwareSerial SWSerial( 3, 2 ); // RX, TX Eerste NFC
SoftwareSerial SWSerialTwo( 5, 4 ); // RX, TX Tweede NFC
 
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );

PN532_SWHSU pn532swhsutwo( SWSerialTwo );
PN532 nfctwo( pn532swhsutwo );

String tagId = "None", dispTag = "None";
byte nuidPICC[4];
 
void setup() {
  Serial.begin(115200);
  Serial.println("Hello Maker!");

  // Initialiseer de eerste NFC-lezer
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't Find PN53x Module for NFC1"); 
    while (1); // Halt
  }
  Serial.print("Found chip PN5");
  Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. ");
  Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.'); 
  Serial.println((versiondata >> 8) & 0xFF, DEC);
  nfc.SAMConfig(); 

  // Initialiseer de tweede NFC-lezer
  nfctwo.begin(); 
  versiondata = nfctwo.getFirmwareVersion(); 
  if (! versiondata) {
    Serial.print("Didn't Find PN53x Module for NFC2"); 
    while (1); // Halt
  }
  Serial.print("Found chip PN5");
  Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. ");
  Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.'); 
  Serial.println((versiondata >> 8) & 0xFF, DEC);
  nfctwo.SAMConfig(); 
}
 

void loop()
{
  readNFC();
  readNFCtwo();
}
 
 
void readNFC()
{
  boolean 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)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
  if (success)
  {
    Serial.print("UID Length: ");
    Serial.print(uidLength, DEC);
    Serial.println(" bytes");
    Serial.print("UID Value: ");
    for (uint8_t i = 0; i < uidLength; i++)
    {
      nuidPICC[i] = uid[i];
      Serial.print(" "); Serial.print(uid[i], DEC);
    }
    Serial.println();
    tagId = tagToString(nuidPICC);
    dispTag = tagId;
    Serial.print(F("tagId is : "));
    Serial.println(tagId);
    Serial.println("");
    delay(1000);  // 1 second halt
  }
  else
  {
    // PN532 probably timed out waiting for a card
    Serial.println("Timed out! Waiting for a card...");
  }
}
String tagToString(byte id[4])
{
  String tagId = "";
  for (byte i = 0; i < 4; i++)
  {
    if (i < 3) tagId += String(id[i]) + ".";
    else tagId += String(id[i]);
  }
  return tagId;
}

void readNFCtwo() {
  boolean success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; 
  uint8_t uidLength; 
  success = nfctwo.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
  if (success) {
    Serial.print("NFC2 UID Length: "); 
    Serial.print(uidLength, DEC);
    Serial.println(" bytes");
    Serial.print("NFC2 UID Value: ");
    for (uint8_t i = 0; i < uidLength; i++) {
      nuidPICC[i] = uid[i]; 
      Serial.print(" "); Serial.print(uid[i], DEC);
    }
    Serial.println();
    // ... (Your tagToString() function for NFC2, if necessary) ...
    Serial.print(F("NFC2 tagId is : "));
    Serial.println(tagId); // You might need to use a different variable for NFC2's tagId
    Serial.println("");
    delay(1000); 
  } else {
    Serial.println("NFC2 Timed out! Waiting for a card...");
  }
}

I think you will need an I2C multiplexer. Can I connect 2 RFID card readers to one arduino? - Stack Overflow

You can't have more than one softwareserial afaik.
Consider using a arduino board with multiple serial ports.

Ah okay, is there any other way of doing this or is this the only way?

Do you know which Arduino boards offer that? We are currently using an Arduino UNO

There are many..
I personally would go with Esp32 board. Arduino Mega is another option.

Hi, @matsdev
Can you please post a link to data/specs of your 532?
Does it have SPI comms as well?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hey Tom! The datasheet is here and if i'm correct it does have SPI comms, but we don't have the right pin points soldered to the module and are not able to do this until monday..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.