NFC (Near-field communication) is a protocol that enable 2 devices to communicate typically smart phone device and NFC tags / devices to establish communication by getting them together range of 4 cm at 1.6 inch defends of usage and antenna circuitry. NFC device are used in contactless payment system, security, marketing, transportations, Bluetooth fairing. Etc. NFC also can be found on your mobile phones that can act as electronic identity documents and keycards working on a low speed connection.
Each NFC device can work in 3 modes, can be NFC Card Emulation it means that NFC device such as smart phones can act as smart cards allowing the smart phones to transact as payment or ticketing. NFC devices also allows you two write / read information stored on NFC tags in embedded in labels, tickers, passports or smart posters. NFC PTP (Pear to Pear) allows you to NFC enabled devices to communicate with each other devices to exchange information.
The diagram below demonstrate how to used NFC devices with your microcontrollers. As you can see the diagram below there are two options the first option is wired using i2C (2 Wire Communication), second option is wired using SPI (Serial Peripheral Interface) below are the component required for this test.
Required Components
- Arduino Microcontroller, ESP8266 12, 12E, ESP8266 NodeMCU, ESPDuino, ATMEGA328 16/12, ATMEGA32u4 16/8/ MHz, ESP8266, ATMEGA250 16 MHz, ATSAM3x8E, ATSAM21D, ATTINY85 16/8 MHz (Note: The Diagram below is using NANO. (please refer to the respective pin-outs)
- NFC Module / NFC Shield / NFC MCU Integrated
- Solder Less Bread Board
- Jumper Wires / DuPont Wires
Wiring Guide
When you save and upload the code below unto your MCU w/d the module/shield attached you can now begin testing and show the message burn-in your tags, when you upload the code into the MCU then opening the serial monitor you will see the message 14CORE | NFC – i2C Test Code TAG Reader see below code to write data into your TAGS to read from your smart phone.
Source Code for i2C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/*Code libraries and more examples can be found below */ #include <Wire.h> // #include <PN532_I2C.h> //Set PN532 i2C Library can be downloaded below. #include <PN532.h> //Library can be dowloaded below #include <NfcAdapter.h> //Library can be dowloaded below PN532_I2C pn532(Wire); // Set PN532 runs on i2C Procotol NfcAdapter nfc = NfcAdapter(pn532); void setup(void) { Serial.begin(9600); Serial.println("14CORE | NFC - i2C Test Code TAG Reader"); Serial.println("Initializing please wait......."); delay(3000); nfc.begin(); } void loop(void) { Serial.println("Please wait while scanning any NFC device"); delay(2000); if (nfc.tagPresent()) { NfcTag tag = nfc.read(); Serial.println(tag.getTagType()); Serial.print("UID: ");Serial.println(tag.getUidString()); if (tag.hasNdefMessage()) //set if there is message in the tags { NdefMessage message = tag.getNdefMessage(); Serial.print("NFC TAG MSG: "); Serial.print(message.getRecordCount()); Serial.print("NFC Tag Data"); if (message.getRecordCount() != 1) { Serial.print("."); } Serial.println("Failed No NFC Device Found"); delay(1000); int recordCount = message.getRecordCount(); for (int i = 0; i < recordCount; i++) { Serial.print("NDEFRecord "); Serial.println(i+1); NdefRecord record = message.getRecord(i); int payloadLength = record.getPayloadLength(); byte payload[payloadLength]; record.getPayload(payload); String valString = ""; for (int c = 0; c < payloadLength; c++) { valString += (char)payload[c]; } Serial.print("INFO (as String): "); Serial.println(valString); String uid = record.getId(); if (uid != "") { Serial.print(" ID: "); Serial.println(uid); // print UID of the NFC Tag } } } } delay(10000); } |
Source Code for SPI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
/*You can download the code library below*/ #include <Adafruit_PN532.h> #include "Wire.h" #include "LiquidCrystal.h" // Optioanl #define SCK (13) #define MOSI (11) #define SS (10) #define MISO (12) Adafruit_PN532 nfc(SCK, MISO, MOSI, SS); void setup(void) { Serial.begin(9600); Serial.println("14CORE | NFC - SPI Test Code TAG Reader"); Serial.println("Initializing please wait......."); delay(3000); 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("Device Found PN5 Chip"); Serial.println((versiondata>>24) & 0xFF, HEX); Serial.print("Firmware version > "); Serial.print((versiondata>>16) & 0xFF, DEC); Serial.println((versiondata>>8) & 0xFF, DEC); nfc.SAMConfig(); //Set to read RFID tags Serial.println("Waiting for an ISO14443A Card ..."); } void loop(void) { 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) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { Serial.println("ISO14443A Card Detected > "); Serial.print(" UID Length: "); Serial.print(uidLength, DEC); Serial.println(" BYTES"); Serial.print(" UID VAL: "); nfc.PrintHex(uid, uidLength); if (uidLength == 4) { Serial.println(" MIFACE CLASSIC CARD > (4 byte UID)"); // Factory default keyA is 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF Serial.println("Authenticating block 4"); uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; //Default Key success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); if (success) { Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); uint8_t data[16]; /* If you wanted to write something at block 4 you can, uncomment */ // data = { 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0}; // success = nfc.mifareclassic_WriteDataBlock (4, data); success = nfc.mifareclassic_ReadDataBlock(4, data); if (success) { Serial.println("Reading Block 4:"); nfc.PrintHexChar(data, 16); delay(1000); } else { Serial.println("ERROR > Unable to read requested block"); } } else { Serial.println("ERROR > Authentication Failed Please Try Again."); } } if (uidLength == 7) { Serial.println("MIFARE LIGHT TAG > (7 byte UID)"); // Try to read the first general-purpose user page (#4) Serial.println("Getting page 4 > "); uint8_t data[32]; success = nfc.mifareultralight_ReadPage (4, data); if (success) { nfc.PrintHexChar(data, 4); delay(1000); } else { Serial.println("ERROR > Unable to read requested page"); } } } } |
Writing on NFC Tag Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
void setup() { Serial.begin(9600); Serial.println("NFC Tag Writer Test Code"); Serial.println("Initializing please wait....."); delay(2000); nfc.begin(); } void loop() { Serial.println("Place an Writable NFC Tag"); if (nfc.tagPresent()) { NdefMessage message = NdefMessage(); message.addTextRecord("14CORE | NFC TAG TEST"); // Text Message you want to write message.addUriRecord("https://www.14core.com"); // URL message.addTextRecord("UID: 0123456789"); // VAl 1 message.addTextRecord("SRV ID: 0123456789"); // Val 2 message.addTextRecord("KEY ID: 0123456789"); // Val 3 boolean success = nfc.write(message); if (success) { Serial.println("Read from your smart phone"); } else { Serial.println("NFC Writing failed"); } } delay(10000); } |
Downloads
- Download PN532 Code Library | Zip
- Download PN532 NFC Shield | Zip
- Download Adafruit PN532 Code Library | Zip
- Download PN532 Datasheet | PDF
- Download PN532 HSU | PN532 I2C | PN532 SPI
Hi, I need help.
Fail to compile “Source Code for i2C” and “Writing on NFC Tag Source Code”.
Cannot identify nfc.tagPresent() in the programs.
I wonder what is missing in the header portion #include ??????
I’m using Arduino UNO and my PC.
I appreciate your reply.
check if you include all required libraries, nfc.tagPresent()
#include PN532_I2C.h #include PN532.h #include NfcAdapter.h
Hi,
Just let me know how to compile above programs without errors.
I’m using Arduino uno.and PN532 reader/writer or RFID reaer/writer.and failed to compile
I copied the program to sketch , but failed to compile, hence no upload.
The nfc.tagPresent() and NfcTag tag = nfc.read() are undefined.
Please tell me what is missing in the header portion.
Thank for your help.