Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1128

Problems with ESP32 and PN5180 rfid

$
0
0

Hi, I am having problems interfacing a PN5180 (https://www.aliexpress.com/item/4001084524106.html?spm=a2g0o.order_list.order_list_main.16.12091802I4McDG) with a ESP32 (wroom-32).
Everything seems to work fine, but when I put one of the cards near the sensor nothing happens. The card or rfidtag are the ones shipped alongside the PN5180 sensor I bought online

The connections are below:
// 5V <--> 5V
// 3.3V <--> 3.3V
// GND <--> GND
// SCLK, 18 --> SCLK
// MISO, 19 <-- MISO
// MOSI, 23 --> MOSI
// SS, 5 --> NSS
// BUSY, 16 <-- BUSY
// Reset, 17 --> RST

I have also tried the below with the same outcome:
// 5V <--> 5V
// 3.3V <--> 3.3V
// GND <--> GND
// SCLK, 18 --> SCLK
// MISO, 19 <-- MISO
// MOSI, 23 --> MOSI
// SS, 16 --> NSS
// BUSY, 5 <-- BUSY
// Reset, 17 --> RST

The code is the sample code got from the PN5180 library:

#define PN5180_NSS 5
#define PN5180_BUSY 16
#define PN5180_RST 17

PN5180ISO15693 nfc(PN5180_NSS, PN5180_BUSY, PN5180_RST);

void setup()
{
  Serial.begin(115200);
  Serial.println(F("=================================="));
  Serial.println(F("Uploaded: " __DATE__ " " __TIME__));
  Serial.println(F("PN5180 ISO15693 Demo Sketch"));

  nfc.begin();

  Serial.println(F("----------------------------------"));
  Serial.println(F("PN5180 Hard-Reset..."));
  nfc.reset();

  Serial.println(F("----------------------------------"));
  Serial.println(F("Reading product version..."));
  uint8_t productVersion[2];
  nfc.readEEprom(PRODUCT_VERSION, productVersion, sizeof(productVersion));
  Serial.print(F("Product version="));
  Serial.print(productVersion[1]);
  Serial.print(".");
  Serial.println(productVersion[0]);

  if (0xff == productVersion[1])
  { // if product version 255, the initialization failed
    Serial.println(F("Initialization failed!?"));
    Serial.println(F("Press reset to restart..."));
    Serial.flush();
    exit(-1); // halt
  }

  Serial.println(F("----------------------------------"));
  Serial.println(F("Reading firmware version..."));
  uint8_t firmwareVersion[2];
  nfc.readEEprom(FIRMWARE_VERSION, firmwareVersion, sizeof(firmwareVersion));
  Serial.print(F("Firmware version="));
  Serial.print(firmwareVersion[1]);
  Serial.print(".");
  Serial.println(firmwareVersion[0]);

  Serial.println(F("----------------------------------"));
  Serial.println(F("Reading EEPROM version..."));
  uint8_t eepromVersion[2];
  nfc.readEEprom(EEPROM_VERSION, eepromVersion, sizeof(eepromVersion));
  Serial.print(F("EEPROM version="));
  Serial.print(eepromVersion[1]);
  Serial.print(".");
  Serial.println(eepromVersion[0]);

  /*
  Serial.println(F("----------------------------------"));
  Serial.println(F("Reading IRQ pin config..."));
  uint8_t irqConfig;
  nfc.readEEprom(IRQ_PIN_CONFIG, &irqConfig, 1));
  Serial.print(F("IRQ_PIN_CONFIG=0x"));
  Serial.println(irqConfig, HEX);

  Serial.println(F("----------------------------------"));
  Serial.println(F("Reading IRQ_ENABLE register..."));
  uint32_t irqEnable;
  nfc.readRegister(IRQ_ENABLE, &irqEnable));
  Serial.print(F("IRQ_ENABLE=0x"));
  Serial.println(irqConfig, HEX);
  */

  Serial.println(F("----------------------------------"));
  Serial.println(F("Enable RF field..."));
  nfc.setupRF();
}

uint32_t loopCnt = 0;
bool errorFlag = false;

void loop()
{
  if (errorFlag)
  {
    uint32_t irqStatus = nfc.getIRQStatus();
    showIRQStatus(irqStatus);

    if (0 == (RX_SOF_DET_IRQ_STAT & irqStatus))
    { // no card detected
      Serial.println(F("*** No card detected!"));
    }

    nfc.reset();
    nfc.setupRF();

    errorFlag = false;
  }

  Serial.println(F("----------------------------------"));
  Serial.print(F("Loop #"));
  Serial.println(loopCnt++);

  /*
    // code for unlocking an ICODE SLIX2 protected tag
    uint8_t password[] = {0x01, 0x02, 0x03, 0x04}; // put your privacy password here
    ISO15693ErrorCode myrc = nfc.unlockICODESLIX2(password);
    if (ISO15693_EC_OK == myrc) {
      Serial.println("unlockICODESLIX2 successful");
    }
  */

  uint8_t uid[8];
  ISO15693ErrorCode rc = nfc.getInventory(uid);
  if (ISO15693_EC_OK != rc)
  {
    Serial.print(F("Error in getInventory: "));
    Serial.println(nfc.strerror(rc));
    errorFlag = true;
    delay(1000);
    return;
  }
  Serial.print(F("Inventory successful, UID="));
  for (int i = 0; i < 8; i++)
  {
    Serial.print(uid[7 - i], HEX); // LSB is first
    if (i < 2)
      Serial.print(":");
  }
  Serial.println();

  Serial.println(F("----------------------------------"));
  uint8_t blockSize, numBlocks;
  rc = nfc.getSystemInfo(uid, &blockSize, &numBlocks);
  if (ISO15693_EC_OK != rc)
  {
    Serial.print(F("Error in getSystemInfo: "));
    Serial.println(nfc.strerror(rc));
    errorFlag = true;
    return;
  }
  Serial.print(F("System Info retrieved: blockSize="));
  Serial.print(blockSize);
  Serial.print(F(", numBlocks="));
  Serial.println(numBlocks);

  Serial.println(F("----------------------------------"));
  uint8_t readBuffer[blockSize];
  for (int no = 0; no < numBlocks; no++)
  {
    rc = nfc.readSingleBlock(uid, no, readBuffer, blockSize);
    if (ISO15693_EC_OK != rc)
    {
      Serial.print(F("Error in readSingleBlock #"));
      Serial.print(no);
      Serial.print(": ");
      Serial.println(nfc.strerror(rc));
      errorFlag = true;
      return;
    }
    Serial.print(F("Read block #"));
    Serial.print(no);
    Serial.print(": ");
    for (int i = 0; i < blockSize; i++)
    {
      if (readBuffer[i] < 16)
        Serial.print("0");
      Serial.print(readBuffer[i], HEX);
      Serial.print(" ");
    }
    Serial.print(" ");
    for (int i = 0; i < blockSize; i++)
    {
      if (isprint(readBuffer[i]))
      {
        Serial.print((char)readBuffer[i]);
      }
      else
        Serial.print(".");
    }
    Serial.println();
  }

#ifdef WRITE_ENABLED
  Serial.println(F("----------------------------------"));
  uint8_t *writeBuffer = malloc(blockSize);
  for (int i = 0; i < blockSize; i++)
  {
    writeBuffer[i] = 0x80 + i;
  }
  for (int no = 0; no < numBlocks; no++)
  {
    rc = nfc.writeSingleBlock(uid, no, writeBuffer, blockSize);
    if (ISO15693_EC_OK == rc)
    {
      Serial.print(F("Wrote block #"));
      Serial.println(no);
    }
    else
    {
      Serial.print(F("Error in writeSingleBlock #"));
      Serial.print(no);
      Serial.print(": ");
      Serial.println(nfc.strerror(rc));
      errorFlag = true;
      return;
    }
  }
#endif /* WRITE_ENABLED */

  /*
    // code for locking an ICODE SLIX2 protected tag
    ISO15693ErrorCode myrc = nfc.lockICODESLIX2(password);
    if (ISO15693_EC_OK == myrc) {
      Serial.println("lockICODESLIX2 successful");
      delay(5000);
  */
  delay(1000);
}

void showIRQStatus(uint32_t irqStatus)
{
  Serial.print(F("IRQ-Status 0x"));
  Serial.print(irqStatus, HEX);
  Serial.print(": [ ");
  if (irqStatus & (1 << 0))
    Serial.print(F("RQ "));
  if (irqStatus & (1 << 1))
    Serial.print(F("TX "));
  if (irqStatus & (1 << 2))
    Serial.print(F("IDLE "));
  if (irqStatus & (1 << 3))
    Serial.print(F("MODE_DETECTED "));
  if (irqStatus & (1 << 4))
    Serial.print(F("CARD_ACTIVATED "));
  if (irqStatus & (1 << 5))
    Serial.print(F("STATE_CHANGE "));
  if (irqStatus & (1 << 6))
    Serial.print(F("RFOFF_DET "));
  if (irqStatus & (1 << 7))
    Serial.print(F("RFON_DET "));
  if (irqStatus & (1 << 8))
    Serial.print(F("TX_RFOFF "));
  if (irqStatus & (1 << 9))
    Serial.print(F("TX_RFON "));
  if (irqStatus & (1 << 10))
    Serial.print(F("RF_ACTIVE_ERROR "));
  if (irqStatus & (1 << 11))
    Serial.print(F("TIMER0 "));
  if (irqStatus & (1 << 12))
    Serial.print(F("TIMER1 "));
  if (irqStatus & (1 << 13))
    Serial.print(F("TIMER2 "));
  if (irqStatus & (1 << 14))
    Serial.print(F("RX_SOF_DET "));
  if (irqStatus & (1 << 15))
    Serial.print(F("RX_SC_DET "));
  if (irqStatus & (1 << 16))
    Serial.print(F("TEMPSENS_ERROR "));
  if (irqStatus & (1 << 17))
    Serial.print(F("GENERAL_ERROR "));
  if (irqStatus & (1 << 18))
    Serial.print(F("HV_ERROR "));
  if (irqStatus & (1 << 19))
    Serial.print(F("LPCD "));
  Serial.println("]");
}

And the logs are below (basically getInventory is always returning -1):

PN5180 ISO15693 Demo Sketch
SPI pinout: SS=5, MOSI=23, MISO=19, SCK=18
----------------------------------
PN5180 Hard-Reset...
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 04 00 00 00'
Register value=0x00000004
IRQ-Status=0x00000004
----------------------------------
Reading product version...
Reading EEPROM at 0x10, size=2...
Sending SPI frame: '07 10 02'
Receiving SPI frame...
Received: 00 04'
EEPROM values: 00 04
Product version=4.0
----------------------------------
Reading firmware version...
Reading EEPROM at 0x12, size=2...
Sending SPI frame: '07 12 02'
Receiving SPI frame...
Received: 00 04'
EEPROM values: 00 04
Firmware version=4.0
----------------------------------
Reading EEPROM version...
Reading EEPROM at 0x14, size=2...
Sending SPI frame: '07 14 02'
Receiving SPI frame...
Received: 00 99'
EEPROM values: 00 99
EEPROM version=153.0
----------------------------------
Enable RF field...
Loading RF-Configuration...
Load RF-Config: txConf=0D, rxConf=8D
Sending SPI frame: '11 0D 8D'
done.
Turning ON RF field...
Set RF ON
Sending SPI frame: '16 00'
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 04 02 00 00'
Register value=0x00000204
IRQ-Status=0x00000204
Clear IRQ-Status with mask=x00000200
Write Register 0x03, value (LSB first)=0x00020000
Sending SPI frame: '00 03 00 02 00 00'
done.
Write Register 0x00 with AND mask (LSB first)=0xF8FFFFFF
Sending SPI frame: '02 00 F8 FF FF FF'
Write Register 0x00 with OR mask (LSB first)=0x03000000
Sending SPI frame: '01 00 03 00 00 00'
----------------------------------
Loop #0
Get Inventory...
Issue Command 0x01...
Send data (len=3): 26 01 00
Write Register 0x00 with AND mask (LSB first)=0xF8FFFFFF
Sending SPI frame: '02 00 F8 FF FF FF'
Write Register 0x00 with OR mask (LSB first)=0x03000000
Sending SPI frame: '01 00 03 00 00 00'
Get Transceive state...
Reading register 0x1D...
Sending SPI frame: '04 1D'
Receiving SPI frame...
Received: 76 00 02 01'
Register value=0x01020076
TRANSCEIVE_STATE=0x01
Sending SPI frame: '09 00 26 01 00'
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 06 00 00 00'
Register value=0x00000006
IRQ-Status=0x00000006
Didnt detect RX_SOF_DET_IRQ_STAT after sendDataError in getInventory: ISO15693ErrorCode=-1
No card detected!
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 06 00 00 00'
Register value=0x00000006
IRQ-Status=0x00000006
IRQ-Status 0x6: [ TX IDLE ]
*** No card detected!
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 04 00 00 00'
Register value=0x00000004
IRQ-Status=0x00000004
Loading RF-Configuration...
Load RF-Config: txConf=0D, rxConf=8D
Sending SPI frame: '11 0D 8D'
done.
Turning ON RF field...
Set RF ON
Sending SPI frame: '16 00'
Read IRQ-Status register...
Reading register 0x02...
Sending SPI frame: '04 02'
Receiving SPI frame...
Received: 04 02 00 00'
Register value=0x00000204
IRQ-Status=0x00000204
Clear IRQ-Status with mask=x00000200
Write Register 0x03, value (LSB first)=0x00020000
Sending SPI frame: '00 03 00 02 00 00'
done.
Write Register 0x00 with AND mask (LSB first)=0xF8FFFFFF
Sending SPI frame: '02 00 F8 FF FF FF'
Write Register 0x00 with OR mask (LSB first)=0x03000000
Sending SPI frame: '01 00 03 00 00 00'
----------------------------------

2 posts - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1128

Trending Articles