0% found this document useful (0 votes)
180 views11 pages

Study and Implement RFID, NFC Using Arduino

This document discusses using RFID and NFC technologies with Arduino. It provides instructions on setting up an RFID reader circuit with an Arduino, downloading the necessary RFID library, and writing code to read RFID tags. It then discusses using similar techniques to read and write data to NFC tags. The code examples allow printing the tag UID and reading/writing multiple records of text and URI data to NFC tags.

Uploaded by

vidhya associate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views11 pages

Study and Implement RFID, NFC Using Arduino

This document discusses using RFID and NFC technologies with Arduino. It provides instructions on setting up an RFID reader circuit with an Arduino, downloading the necessary RFID library, and writing code to read RFID tags. It then discusses using similar techniques to read and write data to NFC tags. The code examples allow printing the tag UID and reading/writing multiple records of text and URI data to NFC tags.

Uploaded by

vidhya associate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Study and Implement RFID, NFC using Arduino

RFID tagging is an ID system that uses small radio frequency identification devices for
identification and tracking purposes. An RFID tagging system includes the tag itself, a read/write
device, and a host system application for data collection, processing, and transmission.
In simple words an RFID uses electromagnetic fields to transfer data over short distances. RFID
is useful to identify people, to make transactions, etc…
You can use an RFID system to open a door. For example, only the person with the right
information on his card is allowed to enter. An RFID system uses:
>> tags attached to the object to be identified, in this example we have a keychain and an
electromagnetic card. Each tag has his own identification (UID).

Keychain and Electromagnetic card are commonly used tags

>> two-way radio transmitter-receiver, the reader, that sends a signal to the tag and read its
response
Radio transmitter receiver

Basic Specifications:
 Input voltage: 3.3V
 Frequency: 13.56MHz
Now, before typing out the necessary code, you need to download the necessary library for this
sensor from this repository.
Extract the contents from the zip folder "rfid-master" and add this library folder under the
existing libraries of Arduino.
After doing so, restart your ArduinoIDE.
Now, our Arduino is ready to take commands and execute accordingly.
The Arduino Code has been uploaded at the end of this tutorial. Compile the code and eliminate
"typo" errors (if any).
Now, its time to connect our Arduino with the RFID reader. Refer to the PIN wiring below,as
well as the Connection schematic diagram for easy reference.
PinWiring to Arduino Uno
SDA------------------------Digital 10
SCK------------------------Digital 13
MOSI----------------------Digital 11
MISO----------------------Digital 12
IRQ------------------------unconnected
GND-----------------------GND
RST------------------------Digital 9
3.3V------------------------3.3V (DO NOT CONNECT TO 5V)
Reading data from an RFID tag
After having the circuit ready, go to File > Examples > MFRC522 > DumpInfo and upload the
code. This code will be available in Arduino IDE (after installing the RFID library).
Then, open the serial monitor. You should see something like the figure below:

Approximate the RFID card or the keychain to the reader. Let the reader and the tag closer until
all the information is displayed.
This is the information that you can read from the card, including the card UID that is
highlighted in yellow. The information is stored in the memory that is divided into segments and
blocks as you can see in the previous picture.
You have 1024 bytes of data storage divided into 16 sectors and each sector is protected by two
different keys, A and B.
Write down your UID card because you’ll need it later.
Upload the Arduino code that has been suffixed here.
Demonstration
Approximate the card you’ve chosen to give access and you’ll see:
If you approximate another tag with another UID, the denial message will show up:

In case of any queries please comment below.


CODE
Arduino code for RFID readerArduino
In the piece of code above you need to change the if (content.substring(1) == “REPLACE WITH YOUR UID”) and type
the UID card you’ve written previously.
/*
*
* All the resources for this project: https://www.hackster.io/Aritro
* Modified by Aritro Mukherjee
*
*
*/

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();

}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "BD 31 15 2B") //change here the UID of the
card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
delay(3000);
}

else {
Serial.println(" Access denied");
delay(3000);
}
}
SCHEMATICS
Connection schematic diagram

Near field communication(NFC) 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.

Reading an NFC Tag


These header files are extremely important and the project won’t work without
them. Afterwards you want to write the following code.

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);

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:

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. The video below shows how my
Nexus 5 reads the tag and displays the messages.

Writing on an NFC Tag


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.

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);

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!”

You might also like