PN532 not working with Arduino Uno R3

Alright, I'm using this code here:

// for Hardware Serial
/*#include <PN532_HSU.h>
  #include <PN532.h>
  PN532_HSU pn532hsu( Serial );
  PN532 nfc( pn532hsu );
*/

// for Software Serial
#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532.h>
SoftwareSerial SWSerial( 2, 3 ); // RX, TX
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );


String tagId = "None", dispTag = "None";
byte nuidPICC[4];

void setup(void) {

  Serial.begin(115200);
  Serial.println("Hello Maker!");
  //  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't Find PN53x Module");
    while (1); // Halt
  }

  // Got valid data, print it out!
  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);

  // Configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A Card ...");

}

void loop() {
  readNFC();
}

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;
}

I'm getting these random characters in the serial monitor:
e~�Hello Maker!

��� and it would randomly not display "Hello Maker!" and sometimes more of these invalid characters would show up. And if I press reset on the board, it would show more of these characters, but not the "Hello Maker!"

And this is the layout:

All from: Using the PN532 RFID reader with Arduino • AranaCorp

Help please

Hi,
Do you have the IDE serial monitor baud rate set at 115200?

Have you configured the two DIP Switches on the module for UART operation?

Does it show any of the connection Serial.prints?
Or does it just cycle "Hello Maker"?

Can you please post a picture of your project so we can see your component layout?

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

Uhh, so I'm kinda new to the whole Arduino stuff and have no idea what DIP switches are. It's connected directly and isn't on a breadboard, if that helps :/. Also, I don't think I have my rate set to 115200. Do I just uncomment the 24th line? And for the serial prints, it's interesting. It would sometimes say "Hello Maker!", then doesn't. It would show random characters and isn't the same quantity every time and might be less characters shown or more. It's really weird. It would show empty squares, colons, forward slashes. Now, it just showed the letter Z for some reason

Try changing that to:

  Serial.begin(115200);
  delay(200);
  Serial.println("Hello Maker!");
  Serial.flush();

When you upload a new sketch it runs for about 180 milliseconds before the uploader disconnects and allows Serial Monitor to connect. Any characters sent in that 180 milliseconds will be crammed into the USB buffer before Serial Monitor sets the input baud rate and that causes them to arrive garbled.

The only reason I can think of for the "Hello Maker!" to not appear is a crash shortly after the text was put in the serial buffer. The .flush() will wait until all of the characters in the buffer have been sent.

Okay, so it kinda works now, not really still.
Now, it just says:
���a���:�/�Z~��/�����Hello Maker!
idn't Find PN53x Module�����

like that, however the question marks are just empty squares ^

and well, it says it didn't find my module too, which is strange. Any ideas as to why? I have connected everything properly and securely

Hi,


Read this before doing anything else.

It explains it all.

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

Sorry, it doesn't help with the problem still

sorry, your answer doesn't help neither!

Please show a real picture of your setup.
Each wire must be visible.
The DIP Switches must be visible.
Please link to the product / shop you have bought - this boards get counterfeit a lot - not all are working like expected.

So, I decided to change the layout from a UART to SPI breakout. I followed another person's guide here: ITEAD PN532 (NFC) board not working! - #4 by jonano but it still doesn't work. At least the gibberish characters are gone :neutral_face:. Here's the layout and the attached image with the switches:
5V -> Arduino 5V
GND -> Arduino GND
SCK -> Digital 2
MI -> Digital 4
MO -> Digital 3
NSS -> Digital 5




There is a M solely labelled but I assumed that was the MI this guide was talking about and it was just a misprint

(yes, I spent 40 dollars on this because it's the only one in Australia.)

OK. Let's stick to HW SPI.
You are using an Arduino UNO - not a Leonardo as in the other thread.
I use an Arduino Leonardo, but it should work with all other Arduinos as well I guess you just need to take care of the SPI pins! Leonardo does not use SPI on 13,12,11 ! For a Arduino universal solution I think using the ICSP Pins would be actually the best solution, but I didn't try that yet.

so first - connect the module to the correct 11-13 pins and modify the sketch.

Hi,
Have you got the slide switches set in the correct position for SPI?

Note, Your board is different to the one indicated in post #1.

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

I don't know what pins to connect. Would you mind assisting, please? Also, what modifications shall I make to the sketch? Should I just change the pin values to what the pins are connected to?

They're both essentially the same thing, with the same chip and connections

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