Arduino-based NFC Tags
Published on 2/20/2017 8:38:04 AM
Description
<style>.e_editor{font:14px/24px Arial,'microsoft yahei','Times New Roman','b8bf53';}.e_editor div,e_editor p,e_editor td,e_editor th,e_editor li{font-weight:inherit;font-size:inherit;font-style:inherit;font-family:inherit;}.e_editor ul{margin:-10px 0 20px;}.e_editor li{padding:5px 0;}.e_table{width:96%;border-collapse:collapse;empty-cells:show;}.e_table th,.e_table td{padding:5px;border:2px solid #eee;}.e_img{padding:10px 0;text-align:center;}.e_p{line-height:20px;padding:0 0 20px;text-indent:0em;}</style> <div class="e_editor"> <div class="e_p"> <strong>What's NFC?</strong> </div> <div class="e_p"> Near field communication are protocols that electronic devices use to communicate and transfer data between each other. Near field communication devices have to be very near to each other, usually between 10cm, but the range can vary depending on the device that is transmitting and the size of the tag. NFC tags require no power input whatsoever. They use magnetic induction between two between two small loop antennas. The tags these days carry between 96 and 4,096 bytes of information. </div> <p> <br /> </p> <p> <strong>Parts List</strong> </p> <p> <br /> </p> <ul> <li> Arduino Uno R3 </li> <li> Adafruit PN532 RFID/NFC Shield </li> <li> Arduino IDE (Integrated Development Environment) </li> <li> Rewritable NFC Tags </li> </ul> <p> <br /> </p> <div class="e_img"> <img src="https://file.allpcb.com/bbs/17/03/15/141341757.jpg" alt="" width="500" height="375" title="" align="" /><br /> </div> <div class="e_p"> It is important that the NFC Tags are rewritable, otherwise this won't work. </div> <div class="e_p"> To test whether what we wrote on the tags was successful, we can test with the Arduino or with an NFC-enabled phone. Most smartphones running Android should be able to read NFC tags, and I will be testing with a Nexus 5. Unfortunately for iPhone users, the only iPhones that supports NFC are the iPhone 6 and the 6s, but they do not support NFC tag reading so just use the Arduino to test out what your tag has written on them. iPhones only use their NFC capability for apple pay, therefore you cannot use them to read tags or anything else. </div> <div class="e_p"> Once we have all the parts together, we need to install two libraries that will make the reading and writing on tags possible. The libraries are don/NDEF and Seeedstudio’s, the one we will be mainly using is don’s since Seeedstudio’s library is used if you have the Seeedstudio NFC shield. We will install it as a library just in case. You have to download and install both libraries using Arduino’s “Add .zip Library” under Sketch >> Include Library. Make sure to install both libraries separately and under the default Arduino directory otherwise you will have compiling errors. </div> <div class="e_p"> Start the IDE and you should have a new sketch file. Save your new file under any name that you chose, like “Read NFC Tag.” The first files that you will have will be the header files and they will be the following. They will go before the void setup. </div> <div class="e_p"> <strong>Reading an NFC Tag</strong> </div> <div class="e_p"> These header files are extremely important and the project won’t work without them. Afterwards you want to write the following code. </div> <div class="e_p"> void setup(void) { Serial.begin(9600); Serial.println("NFC TAG READER"); // Header used when using the serial monitor nfc.begin(); } void loop(void) { Serial.println("\nScan your NFC tag on the NFC Shield\n"); // Command so that you an others will know what to do if (nfc.tagPresent()) { NfcTag tag = nfc.read(); Serial.println(tag.getTagType()); Serial.print("UID: ");Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag if (tag.hasNdefMessage()) // If your tag has a message { NdefMessage message = tag.getNdefMessage(); Serial.print("\nThis Message in this Tag is "); Serial.print(message.getRecordCount()); Serial.print(" NFC Tag Record"); if (message.getRecordCount() != 1) { Serial.print("s"); } Serial.println("."); // If you have more than 1 Message then it wil cycle through them int recordCount = message.getRecordCount(); for (int i = 0; i < recordCount; i++) { Serial.print("\nNDEF Record ");Serial.println(i+1); NdefRecord record = message.getRecord(i); int payloadLength = record.getPayloadLength(); byte payload[payloadLength]; record.getPayload(payload); String payloadAsString = ""; // Processes the message as a string vs as a HEX value for (int c = 0; c < payloadLength; c++) { payloadAsString += (char)payload[c]; } Serial.print(" Information (as String): "); Serial.println(payloadAsString); String uid = record.getId(); if (uid != "") { Serial.print(" ID: ");Serial.println(uid); // Prints the Unique Identification of the NFC Tag } } } } delay(10000); }<br /> <br /> <p> Once you have saved and uploaded this code unto your Arduino with the shield attached, you can begin testing what messages your tags have, if any. When you upload the program to the Arduino, open the Serial monitor and you should see a message saying “NFC TAG Reader,” and below it instructions telling you to “Scan your NFC tag on your NFC Shield.” When I do that I get this on my serial monitor: </p> <p align="center"> <img src="https://file.allpcb.com/bbs/17/03/15/141402365.jpg" alt="" width="500" height="264" title="" align="" /> </p> <p> Notice that it gives the the unique identification of the NFC tag and it tells me what information I have written on the tags. On this particular tag I have a simple welcome message and a link to the Arduino Twitter. The Arduino is successfully reading the info on my tag. </p> <p> </p> <h3> Writing on an NFC Tag </h3> <p> Now to be able to write a message on a tag, the process is similar except we are going to change the code a little bit. The header before void setup() will stay the same but this will be the code you want to write and upload to the Arduino. </p> <p> </p> void setup() { Serial.begin(9600); Serial.println("NFC Tag Writer"); // Serial Monitor Message nfc.begin(); } void loop() { Serial.println("\nPlace an NFC Tag that you want to Record these Messages on!"); // Command for the Serial Monitor if (nfc.tagPresent()) { NdefMessage message = NdefMessage(); message.addTextRecord("My First NFC Tag Write"); // Text Message you want to Record message.addUriRecord("http://allaboutcircuits.com"); // Website you want to Record message.addTextRecord("Way to Go, It Worked!"); // Ednding Message for you to Record boolean success = nfc.write(message); if (success) { Serial.println("Good Job, now read it with your phone!"); // if it works you will see this message } else { Serial.println("Write failed"); // If the the rewrite failed you will see this message } } delay(10000); } </div> <div class="e_p"> </div> <div class="e_p"> This Code is Saving three messages on the tag: an intro text saying “My First NFC Tag Write”, then a link to AllAboutCircuits and lastly an ending message saying “Way to Go, It Worked!” </div> <div class="e_p"> When I scan the tag on my phone, I now get the two messages and the link. You can change the code to say whatever you want on the messages and to direct you to another link. When making any changes make sure you check the tag info with the Arduino or a smartphone. Have fun working with NFC! </div> </div>
121
comment
All comments
5890
0
121
Rules about cashback: 1. Valid time: ALLPCB cashback activity will end on April 1st. 2. Capped amount: The capped amount of cashback for each account is $5,000. Each order can get a maximum of $2,000 cashback. That means every author can get $5,000 max. 3. Cashback range: The cashback activity only covers the corresponding PCB order. The order amount for other combined payment products will be invalid. 4. Clicking your own promotional link will be invalid. The same email address, shipping address, contact information, and phone number are all recognized as the same account. 5. ALLPCB has the final interpretation right of the cashback activity.
ALLPCB will donate 2% to the author for this promotion link.