NFC Shield V2 with Arduino MEGA

Good evening,

I have this Shield NFC( 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]#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
// 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

Serial.begin(9600); // start serial comm
Serial.println("NDEF Reader");
nfc.begin(); // begin NFC comm
// turn off the LEDs

// 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
   case FLAG_IRQ_TRIGGERED: // the interrupt pin has been triggered
       Serial.println("Interrupt Triggered");
       if (nfc.tagPresent())
         // an NFC tag is present
          NfcTag tag =; // 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)
            // 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
         // a tag was not present (the IRQ was triggered by some other action)
     // do any other stuff for flags not handled above



  • 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()
  • Name: reset_PN532_IRQ_pin
  • Description: used to reset the PN532 interrupt request (IRQ) pin
  • Parameters: void
  • Returns: void
    void reset_PN532_IRQ_pin()


  • 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++){

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, I want to know how do you connect those two boards? Could you please post a picture?


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.

<LINK_TEXT text=" … -spi-bus/

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

Instead of this:
</s><i> </i>PN532_SPI interface(SPI, 9); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10 <e>

You need to use this:
</s><i> </i>PN532_SPI interface(SPI, 53); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10 <e>

I hope it will help you.