Copyright
Near Field Communication (NFC) for Embedded Applications
Agus Kurniawan
1st Edition, 2015
Copyright 2015 Agus Kurniawan
NFC logo image is copyright and trademark from NFC Forum, http://nfc-forum.org/our-work/nfc-branding/n-mark/ .
Table of Contents
Preface
This book was written to help anyone want to get started with NFC module programming on Arduino and Raspberry Pi. It describes all the basic elements of the NFC module deploying and how to program with step-by-step approach. There are two study cases to illustrate how to work with NFC module.
Agus Kurniawan
Depok, August 2015
1. Preparing Development Environment
1.1 Near Field Communication (NFC)
Near field communication (NFC) is a set of standards for smart phones and similar devices to establish radio communication with each other by touching them together or bringing them into close proximity, usually no more than a few centimeters.
In this book, we focus on NFC development with targeting on Arduino and Raspberry Pi boards.
1.2 Getting Hardware
How to get NFC module?
There are many NFC hardware module. For testing I use two NFC modules, ITEAD PN532 NFC module and PN532 NFC RFID Module V3.
ITEAD PN532 NFC module. This module is based on PN532 chip and used for 13.56MHz near field communication. You can buy this module on this website, http://imall.iteadstudio.com/featured-product/im130625002.html .
PN532 NFC RFID Module V3. This module made in China. You can get it from eBay.
PN532 NFC/RFID controller breakout board from Adafruit, https://www.adafruit.com/products/364 .
You also can buy NFC module on your local electronics store.
1.3 Development Tools
For development, I use several development tools corresponding to embedded platform. You can use any code editor to develop embedded app for NFC.
1.4 NFC Cards
We need some electronic components to build our testing, for instance, Resistor, LED, sensor devices and etc. I recommend you can buy electronic component kit. You find them on your local electronics shops
2. NFC Programming for Arduino
This chapter explains how to work with NFC module on Arduino board.
2.1 Getting Started
We are going to read NFC cards and keys via NFC module on Arduino board. For testing, we build demos such as reading and writing data on NFC cards.
In this chapter, we access NFC module to Arduino via SPI. You should enable SPI access on NFC module. Itead PN532 Module should be set SET0 = L and SET1 = H to activate SPI communication.
2.2 NFC Library for Arduino
In this demo, I used NFC module from ITEAD Studio, http://imall.iteadstudio.com/featured-product/im130625002.html . You can download NFC library for Arduino on http://wiki.iteadstudio.com/ITEAD_PN532_NFC_MODULE .
After you got *.zip file, you can add it into Arduino Sotware by clicking menu Sketch -> Include Library -> Add .ZIP Library.
Navigate to your .zip library.
After that, you're ready to develop a program for NFC.
2.3 Wiring
We connect NFC module to Arduino via SPI. You can see NFC module pins on Figure below.
The following is our wiring:
- NFC SCK to Arduino Digital 13 (SCK)
- NFC MI to Arduino Digital 12 (MISO)
- NFC MO to Arduino Digital 11 (MOSI)
- NFC NSS to Arduino Digital 10 (SS)
- NFC GND to Arduino GND
- NFC 5V to Arduino VCC +5V
My implementation for hardware wiring, shown in Figure below.
2.4 Reading NFC Card
In this section, we build a simple app to read NFC card. Then, we read detail of inside card data. You can write these codes on Arduino software.
#include #include /*Chip select pin can be connected to D10*/ #define PN532_CS 10 PN532 nfc (PN532_CS); 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 (); // halt } Serial.print( "Found chip PN5" ); Serial.println((versiondata >> ) & 0xFF , HEX); Serial.print( "Firmware ver. " ); Serial.print((versiondata >> ) & 0xFF , DEC); Serial.print( '.' ); Serial.println((versiondata >> ) & 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; id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A); if (id != ) { Serial.print( "Read card #" ); Serial.println(id); Serial.println(); uint8_t keys[] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF }; // default key of a fresh card for ( uint8_t blockn = ;blockn < ;blockn ++ ) { if (nfc.authenticateBlock(, id ,blockn,KEY_A,keys)) //authenticate block blockn { //if authentication successful uint8_t block[]; //read memory block blockn if (nfc.readMemoryBlock(,blockn,block)) { //if read operation is successful for ( uint8_t i = ;i < ;i ++ ) { //print memory block Serial.print(block[i],HEX); if (block[i] <= 0xF ) //Data arrangement / beautify { Serial.print( " " ); } else { Serial.print( " " ); } } Serial.print( "| Block " ); if (blockn <= ) //Data arrangement / beautify { Serial.print( " " ); } Serial.print(blockn,DEC); Serial.print( " | " ); if (blockn == ) { Serial.println( "Manufacturer Block" ); } else { if (((blockn + ) % ) == ) { Serial.println( "Sector Trailer" ); } else { Serial.println( "Data Block" ); } } } } } } delay();}
Save this program as ArduinoNFC.
Don't forget to change targeted board to Arduino and its port.
Compile and upload this program into Arduino board.
Open Serial Monitor tool from Arduino software. Now you can swap your NFC card and see the program output on Serial Monitor tool. A sample output can be seen in Figure below.
2.5 Writing NFC Card
In this section, we try to write data into NFC card. For instance, we write data 0..F for 16 data.