NFC/RFID 實驗在Arduino 上其實也很簡單, 這次利用PN532相容板來做實驗, 此塊板子支援 I2C 和 SPI介面, 我就來用 SPI 跟Arduino連接! 參考下圖:
Arduino PN532
D10 ------> SS
D11 ------> MOSI
D12 ------> MISO
D13 ------> SCK
5V -------> 5V
GND ------> GND
Sketch 範例程式:
#include <arduino.h>
#include <PN532.h>
#define SCK 13
#define MOSI 11
#define SS 10
#define MISO 12
PN532 nfc(SCK, MISO, MOSI, SS);
void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");
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);
Serial.print("Supports "); Serial.println(versiondata & 0xFF, HEX);
// configure board to read RFID tags and cards
nfc.SAMConfig();
}
void loop(void) {
uint32_t id;
// look for MiFare type cards
id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
if (id != 0) {
Serial.print("Read card #"); Serial.println(id);
}
}
PN532 arduino library 下載點 here.
很簡單! 拿起 NFC tag 感應一下就會跑出 Card id.