loop() {} not working in NFC Module code

Have a code for NFC Module PN532 v3

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

void setup() {
  Serial.begin(9600);
  Serial.println("NDEF Writer");
  nfc.begin();
}

void loop() {
  Serial.println("check");
  Serial.println("\n Place a formatted NFC tag on the reader.");
  if (nfc.tagPresent()) {
    NdefMessage message = NdefMessage();
    message.addUriRecord("http://arduino.cc");

    bool success = nfc.write(message);
    if (success) {
      Serial.println("Success. Try reading this tag with your phone.");        
    } else {
      Serial.println("Write failed.");
    }
  }
  delay(5000);
}

In serial monitor printing only "NDEF Writer", "check" dont printing. Have the same problem with enother code for NFC module. With other projects dont have any problems.

Please, help

It appears that your code is hanging in nfc.begin() which suggests that the NFC module is not wired correctly.
To be sure that it is hanging there add a print immediately afterwards:

  nfc.begin();
  Serial.println("NFC opened");

Pete

Make sure that your solder joints for the four pins are good and that you have wired the pins to the correct pins on the Arduino. SDA to A4, SCL to A5.
Do you have 4.7k pullup resistors to +5V on A4 and A5? If the module itself doesn't have them, you definitely need to add them.
[+edit] Use an I2C scanner sketch to see if it finds the module's I2C address. If it doesn't, you've definitely got a wiring problem.

Pete

el_supremo, thanks for the advice. I checked everything, as you said and as I understood I have problems with the wiring. But I have correct connections SDA to A4 and SCL to A5, vcc I connected with 3,3V because with 5V arduino didn't find PN532 board. On this I want to clarify about this one here: "Do you have 4.7k pullup resistors to + 5V on A4 and A5? If the module itself does not have them, you definitely need to add them." Can you send a diagram or tell me about it in more detail?

Look at this documentation
It shows that the module has two jumper switches which by default are set for HSU mode (whatever that is). If you haven't changed or checked these switches (see the photos on the third page) you need to make sure that switch 1 (channel 1) is pushed towards ON and switch 2 is OFF.
The circuit diagram for the board is here and if I read it correctly, it already has the 4.7k pullup resistors. So after you've got the switches set correctly, this should work.

Pete

el_supremo, problem in this, that I did all from this documentation what you sended. And switches are correct, but after "nfc.begin" code does not want to work.

Run this I2C scanner sketch to see if it sees your module.

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011
// See: http://www.gammon.com.au/forum/?id=10896 

#include <Wire.h>

void setup() {
  Serial.begin (9600);
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

Pete

el_supremo, hmmm, yes, it sees, in serial monitor is:
I2C scanner. Scanning ...
Found address: 36 (0x24)
Done.
Found 1 device(s).

But the code I wrote at the beginning still does not work after nfc.begin ();.
What does it mean?

This is just a test. Try removing the call to begin and call Wire instead:

//  nfc.begin();
  Wire.begin();

All nfc.begin() does is call Wire.begin() and we know that the I2C_scanner (which also does Wire.begin()) works. So, nfc.begin() should work.

Pete

el_supremo, if I comment "nfc.begin();" and write "Wire.begin();" loop starts working. But how I understand now all nfc parts, for example: nfc.tagPresent() not working. May be problem in this part:

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

Try adding & here:

PN532_I2C pn532_i2c(&Wire);

and switch back to nfc.begin().

Pete

el_supremo, Dropbox - shot.PNG - Simplify your life

Dont know what to do

I have correct connections SDA to A4 and SCL to A5

Did you solder these? Just making sure - I've seen a few cases where people just stuck pins through holes without soldering them.

I connected with 3,3V

Connect Vcc to 5V (the Vcc pin between GND and SDA on the module - not the one between GND and SS).
And change &Wire back to Wire.

Pete

el_supremo:
Did you solder these? Just making sure - I've seen a few cases where people just stuck pins through holes without soldering them.

Connect Vcc to 5V (the Vcc pin between GND and SDA on the module - not the one between GND and SS).
And change &Wire back to Wire.

Pete

Yes, they are soldered. And this all not working with 5V and &Wire back to Wire

OK. I'm officially out of ideas. Sorry.
Can you post a photo of the module which shows how it is wired to the Arduino?

Pete

el_supremo:
OK. I'm officially out of ideas. Sorry.
Can you post a photo of the module which shows how it is wired to the Arduino?

Pete

https://drive.google.com/open?id=0B8Fmuk-qQtKva1dtcXFHVU9ZTU0

That looks good.
I have two more things to try but they are desperate shots in the dark.

  • replace all the wiring between the module and the UNO. One of the wires might be flaky and, if so, replacing them all should fix it.
  • Add these two lines before the nfc.begin()
  Wire.begin();
  Serial.println("Done Wire.begin()");

Pete

el_supremo:
That looks good.
I have two more things to try but they are desperate shots in the dark.

  • replace all the wiring between the module and the UNO. One of the wires might be flaky and, if so, replacing them all should fix it.
  • Add these two lines before the nfc.begin()
  Wire.begin();

Serial.println("Done Wire.begin()");





Pete

I tried all this and it does not work. I think the problem is in these two lines:

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

. I tried to write them down to another:

PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

but it did not help. A friend advised to write the code without using the library, but I did not find any example.

@avran Did you find the solution? I am also struggling with the same problem.

Hey guys. I have the exact same issue. I2C working but it's hanging in nfc.begin()... I even changed all cables, messed with the libraries, tested different switch settings (SPI/I2C) but nothing helped. I got my arduino nano from aliexpress as well as the pn532 module. Did anyone of you investigated further? Could it be a cold soldering joint?