Hello,
I would like to read a card with PN532 NFC card reader via interrupt , because my Arduino does a lot of other things. I use this NFC module: (https://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=90_93&products_id=2271) with an Arduino Mega. Basically the NFC module has an IRQ pin which is HIGH (if you set SAM with 14 01 14 01 as you can read that at page 89), and I get the answer 15. If a card is presented, then the IRQ goes to LOW, but in my case the IRQ voltage never changes , it's always approx. 3,2V. I supply the NFC board with a different power supply line which has approx. 5V, I connected the GND of the power supply line with arduino's GND. If I disconnect the IRQ and the pin 2 between the arduino and the NFC module, then I get and iterrupt, and after that the interrupt always triggered (obviously the interrupt function is triggered, because I use FALLING interrupt condition), after that my interrupt function is always called, and it's like the function is called from void loop() . Here is my code: https://pastebin.com/gYuQ0kfQ
To tell the truth, I do not really know what could cause the problem. Maybe PN532 IRQ voltage is for 3,3V logic level ? Because Arduino has 5V logic level. May I use logic level converter for IRQ pin?
Here is my updated code:
#include "emulatetag.h"
#include "NdefMessage.h"
#include <avr/wdt.h>
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
//MEGA
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
PN532_SPI pn532spi(SPI, 53);
//UNO
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
PN532_SPI pn532spi(SPI, 10);
#endif
#define PN532IRQPIN (2)
volatile boolean cState = false;
PN532 nfc(pn532spi);
void cardreading();
void setup() {
// put your setup code here, to run once:
//pinMode(PN532IRQPIN, INPUT);
//analogWrite(PN532IRQPIN, HIGH);
Serial.begin(115200);
Serial.println("\nHello!");
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);
nfc.SAMConfig();
//pinMode(PN532IRQPIN, INPUT_PULLUP);
//nfc.setPassiveActivationRetries(0x00);
Serial.println(cState);
attachInterrupt(digitalPinToInterrupt(PN532IRQPIN), cardreading, FALLING);
//attachInterrupt(0, cardreading, FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
if(cState)
{
Serial.println("Interrupted");
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)
//nfc.inListPassiveTarget();
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);
}
cState = false;
Serial.print("OUT: ");
Serial.println(cState);
}
}
void cardreading()
{
cState = true;
}