Arduino RFID reader: A beginners guide to using an RFID reader with an Arduino Uno

Introduction

Arduino RDI reader MFRC522An Arduino RFID reader allows you to easily interact with RFID (radio-frequency identification) tags. RFID technology is used in many applications like contactless payment cards, electronic passports and identity cards, and object/animal identificatoin.

In this beginners guide, you will learn how to connect an RFID reader module to an Arduino Uno and read tag information from RFID cards and tags.

We will go over the components needed, how to wire up the circuit, example Arduino code to interface with the reader, and testing procedures. By the end, you'll have a basic understanding of working with RFID readers and will have built a simple Arduino RFID reader project.

The component is MFRC522. I've chosen the MFRC522 as it is one of the most common and affordable RFID reader modules compatible with the Arduino. It can read UID, type and other data from MIFARE RFID cards and tags.

What is an MFRC522

Arduino RFID kit for experimentRFID stands for Radio Frequency Identification. It's a technology that allows objects to be identified and tracked wirelessly using radio waves. An RFID system has two main components - RFID tags and an RFID reader.

The MFRC522 is an RFID reader module. It's a small electronic circuit board that can detect RFID tags from a short distance away, usually a few centimeters (for the MFRC522 this is up to 5cm - depending on the antenna configuration).

RFID tags are tiny computer chips that are attached to or embedded in objects. Things like contactless payment cards, keycards to access buildings, and tickets all contain RFID tags. The tags work like tiny transmitters - when near an RFID reader, they transmit a unique ID number wirelessly.

The MFRC522 module can read these tags and extract information from them like the unique ID number. It communicates wirelessly with tags using radio frequency (RF) signals at 13.56 MHz.

To use the MFRC522, it needs to be connected to the Arduino board. The Arduino talks to the MFRC522 module using its SPI interface - a serial interface protocol.

Once connected, your Arduino code can control the MFRC522 module to listen for nearby tags. When a tag is detected, the unique ID number and other data about the tag is sent back to the Arduino for your code to read and use however you like.

How are they powered?

When a tag is inserted into your pet you'll notice that you never need the device extracted to change the battery, and that is because they don't have a battery.

However, power must come from somewhere, and in fact it is supplied by the RFID reader. This sounds counter intuitive, but uses a the principle of magnetic induction. A loop of wire within the tag (the aerial) will collect some of the RF energy generated by the reader when it interrogates the tag. This is then used to power the circuit within the tag, which can then transmit the data out using this parasitically obtained power!

Required Components

  • Arduino Uno
  • MFRC522 RFID reader module
  • Jumper wires
  • RFID card or tag (MIFARE Classic card)
  • 510 Ohm resistor x 4
  • 1kOhm resistor x 4
Arduino RFID components laid out

Arduino RFID Reader: Circuit

Arduino Interface Problem

Most of the - if not all the designs on the web for this Arduino RFID reader are completely wrong because they claim that the RFID chip is 5V tolerant - it's not. Why? Because the device is 3V3 digital I/O - with the chip maximum voltage tolerance being 4.5V.

A lot of circuits use the 3V3 supply from the Uno - which is correct, but they allow the other signals to be way above the specified voltage - in the datasheet this is 4.5V. When these other designs allow a 5V voltage to be applied it could damage the chip (MFRC522).

Warning: Use level shifters for all control signals to the MFRC522.
You could use a few logic-level-converters to change voltage levels but the SPI interface runs fast and probably won't keep up. The speed of the converter is not fast enough for SPI operation.

The easy (and fast) way to convert signals going from the Uno to the MFRC522 is to divide down each signal using a voltage divider. Use dividers for these signals:
  1. RST
  2. SS (SDA)
  3. MOSI
  4. SCK
The only other signal to worry about is the one from the MFRC522 to the Uno - the MISO. But the voltage levels going from 3V3 systems to 5V systems will be high enough to allow correct operation i.e. you can connect this one directly!

Arduino RFID Reader: Connections

Ground (0V) and supply voltage (5V) are connected from pins in the lower pin header of the Arduino Uno, while Serial Clock (SCK), Serial Data (SDA) and the RFID reader's chip select (SS) pin connect to the corresponding SPI pins on the Arduino (SCK, MOSI, MISO, respectively).

Missing Slave Select pin

Note that the module is capable of I2C and Serial operation but is usually configured in SPI mode (depends on two pins that are control inputs on the chip). The Slave Select pin is not shown but a shares name with the I2C interface. This is why the I2C signal SDA doubles as the SS (SPI - Slave Select pin).

Arduino RFID Reader: Arduino Pins used

  • RST pin connects to Arduino pin 9 - with divider.
  • SS pin connects to Arduino pin 10 - with divider - (labelled SDA on breakout).

  • MOSI pin connects to Arduino pin 11- with divider.

  • MISO pin connects to Arduino pin 12.

  • SCK pin connects to Arduino pin 13- with divider.

  • 3.3V power pin connects to Arduino 3.3V power.

  • GND pin connects to Arduino GND.

Arduino RFID Reader: Circuit layout

Labels on the module from left to right are:

SDA SCK MOSI MISO IRQ GND RST 3.3V

Arduino RFID Reader layout Diagram

Diagram using Fritzing

Arduino RFID Reader: Schematic Diagram

Arduino RFID Reader Schematic Diagram

Diagram using Fritzing

Typical output on the serial monitor

The following output is from my RFID kit and your numbers will be different:

Place your RFID tag near the reader...
UID tag: C1 20 85 0F
Message: Unauthorized access
UID tag: B3 FA 3A FE
Message: Unauthorized access

The first is for the key-fob and the second is for the credit card.

Get the values for your fob and card. Now choose one and copy and paste the hex UI values replacing the text "YOUR_TAG_ID_HERE" in the code below. Re-compile and one should output "Authorized access" and the other should output "Unauthorized access".

Libraries needed

The library you need to install is MFRC522 by github community (miguelbalboa/rfid: Arduino RFID Library for MFRC522 (github.com)).

Installing an Arduino Library with IDE

Install the library as follows:

1st method:

    Click the menu bar and follow the menu items:

Sketch-->Include Library-->Manage Libraries...

2nd method:

    Click the ICON on the left that looks like a set of books.
The library manager appears on the left of the screen.

In the search box type MFRC522. Now click the install button.

Arduino RFID Reader: Example Sketch

Here's an example sketch to get you started with the RFID reader. This code allows an Arduino to read RFID tags using a MFRC522 reader module connected via SPI. It continuously loops to detect tags and check if they are authorized, printing a response each time.

You can copy and paste the code below into the Arduino IDE (in a new sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).


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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("RFID Reader Initialized");
  Serial.println("Place your RFID tag near the reader...");
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  Serial.print("UID tag:");
  char content[32];
  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);
    sprintf(&content[strlen(content)], "%s%02X", (i == 0) ? "" : " ", mfrc522.uid.uidByte[i]);
  }
  Serial.println();
  Serial.print("Message: ");
  for (byte i = 0; i < strlen(content); i++) {
    content[i] = toupper(content[i]);
  }

  if (strcmp(&content[1], "YOUR_TAG_ID_HERE") == 0) {
    Serial.println("Authorized access");
    // Add your authorized access logic here
  } else {
    Serial.println("Unauthorized access");
    // Add your unauthorized access logic here
  }

  delay(2000);
}

Short Code Explanation

  • Initializes the MFRC522 RFID reader module
  • Prints a message when initialized
  • Reads the card serial number if present
  • Prints the UID tag as hex values
  • Checks if the UID matches an authorized tag
  • Prints a message for authorized or unauthorized access

    Detailed Code Explanation

    RFID Reader Code

    This Arduino code is used to read RFID tags from a MFRC522 RFID reader module connected via SPI. It demonstrates how to initialize the reader, read tag IDs, and check for authorization.

    Setup

    In the setup function:

    • Serial communication is initialized at 9600 baud.
    • SPI communication is initialized.
    • The MFRC522 reader is initialized.
    • A message is printed to indicate initialization is complete.

    Loop

    The main loop continuously runs to:

    • Check if a new card is present.
    • Read the card serial number if a card is present.
    • Print the UID tag as hex values over serial.
    • Compare the UID to an authorized tag ID.
    • Print a message indicating authorization status.

    A 2 second delay is added between reads to allow time for tag removal. This allows the code to continuously monitor for tags and respond accordingly.

    Arduino RFID Reader: Uploading the Code

    There are a few steps to uploading the code using the Arduino IDE:

    1. Connect the Arduino Uno to the PC with a USB cable.
    2. Select the Arduino Uno hardware.
    3. Open a new sketch.
    4. Paste the code above into the new page (overwrite everything).
    5. Press the upload button (right arrow at top).
    You can find a more detailed tutorial on the Arduino IDE page.

    Arduino RFID Reader: Testing the Circuit

    To test the circuit, hold an RFID card near the reader module. You should see the card's unique identifier (UID) printed to the serial monitor. You can now start exploring additional ways to utilize the reader like controlling outputs based on card scans.

    Conclusion


    In this beginners guide you learned how to interface an RFID reader module with the Arduino and read tag information from RFID cards. With RFID you can easily identify and track objects which has many applications. The techniques covered can be expanded to create more complex RFID based projects.



    Written by John Main who has a degree in Electronic Engineering.

    Note: Parts of this page were written using claude-instant as a research assistant.



     


    Comments

    Have your say about what you just read! Leave me a comment in the box below.

    Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



    Privacy Policy | Contact | About Me

    Site Map | Terms of Use