Maker Portal

View Original

An Introduction to RFID with Arduino

“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”

See this content in the original post

See this content in the original post

We will be using an Arduino Uno board and an MFRC52213.56 MHz RFID reader in this tutorial, along with several RFID tags and cards. The MFRC522 communicates with the Arduino board via SPI, so we will need quite a few jumper wires to connect the two devices. We’re also interfacing with a Raspberry Pi 4 computer. The full parts list is given below:

  • Arduino Uno Board - $13.00 [Our Store]

  • MFRC522 RFID Kit (6-Tags + Fixture) - $18.00 [Our Store]

  • RFID Tags/Cards - $7.88 (10 fobs) [Amazon], $6.99 (10 cards) [Amazon]

  • 9x Male-to-Female Jumper Wires - $1.35 [Our Store]

  • Raspberry Pi 4 Computer - $69.99 (4GB) [Amazon], $119.99 (8GB Kit) [Amazon]

See this product in the original post

The wiring between the MFRC522 and Arduino Uno board is given below for SPI connection:

See this content in the original post

See this content in the original post

The Arduino IDE’s Library Manager has a widely used RFID library that is compatible with the MFRC522 chip. The library, conveniently titled MFRC522, can be downloaded directly from the library manager and the user can get started right away with the many example scripts available to the Arduino platform. Below is a short sequence that outlines the procedure for downloading the MFRC522 library:

We will be using these examples to read and write various information onto and from RFID cards with unique identifiers (UIDs).

Info Dump Test from RFID Tag:

The first checks we can do for verification of wiring, the Arduino library, and the validity of the RFID tags is to upload the “DumpInfo” code from the examples list. Upon uploading, assuming the wiring is correct and there are no errors, the user should place a tag within 4 inches of the MFRC522 scan area (the wave-like drawing on the MFRC522 board) and look for a print out on the Arduino serial port that looks similar to the following:

We can see from the info dumpy that the tag we are using is a MIFARE 1KB tag, which we can quantify by the multiplication of 64 blocks x 16 bytes per block = 1024 = 1KB. This is a standard classic MIFARE tag, which are used in low-security systems such as public transportation, schools and universities, and employee office access.

Below is a great MIFARE Classic Resource:

- "MIFARE Classic 1K-Mainstream contactless smart card IC for fast and easy solution development." Rev. 3.2 — 23 May 2018, by NXP.

See this content in the original post

There is also a detailed chart produced by NXP that explains many of the components printed by the Arduino code:

Courtesy of NXP, from MIFARE Classic EV1 1K

There are a few more pieces of information contained in the chart above that we can use to comprehend the output from the Arduino serial monitor. From our serial printout, the Key B for our tag sectors are FF FF FF FF FF FF, which is somewhat of a manufacturer default for classic MIFARE tags. Additionally, the NXP document goes into the details of the relationship between keys and access bits, however, we will not spend time on that as it can get quite complex. One final note on the info dump - we can identify crucial information about each card based on the zeroth block of the zeroth sector, known as the manufacturer data. In our card printed above, we can see that the first four bytes of block 0 contains the UID of the card.

Using the table provided by NXP and the definitions above, we can identify a few key parameters for our specific tag (from the Arduino printout above):

  • UID - CE 5B F4 3C

  • SAK - 08

  • Key B - FF FF FF FF FF FF

In the next section, we will explore a few routines for altering these default values and seeing just how far we can push the customization of these RFID tags using the MFRC522 module.


See this content in the original post

We are going to attempt to change the Unique Identifier (UID) of each tag being placed in front of the MFRC522 active area. The MFRC522 example code ‘ChangeUID’ allows us to do this by giving the user the power to change the UID to a custom 4-byte address. We’re going to change the address of the tag printed out in the previous section, to a completely random address of “F3 45 1B 0C” for testing. If we place the same tag in front of the MFRC522 scan area, we get the following output, which we can compare with the previous output:

Previous tag scan output with factory defaults

Tag scan after changing the UID with MFRC522

We can see that the only information that changed was the first five bytes of the zeroth block of sector 0. The first four bytes have been changed to the desired UID, while the fifth byte appears to be either random or specified by the write action (we have not identified this byte designation in the library).

See this content in the original post

We tried several times to write new UIDs to our black tags, but we were unable. This was expected, due to the specification of read-only, however, we figured an attempt wouldn’t hurt!

Writing Data to RFID Tags

Next, we’ll attempt to write specific data to each tag to see what information we can store. Each sector (except sector 0) has three available blocks for storing data (4-blocks per sector, minus 1 for keying). Thus, we can store up to 48 bytes of data in each sector. This can leads to the storing of names, dates, location information, product information - the possibilities are expansive. We can start with the example in the Arduino MFRC522 library called ‘rfid_write_personal_data’ which will ask the user to input a first and last name for storing in the Electrically Erasable Programmable Read-Only Memory (EERPROM), also known as the 16-sectors of data.

Upon opening the serial monitor after uploading ‘rfid_write_personal_data’ the user should place their RFID tag in front of the MFRC522 active area. The serial monitor will ask for two inputs:

  1. Type Family name, ending with #

  2. Type First name, ending with #

The user must input a name followed by the # symbol. For example:

  • Smith#

  • John#

The resulting inputs will be saved in memory and should output the following if successful:

If we now upload the ‘rfid_read_personal_data’ we can see how the personal data is returned to us based on our inputs above:

And this verifies that our data is saved to the RFID tag. We can see how the data is saved by again returning to the “DumpInfo” example and looking at the output:

If we were to copy sector 1 and convert it from hex to UTF-8 characters, we get the following output:

The same can be done for the first name as well (contained in sectors 4), though sector four contains carriage return and newline characters as the first two bytes. These two characters are due to the setting on our serial monitor (Both NL & CR). If we change the line setting to "No line ending” this disappears and we just get “John” and “Smith” as outputs for sectors 1 and 4. Users can also verify the hex to UTF-8 values by inserting the hex output to a hex conversion website (such as: https://www.rapidtables.com/convert/number/hex-to-ascii.html).

RFID Resources:

- Want, Roy. "An introduction to RFID technology." IEEE pervasive computing 5.1 (2006): 25-33.


See this content in the original post

In this first entry of the RFID tutorial series with Arduino, the wiring, testing, and simple customization of RFID tags was introduced with the MFRC522 RFID module. The MFRC522 is read and controlled via SPI using an Arduino Uno board, which has a dedicated Arduino library downloadable from the library manager. Several of the available examples were used to introduce and explore the basics of RFID with Arduino and understand some of the inner workings of the library and radio frequency identification as a technology. RFID terms such as sectors, blocks, and unique identifiers were discussed and used to understand some of the examples provided by the Arduino RFID library. Four different examples were used to test the various capabilities of the MFRC522 module, which involve changing the UID of writable RFID tags, dumping the information contained on all sectors of a tag, and writing/reading custom data from an RFID tag. This tutorial will be continued in upcoming tutorials, which further delve into customization of the data contained in an RFID tag.

See this content in the original post

See more in Arduino:

See this content in the original post