RFID that works with UNO gives weird results with MEGA

Hello all,

My first post, and I really hope that I'm getting all the instructions right... please tell me otherwise!

I've been headdesking for three days over a thorny problem. As part of a larger project, I've been using a PN5180 RFID reader. It worked fine with an UNO, but I've had to switch to MEGA for the extra memory. All other components work fine (including other SPI ones)... except this one, with some very strange results.

For starters, here are the pinouts, old and new. (I've checked them over a dozen times, so hopefully no mistake there.) All pins are shifted 5V <> 3.3V. I chose only pins that have interrupt, just in case.

RST: 7 on UNO, 10 on MEGA
NSS: A0 on UNO, A8 on MEGA
MOSI: 11 on UNO, 51 on MEGA
MISO: 12 on UNO, 50 on
SCK: 13 on UNO, 52 on MEGA
BUSY: 8 on UNO, 11 on MEGA

Next, a simplified bit of code that I wrote to showcase the issue:

#include <PN5180.h>
#include <PN5180ISO15693.h>

PN5180ISO15693 nfc(A8, 11, 10);

void setup() {
  Serial.begin(9600);
  Serial.println(F("Initialising..."));
  nfc.begin();
  Serial.println(F("Resetting..."));
  nfc.reset();
  Serial.println(F("Enabling RF field..."));
  nfc.setupRF();
  Serial.println(F("Setup Complete"));
}

void loop() {
  nfc.reset();
  nfc.setupRF();
  // Variable to store the ID of any tag read by this reader
  uint8_t thisUid[8];
  // Try to read a tag ID (or "get inventory" in ISO15693-speak)
  ISO15693ErrorCode rc = nfc.getInventory(thisUid);
  // If the result code was that a card had been read
  if (rc == ISO15693_EC_OK) {
    Serial.print(F("New Card Detected on Reader: "));
    for (int j = 0; j < sizeof(thisUid); j++) {
      Serial.print(thisUid[j], HEX);
    }
    Serial.println();
  }
  delay(1000);
}

I use ATrappmann's library, which as far as I can find is the only one available for the PN5180:

And finally, the results on MEGA:

Looks fine... except that these were obtained with only three RFID tags, which stayed in contact with the reader the whole time! (One for the first four lines, one for the next three, one for the last three.) So the readings vary, and come in at random intervals of several seconds.
By contrast, on UNO, the results are correct and come in every second as they should.

Right now, the only idea that I have left is that something in the library might be malfunctioning with MEGA... but I searched for "mega" and "uno" in every h and cpp file with no results, and after that I'm out of my depths. I checked several other programs from the library's examples folder, all have comparable issues: difficulty finding the card, semi-random results when they do, and work fine on UNO.

Does anyone have any leads, any ideas of where such an issue could come from? I'm open to anything right now.

Thank you all,
Alex

Just a thought, and maybe it won't have any impact on your issue, but you don't use the SPI interface SS pin on your UNO or MEGA as the Slave Select signal for your SPI device.

If you are using another pin as the Slave Select, then you should set the SS pin as an output.

Hello Mark,
Good idea, but no change (both specifying that A8 is output and switching NSS to 53).
I use A8 because in the full setup there are three SPI devices. And, in the case of UNO, I was all out of digital pins.
It does feel like the reading is a bit more regular, but that may be subjective...

For anyone that this might help, I found a solution after a few more days of migraine. There were two separate issues, one on a library (caused the false readings) and one hardware (maybe caused the delays, maybe unrelated).

First:
After restarting my computer, I found that the following error message started appearing in the debugger:

In file included from C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.cpp:22:0:
C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.cpp: In member function 'bool PN5180::switchToLPCD(uint16_t)':
C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.h:72:30: warning: left shift count >= width of type [-Wshift-count-overflow]
#define LPCD_IRQ_STAT (1<<19) // LPCD Detection IRQ
^
C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.cpp:430:29: note: in expansion of macro 'LPCD_IRQ_STAT'
writeRegister(IRQ_ENABLE, LPCD_IRQ_STAT | GENERAL_ERROR_IRQ_STAT);
^~~~~~~~~~~~~
C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.h:71:37: warning: left shift count >= width of type [-Wshift-count-overflow]
#define GENERAL_ERROR_IRQ_STAT (1<<17) // General error IRQ
^
C:\Users\Alexandre\Documents\Arduino\libraries\PN5180-Library-master\PN5180.cpp:430:45: note: in expansion of macro 'GENERAL_ERROR_IRQ_STAT'
writeRegister(IRQ_ENABLE, LPCD_IRQ_STAT | GENERAL_ERROR_IRQ_STAT);
^~~~~~~~~~~~~~~~~~~~~~

I know enough to recognize that this would cause the kind of bugs that I was getting, since the overflow would send false data all over the place. I don't know enough to fix the libraries, so instead I tried different forks until one worked (it was the original, unmodified one, in fact). This eliminated the unreliable readings.

Second:
Then, patching the RFID reader into the rest of my system, I found that it didn't work anymore. It took a while, but I found the solution on the following thread:

It was a sub-par SD card reader (Joy-it ST7735) that was causing SPI issues. I tried adding a resistor on the MISO pin as suggested: higher than 1.5 kOhm stopped the SD, lower than 150 Ohm stopped the RFID reader. 680 Ohm, and everything seems to work!

Anyways, problem solved. I hope that this can help someone eventually!