NFC Shield V2 with Arduino MEGA

Arduino, Seeeduino Serials and mutants. Share your problems and experence on arduino compatible board such as seeeduino/stalker, etc.

Moderators: lily.li, violet, salmanfarisvp

Post Reply
misterdexter
Pre-kindergarten
Pre-kindergarten
Posts: 1
Joined: Sun Jan 20, 2019 7:50 am

NFC Shield V2 with Arduino MEGA

Post by misterdexter » Sun Jan 20, 2019 8:02 am

Good evening,

I have this Shield NFC( http://wiki.seeedstudio.com/NFC_Shield_V2.0/) that has been running for almost a year on an arduino UNO.
I recently acquired a MEGA Arduino to expand the possibilities of my project.
The NFC shield has benefited from the modifications indicated in the wiki to be
able to use the shield with an IRQ (I also use the pin 9 instead of the 10)
and it works very well with the UNO.
Unfortunately it's been a few days since I stumble to make it work the same
way on the MEGA. So I took the code of example # 3 with the adaptations that go well:

Code: Select all

#include <SPI.h>
#include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"

// FLAG_NONE used to signal nothing needs to be done
#define FLAG_NONE 0
// FLAG_IRQ_TRIGGERED used to signal an interrupt trigger
#define FLAG_IRQ_TRIGGERED 1
// FLAG_RESET_IRQ used to signal that the interrupt needs to be reset
#define FLAG_RESET_IRQ 2
// flags variable used to store the present flag
volatile int flags = FLAG_NONE;

String const myUID = "1B B3 C6 EF"; // replace this UID with your NFC tag's UID
// LED pins
int const greenLedPin = 3; // green led used for correct key notification
int const redLedPin = 4; // red led used for incorrect key notification

// the interrupt we'll be using (interrupt 0) is located at digital pin 2
int const irqPin = 2; // interrupt pin

PN532_SPI interface(SPI, 9); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10

NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object

String scannedUID = ""; // this is where we'll store the scanned tag's UID
 
void setup(void) {
    // make LED pins outputs
    pinMode(greenLedPin,OUTPUT);
    pinMode(redLedPin,OUTPUT);
   
    Serial.begin(9600); // start serial comm
    Serial.println("NDEF Reader");
    nfc.begin(); // begin NFC comm
       
    // turn off the LEDs
    digitalWrite(greenLedPin,LOW);
    digitalWrite(redLedPin,LOW);
   // attach the function "irq" to interrupt 0 on the falling edges
   attachInterrupt(digitalPinToInterrupt(2),irq,FALLING);// digital pin 2 is interrupt 0, we'll call the irq function (below) on the falling edge of this pin
}
 
void loop(void) {
    int flag = getFlag(); // get the present flag
   
    switch(flag) // check which flag/signal we are on
    {
       case FLAG_NONE:
         // nothing needs to be done
       break;
       case FLAG_IRQ_TRIGGERED: // the interrupt pin has been triggered
           Serial.println("Interrupt Triggered");
           if (nfc.tagPresent())
           {
             // an NFC tag is present
              NfcTag tag = nfc.read(); // read the NFC tag
              scannedUID = tag.getUidString(); // get the NFC tag's UID
              if(myUID.compareTo(scannedUID) == 0) // compare the NFC tag's UID with the correct tag's UID (a match exists when compareTo returns 0)
              {
                // the scanned NFC tag matches the saved myUID value
                Serial.println("Correct tag/key");
                blinkLed(greenLedPin,200,4); // blink the green led
                // put your here to trigger the unlocking mechanism (e.g. motor, transducer)
              }else{
                // the scanned NFC tag's UDI does not match the myUID value
                Serial.println("Incorrect tag/key");
                blinkLed(redLedPin,200,4); // blink the red led
                // DO NOT UNLOCK! an incorrect NFC tag was used.
                // put your code here to trigger an alarm (e.g. buzzard, speaker) or do something else
              }
              // return to the original state
              setFlag(FLAG_NONE);
              reset_PN532_IRQ_pin();
           }else{
             // a tag was not present (the IRQ was triggered by some other action)
             setFlag(FLAG_NONE);
           }
       break;
       default:
         // do any other stuff for flags not handled above
       break;
    }
}

/*
* Name: setFlat
* Description: used to set actions/flags to be executed in the loop(void) function
* Parameters:
*        int flag - the action/flag to store
* Returns: void
*/
void setFlag(int flag)
{
  flags = flag;
}

/*
* Name: getFlag
* Description: used to get the present flag/action
* Parameters: void
* Returns: int - the flags variable. The action/flag set by setFlag
*/
int getFlag()
{
  return flags;
}

/*
* Name: irq
* Description: Interrupt service routine (ISR). This function will be executed whenever there is a falling edge on digital pin 2 (the interrupt 0 pin)
* Parameters: void
* Returns: void
*/
void irq()
{
  if(getFlag()==FLAG_NONE){
    setFlag(FLAG_IRQ_TRIGGERED);
  }
}
/*
* Name: reset_PN532_IRQ_pin
* Description: used to reset the PN532 interrupt request (IRQ) pin
* Parameters: void
* Returns: void
*/
void reset_PN532_IRQ_pin()
{
  nfc.tagPresent();
}

/*
* Name: blinkLed
* Description: used to toggle a pin to blink an LED attached to the pin
* Parameters:
*      ledPin - the pin where the led is connected to
*      delayTime - the time in milliseconds between HIGH and LOW
*      times - the number of times to toggle the pin
* Returns: void
*/
void blinkLed(int ledPin,int delayTime,int times)
{
  for(int i=0;i<times;i++){
    digitalWrite(ledPin,HIGH);
    delay(delayTime);
    digitalWrite(ledPin,LOW);
    delay(delayTime);
  }
}
It turns out that this code works very well with the UNO, as expected,
the tags are read correctly. On the other hand, on the MEGA,
the chip is detected but no reading of the tags is carried out.

It seems to me that this code should work on both cards without modification?

Any help will be appreciate.
Forgive my english and thanks to gTrad :roll:

User avatar
bestlux
Kindergarten
Kindergarten
Posts: 85
Joined: Wed Jan 30, 2019 11:51 am

Re: NFC Shield V2 with Arduino MEGA

Post by bestlux » Wed Jan 30, 2019 3:18 pm

Hi, I want to know how do you connect those two boards? Could you please post a picture?

User avatar
designnotes.com
Pre-kindergarten
Pre-kindergarten
Posts: 10
Joined: Mon Jan 28, 2019 11:08 pm
Location: Philadelphia, PA
Contact:

Re: NFC Shield V2 with Arduino MEGA

Post by designnotes.com » Sat Feb 09, 2019 10:31 pm

misterdexter wrote:
Sun Jan 20, 2019 8:02 am
Good evening,

I have this Shield NFC( http://wiki.seeedstudio.com/NFC_Shield_V2.0/) that has been running for almost a year on an arduino UNO.
I recently acquired a MEGA Arduino to expand the possibilities of my project.
The NFC shield has benefited from the modifications indicated in the wiki to be
able to use the shield with an IRQ (I also use the pin 9 instead of the 10)
and it works very well with the UNO.
Unfortunately it's been a few days since I stumble to make it work the same
way on the MEGA. So I took the code of example # 3 with the adaptations that go well:

Code: Select all

#include <SPI.h>
#include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"

// FLAG_NONE used to signal nothing needs to be done
#define FLAG_NONE 0
// FLAG_IRQ_TRIGGERED used to signal an interrupt trigger
#define FLAG_IRQ_TRIGGERED 1
// FLAG_RESET_IRQ used to signal that the interrupt needs to be reset
#define FLAG_RESET_IRQ 2
// flags variable used to store the present flag
volatile int flags = FLAG_NONE;

String const myUID = "1B B3 C6 EF"; // replace this UID with your NFC tag's UID
// LED pins
int const greenLedPin = 3; // green led used for correct key notification
int const redLedPin = 4; // red led used for incorrect key notification

// the interrupt we'll be using (interrupt 0) is located at digital pin 2
int const irqPin = 2; // interrupt pin

PN532_SPI interface(SPI, 9); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10

NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object

String scannedUID = ""; // this is where we'll store the scanned tag's UID
 
void setup(void) {
    // make LED pins outputs
    pinMode(greenLedPin,OUTPUT);
    pinMode(redLedPin,OUTPUT);
   
    Serial.begin(9600); // start serial comm
    Serial.println("NDEF Reader");
    nfc.begin(); // begin NFC comm
       
    // turn off the LEDs
    digitalWrite(greenLedPin,LOW);
    digitalWrite(redLedPin,LOW);
   // attach the function "irq" to interrupt 0 on the falling edges
   attachInterrupt(digitalPinToInterrupt(2),irq,FALLING);// digital pin 2 is interrupt 0, we'll call the irq function (below) on the falling edge of this pin
}
 
void loop(void) {
    int flag = getFlag(); // get the present flag
   
    switch(flag) // check which flag/signal we are on
    {
       case FLAG_NONE:
         // nothing needs to be done
       break;
       case FLAG_IRQ_TRIGGERED: // the interrupt pin has been triggered
           Serial.println("Interrupt Triggered");
           if (nfc.tagPresent())
           {
             // an NFC tag is present
              NfcTag tag = nfc.read(); // read the NFC tag
              scannedUID = tag.getUidString(); // get the NFC tag's UID
              if(myUID.compareTo(scannedUID) == 0) // compare the NFC tag's UID with the correct tag's UID (a match exists when compareTo returns 0)
              {
                // the scanned NFC tag matches the saved myUID value
                Serial.println("Correct tag/key");
                blinkLed(greenLedPin,200,4); // blink the green led
                // put your here to trigger the unlocking mechanism (e.g. motor, transducer)
              }else{
                // the scanned NFC tag's UDI does not match the myUID value
                Serial.println("Incorrect tag/key");
                blinkLed(redLedPin,200,4); // blink the red led
                // DO NOT UNLOCK! an incorrect NFC tag was used.
                // put your code here to trigger an alarm (e.g. buzzard, speaker) or do something else
              }
              // return to the original state
              setFlag(FLAG_NONE);
              reset_PN532_IRQ_pin();
           }else{
             // a tag was not present (the IRQ was triggered by some other action)
             setFlag(FLAG_NONE);
           }
       break;
       default:
         // do any other stuff for flags not handled above
       break;
    }
}

/*
* Name: setFlat
* Description: used to set actions/flags to be executed in the loop(void) function
* Parameters:
*        int flag - the action/flag to store
* Returns: void
*/
void setFlag(int flag)
{
  flags = flag;
}

/*
* Name: getFlag
* Description: used to get the present flag/action
* Parameters: void
* Returns: int - the flags variable. The action/flag set by setFlag
*/
int getFlag()
{
  return flags;
}

/*
* Name: irq
* Description: Interrupt service routine (ISR). This function will be executed whenever there is a falling edge on digital pin 2 (the interrupt 0 pin)
* Parameters: void
* Returns: void
*/
void irq()
{
  if(getFlag()==FLAG_NONE){
    setFlag(FLAG_IRQ_TRIGGERED);
  }
}
/*
* Name: reset_PN532_IRQ_pin
* Description: used to reset the PN532 interrupt request (IRQ) pin
* Parameters: void
* Returns: void
*/
void reset_PN532_IRQ_pin()
{
  nfc.tagPresent();
}

/*
* Name: blinkLed
* Description: used to toggle a pin to blink an LED attached to the pin
* Parameters:
*      ledPin - the pin where the led is connected to
*      delayTime - the time in milliseconds between HIGH and LOW
*      times - the number of times to toggle the pin
* Returns: void
*/
void blinkLed(int ledPin,int delayTime,int times)
{
  for(int i=0;i<times;i++){
    digitalWrite(ledPin,HIGH);
    delay(delayTime);
    digitalWrite(ledPin,LOW);
    delay(delayTime);
  }
}
It turns out that this code works very well with the UNO, as expected,
the tags are read correctly. On the other hand, on the MEGA,
the chip is detected but no reading of the tags is carried out.

It seems to me that this code should work on both cards without modification?

Any help will be appreciate.
Forgive my english and thanks to gTrad :roll:
Hi,

If you are trying to run this sketch on mega then you need to change the SPI pins of Mega that is different from the SPI pins of Uno. See the tutorial below for the SPI interface of mega.

https://tronixstuff.com/2011/05/13/tut ... -spi-bus/

Also you need use the CS pin of mega in this line of code.
Instead of this:

Code: Select all

PN532_SPI interface(SPI, 9); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10
You need to use this:

Code: Select all

PN532_SPI interface(SPI, 53); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10
I hope it will help you.

Post Reply