PN532 (NFC Shield) + Data Logging Shield

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
j-culp
 
Posts: 10
Joined: Tue Jan 01, 2013 1:42 pm

PN532 (NFC Shield) + Data Logging Shield

Post by j-culp »

I have an Arduino Uno with the PN532 (NFC Shield) and the Data Logging Shield stacked.

Code to utilize either shield individually works flawlessly. Yet when I combine the code for the two the arduino locks up or resets constantly when trying to read a card with the NFC Shield and print the data.

As far as I can tell both use the I2C pins analog 4 and 5, the data logger uses pin 10 for chipselect, and the NFC Shield uses pins 2 and 3 for interrupt and reset. Is this an issue with a hardware conflict I'm not seeing or is my code broken somehow?

Thanks for any help anyone can offer,
James

Code: Select all

/* SD Card Setup */
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;

File myFile;

/* RealTime Clock Setup */
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;


/* NFCShield Setup */
//#include <Wire.h> // already included above
#include <Adafruit_NFCShield_I2C.h>

#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield

Adafruit_NFCShield_I2C nfc(IRQ, RESET);




void setup()
{
  // Start Serial communications, high baud rate to keep up with the NFC reader.
  Serial.begin(115200);

  // Startup SD Card
/* If this part is uncommented things hang in loop() after the card is detected and the first block is read and trying to be printed.
  if (!SD.begin(chipSelect)) {
    Serial.println("SD initialization failed!");
    return;
  }
  Serial.println("SD initialization done.");
*/
  Serial.println("##############################");

  // Start RTC stuff
  Wire.begin();
  rtc.begin();

  DateTime ts;
  ts = rtc.now();
    Serial.print(ts.year(), DEC);
    Serial.print('/');
    Serial.print(ts.month(), DEC);
    Serial.print('/');
    Serial.print(ts.day(), DEC);
    Serial.print(' ');
    Serial.print(ts.hour(), DEC);
    Serial.print(':');
    Serial.print(ts.minute(), DEC);
    Serial.print(':');
    Serial.print(ts.second(), DEC);
    
    Serial.print(" = ");
    Serial.print(ts.unixtime());
    Serial.print("s / ");
    Serial.print(ts.unixtime() / 86400L);
    Serial.print("d since 1970");
    
    Serial.println();

  Serial.println("##############################");
  
  // Start RFID stuff
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok 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() {
  uint8_t 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)
  
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");


    if (uidLength == 4)
    {
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
    
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
    
      // Start with block 4 (the first block of sector 1) since sector 0
      // contains the manufacturer data and it's probably better just
      // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    
      if (success)
      {
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
    
        // If you want to write something to block 4 to test with, uncomment
        // the following line and this text should be read back in a minute
        //memcpy(data, (const uint8_t[]){ 't', 'e', 'a', 'm', '3', '3', '8', '9', '.', 'i', 'n', 'f', 'o', 0, '1', '4' }, sizeof data);
        //success = nfc.mifareclassic_WriteDataBlock (4, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);
    
        if (success)
        {
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
/* If the SD device is running the next line locks up the arduino after the first hex character is dumped 
    The data being read contains ...
74 65 61 6D 33 33 38 39 2E 69 6E 66 6F 00 31 34  team3389.info.14
*/
          nfc.PrintHexChar(data, 16);
          Serial.println("");
      
          // Wait a bit before reading the card again
          delay(1000);
        }
        else
        {
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        }
      }
      else
      {
        Serial.println("Ooops ... authentication failed: Try another key?");
      }
    }
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
    
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
    
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }
}


void readFile() {
  // Open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}


void writeFile() {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  return;
}


User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by Franklin97355 »

Make sure the NFC and the RTC have different i2c addresses. I have not looked at the shields yet to see if they do but you should check.
<edit> they do not use the same addresses the RTC uses 0x68 and the NFC uses 0x48

User avatar
j-culp
 
Posts: 10
Joined: Tue Jan 01, 2013 1:42 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by j-culp »

I'm guessing by the lack of response that noone including Adafruit tech support has any insight into this? This is a project for our high school robotics team and the students are stopped until we can figure out what is causing the rapid reboots when trying to read the RFID card.

Thanks to anyone with any further ideas,
James

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by Franklin97355 »

I don't have these devices but let's work a bit further and I will see if the rest of the team have any ideas.Could you post clear pictures of your board and the connections to it? Also how are you powering the system?

User avatar
j-culp
 
Posts: 10
Joined: Tue Jan 01, 2013 1:42 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by j-culp »

I am using the usb cable to program, communicate and power the Arduino and shields.

Here is a picture of the shields in their current configuration.
Untitled.png
Untitled.png (896.83 KiB) Viewed 644 times
James

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: PN532 (NFC Shield) + Data Logging Shield

Post by adafruit_support_rick »

j-culp wrote:Code to utilize either shield individually works flawlessly. Yet when I combine the code for the two the arduino locks up or resets constantly
This is a classic symptom of running out of SRAM. We have a tutorial on Arduino memory here which offers some tips on this problem.
https://learn.adafruit.com/memories-of-an-arduino

I would start by applying the F() macro to all of the literal strings in your Serial.print statements. Literal strings are stored in SRAM by default. The F() macro moves them to program memory.
Example:

Code: Select all

Serial.println(F("##############################"));

User avatar
j-culp
 
Posts: 10
Joined: Tue Jan 01, 2013 1:42 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by j-culp »

Thanks,

I'll give that a try tonight and see if it helps out, I'll post back either way.

Thanks again,
James

User avatar
j-culp
 
Posts: 10
Joined: Tue Jan 01, 2013 1:42 pm

Re: PN532 (NFC Shield) + Data Logging Shield

Post by j-culp »

Using the F() macro has fixed the problem, thanks very much for the link to memory and the fix.

James

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: PN532 (NFC Shield) + Data Logging Shield

Post by adafruit_support_rick »

Great!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”