How To Use NFC Shield With Arduino and Demo Code
How To Use NFC Shield With Arduino and Demo Code
Demo Code?
I) Hardware Installation
1) Connect NFC Shield to Arduino as shown below.
2) Compile and upload the example sketch provided.
3) Hold the MIFARE Card near the antenna. The NFC Shield will read the
passive id data.
4) Hold the MIFARE Tag near the antenna. The NFC Shield will read the passive
id data
II) Programming
The PN532 software library for NFC Shield is derived from Adafruits PN532
Library.The original library provides API for reading Passive Target ID of Mifare
Card/Tags. This is enough for card/tag identification purpose. We have added
APIs for authentication, reading from and writing to Mifare Cards/Tags. The
software library only provides low level functionality. Users have to implement
NFC application layer(if required).
The PN532 software library should be inculded in the Arduino Include folder of
the Arduino software program before using the following code example
Include the PN532 software library in the include folder of the arduino
program
#include <PN532.h>
/*
* Corrected MISO/MOSI/SCK for Mega from Jonathan Hogg
(www.jonathanhogg.com)
* SS is the same, due to NFC Shield schematic
*/
#define SS 10
#if defined(__AVR_ATmega1280__) || defined
(__AVR_ATmega2560__)
#define MISO 50
#define MOSI 51
#define SCK 52
#else
#define MISO 12
#define MOSI 11
#define SCK 13
#endif
void setup(void) {
Serial.begin(9600);
nfc.begin();
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);
}
}
SCK 13
MOSI 11
SS 10
MISO 12
begin()
begin() method has to be called to initialize the driver.
Usage:
nfc.begin();
boolean SAMConfig(void)
This API invokes the SAMConfiguration command of PN532 and sets it to
Normal Mode. SAMstands for Security Access Module (i.e the PN532 system).
PN532 system can work in Normalmode, Virtual Card mode, Wired Card mode
and Dual Card mode.
Usage:
nfc.SAMConfig(); // Call this before any read/write operation
Future Electronics Egypt Ltd. (Arduino Egypt).
cardnumber can be 1 or 2
Usage:
uint8_t keys[]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; // default key
of a fresh card
nfc.authenticateBlock(1, id ,3,KEY_A,keys); ////authenticate
block 3, id is 32-bit passive target id.
cardnumber can be 1 or 2
Usage:
uint8_t block[16];
nfc.readMemoryBlock(1,3,block); //Read can be performed only
when authentication was successful.
cardnumber can be 1 or 2
Usage:
uint8_t writeBuffer[16];
for(uint8_t ii=0;ii<16;ii++)
{
writeBuffer[ii]=ii; //Fill buffer with 0,1,2....F
}
nfc.writeMemoryBlock(1,0x08,writeBuffer); //Write writeBuffer[]
to block address 0x08. Read can be performed only when
authentication was successful.
readAllMemoryBlocks.pde
Compile and upload readAllMemoryBlocks.pde example provided with the
library. This sketch reads the complete memory of a MIFARE Standard card
using default authentication keys. The output gives typical memory layout of
fresh MIFARE Standard card.
Future Electronics Egypt Ltd. (Arduino Egypt).
Output