Skip to main content

Using NFC Tools to Read/Write NFC tag

What is NFC?NFC Tools?

NFC (Near Field Communication)Tools is aan short-rangeapp wireless technology thatwhich allows two electronic devicesyou to communicateread, when they are within a few centimeters of each other. It’s most commonly used in contactless payments, digital keycards, smart tags,write and tap-to-shareprogram features.tasks on your NFC works by using magnetic field inductiontags and doesn’tother requirecompatible pairingNFC like Bluetooth — just a simple tap is enough to exchange data.chips.

InSimple thisand tutorial,intuitive, weNFC areTools usingcan anrecord iPhonestandard to tapinformation on theyour NTAG203NFC totags open up a link. This tutorial is more intensive andwhich will be morecompatible usefulwith ifany NFC device. For instance, you havecan othereasily interactive components instore your projectcontact involvingdetails, Arduino.an URL, a phone number, your social profile or even a location.

If

you only want a simple read and write function, we

We have another tutorial which shows you how to read and write an NFC tag using the appArduino, NFCwhich tools.

is

Wiringmore intensive and Library

useful

Weif will be using DFRobot PN532 module, please refer to this tutorial. We will be using Adafruit_PN532 library, weyou have aother tutorialinteractive oncomponents howin toyour installproject ainvolving library here.Arduino..

Types of Tag

In this tutorial, we are using NTAG203, NTAG215 or NTAG216 should work fine as well. You cannot use MIFARE Classic tag or card.

CodeSteps

  1. Download the app, it is available for Writingboth DataiPhone
    and Android.
  2. Choose #includeWrite. <Wire.h>nfcTools-1.PNG #include
  3. <Adafruit_PN532.h>
  4. Add #definea SDA_PINrecord. A4nfcTools-2.PNG #define
  5. SCL_PIN
  6. A5 Adafruit_PN532 nfc(SDA_PIN, SCL_PIN); //your url const char *url = "youtube.com"; // Keep short dueURL/URI to 144open bytea limitlink, &there omitare other functions that you can explore. nfcTools-3.PNG
  7. Put in your link. nfcTools-4.PNG
  8. Hold the "https://" part uint8_t urlPrefix = 0x01; // 0x01 = http://www. void setup(void) { Serial.begin(115200); Serial.println("Starting NFC writer with Adafruit PN532"); nfc.begin(); uint32_t versiondata = nfc.getFirmwareVersion(); if (!versiondata) { Serial.println("Didn't find PN532 board"); while (1); } nfc.SAMConfig(); // configure board to read RFID Serial.println("Waiting for an NFC tag..."); } void loop(void) { uint8_t uid[] = { 0 }; uint8_t uidLength; if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) { Serial.println("Tag detected!"); // Build NDEF URI record uint8_t urlLength = strlen(url); uint8_t payloadLength = 1 + urlLength; // Prefix + URL uint8_t ndef[] = { 0xD1, // MB, ME, SR, TNF=0x01 (well-known) 0x01, // Type Length = 1 payloadLength, // Payload Length 0x55, // Type = 'U' urlPrefix // URL Prefix }; uint8_t messageLength = sizeof(ndef) + urlLength; uint8_t totalLength = messageLength + 3; // TLV: 0x03 len + msg + 0xFE uint8_t full[totalLength]; full[0] = 0x03; // NDEF Message TLV tag full[1]near =your messageLength;phone //and Lengthpress ofWrite. NDEFnfcTools-5.PNG message
  9. memcpy(&full[2],
ndef, sizeof(ndef)); memcpy(&full[2 + sizeof(ndef)], url, urlLength); full[totalLength - 1] = 0xFE; // Terminator TLV // Write to tag starting at page 4 int page = 4; for (int i = 0; i < totalLength; i += 4) { uint8_t buffer[4] = {0x00, 0x00, 0x00, 0x00}; for (int j = 0; j < 4 && (i + j) < totalLength; j++) { buffer[j] = full[i + j]; } if (!nfc.ntag2xx_WritePage(page, buffer)) { Serial.print("Failed writing to page "); Serial.println(page); return; } page++; } Serial.println("Wrote NDEF URL to NTAG203 successfully!"); delay(5000); // prevent immediate re-trigger } }