Arduino Day Special: Getting started with RFID tags

No readers like this yet.
Gift box opens with colors coming out

Opensource.com

RFIDs—we use them every day. With every visit to the supermarket, public library, bookstore, or department store, we handle objects that have an RFID tag, which stands for Radio Frequency Identification. For one, these small tags make it easier for shop owners to keep inventory by tracking the flow of items as they're brought in, moved around, and purchased. RFID tags are usually composed of a small electronic chip that can store a few thousand bytes and an antenna that commonly takes the shape of a tight spiral—sometimes squared, sometimes circular.

This article is about how I tinkered with the Adafruit PN532 controller shield to read and write RFID tags.

Pictures of multiple types of RFID tags

To read and write information onto an RFID tag, you need a device composed of an antenna, radio circuitry, and a few digital electronic components. That's exactly what the Adafruit PN532 controller shield is. It attaches to an Arduino board that provides the programming logic for reading or writing data.

RFID / NFC Shield PN532 from Adafruit

History: RFID readers have a colorful history that dates back to the Cold War, when inventor Leon Theremin conceived a passive listening device known as "The Thing" that allowed Soviet intelligence to listen to conversations in the U.S. Ambassador's office in Moscow from 1945-1952.

The hardware

To start our exploration of the RFD/NFC shield, we need the following items:

Adafruit provides a nice starting guide for assembly and testing of the shield. I started by soldering connectors into the shield:

Soldering connectors into the RFID/NFC shield.

Only then did I realize that these connectors should be soldered on the other side of the shield. Undeterred by the error, I used one side of the connectors and soldered jumping cables to wire the other side. As usual, this mishap was a learning opportunity—I realized that the shield only really needed five wires and that most of the connectors are really for mechanical support. The essential wires are:

  • Two wires for the Analog 4 and 5 connectors, support the I2C communications protocol
  • The digital line 2 of the Arduino is connected to an interruption line from the shield to notify the Arduino when an RFID tag is within reading range of the shield's antenna
  • Two more wires bring power (5V and GND)

Wiring of the shield after mishap.

The software

Having (clumsily but surely) taken care of the wiring, I moved on to the software stage.

Following the quick start guide instructions, I cloned the GitHub repository of the Adafruit-PN532 library. I then renamed the directory as "Adafruit_PN532"  (the library import feature of the Arduino IDE does not accept "-" symbols, but accepts "_"). To import the library, I used the "Sketch" -> "Import Library..." -> "Add Library..." menu options. After importing, you can see the examples by following the "File" -> "Examples" -> "Adafruit_PN532" menu options, as illustrated in the image below.

Importing Adafruit PN532 Library into Arduino IDE.

I opened the readMiFare example in the IDE, and given that I used the shield with the I2C protocol, I made the following modifications in the code, as indicated by the starting guide:

Commented out the line:

// Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

Uncommented the line:

Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

I then saved the modified program with a new name: "readMiFare_for_Shield." I then used the USB cable to connect the Arduino board to a Linux laptop (Windows or Mac could be used as well).

A few seconds after connecting the cable, a new serial device appeared in the "/dev" directory under the name "/dev/ttyACM0" or "/dev/ttyACM1." At that point, I launched the Arduino IDE, and selected that serial device under the menu "Tools" -> "Serial Port." I then compiled the program with the "CTRL+R" shortcut and uploaded it to the Arduino board with the "CTRL+U" shortcut.

This specific example is going to initialize the board and then wait for RFID tags to get in the reading distance range of the antenna, at which point it will read the header information from the tag. The output can be seen by opening the serial monitor window with the shortcut "CTRL+SHIFT+M," and will look like the picture below:

Arduino IDE while reading an RFID tag.

Physical set up of the shield while reading an RFID tag.

Writing an RFID tag

I used the example mifareclassic_formatndef to format an RFID MiFare tag and store in it a short string that could be read by the NFC (Near Field Communication) of a cell phone or tablet. I opened the example in the Arduino IDE, commented out the line for the break out, and uncommented the line for the shield with I2C connection.

Arduino IDE while writing an RFID tag.

When running the example, you can see the success message in the serial monitor (right window in the figure above).

Physical set up of the shield while writing an RFID tag.

Conclusion

The Adafruit PN532 NFC/RFID controller shield for Arduino is a pretty fun component to play with. The instructions provided are very clear and detailed, but one has to pay close attention at positioning the connectors (see my rookie mistake above). After that, though, soldering is straightforward. One could also just go for wiring the five essential connectors, at the expense of ending up with a less mechanically robust setup.

Using the software examples was pretty easy and just required a couple of lines of modifications that are clearly explained in the instructions.

This is a great kit for getting familiar with RFID and NFC. And it's a must-have for educational projects and makerspaces. It's also a cool tool to have at hand for creating SWAG tags for your next community event.


Adafruit Industries provided the open hardware in this article. After the author tinkered with the hardware, we sent it back to Adafruit to make someone else happy.

User profile image.
Luis Ibáñez works as Senior Software Engineer at Google Inc in Chicago.

2 Comments

RFID is a good idea for things like inventory systems. However it should never be used for any application where personally identifiable information is stored. Why? Because RFIDs have been sucessfully read from several feet away from the tag, making it possible for information stored in the tag to be stolen without the owner's knowlege.

Nonya,

Thanks for pointing out the drawbacks and risks of storing personal information in RFID tags. I should have included that in the article. Your warning applies also in general to any critical information that one may want to control access to.

RFID tags are better used in the context where other layers of security and access have been put in place (for example, encryption and/or physical security).

Thanks for commenting.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.