Arduino - RFID/NFC

In this tutorial, we are going to learn how to use RFID/NFC with Arduino. The RFID/NFC system includes two components: reader and tag. There are two popular RFID/NFC readers: RC522 and PN532 RFID/NFC reader. This tutorial focuses on RC522 RFID/NFC reader. PN532 RFID/NFC reader will be presented in an upcoming tutorial.

RC522 RFID/NFC reader (also called RFID-RC522 Module) can:

In above capabilities, for Arduino, reading the UID is the most widely-used. This tutorial focuses on reading the UID of RFID/NFC tag. The other will be present in next tutorials

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×RFID/NFC RC522 Kit (reader + tags)
7×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About RFID-RC522 Module

Pinout

RFID-RC522 has 8 pins, some of them are common pins, the others are shared among three communication modes: SPI, I2C, UART. At a time, only one communication mode can be used. The pin are:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (3.3)
  • RST pin is a pin for reset and power-down. When this pin goes low, hard power-down is enabled. On the rising edge, the module is reset.
  • IRQ pin is an interrupt pin that can alert the microcontroller when RFID tag comes into its vicinity.
  • MISO/SCL/TX pin acts as MISO when SPI interface is enabled, acts as SCL when I2C interface is enabled and acts as TX when UART interface is enabled.
  • MOSI pin acts as MOSI when SPI interface is enabled.
  • SCK pin acts as SCK when SPI interface is enabled
  • SS/SDA/RX pin acts as SS when SPI interface is enabled, acts as SDA when I2C interface is enabled and acts as RX when UART interface is enabled.
  • RFID-RC522 Pinout

※ NOTE THAT:

  • Do not connect VCC pin to 5V pin. This will likely destroy your module.
    • MFRC522 library supports only SPI mode. Therefore, this tutorial uses only SPI communication.

How RFID/NFC Works

RFID/NFC includes two components: reader and tag.

  • The reader consists of a radio frequency module and an antenna which generates high frequency electromagnetic field.
  • The tag is usually a passive device, which doesn’t need to have power source. The tag contains a microchip that stores and processes information, and an antenna to receive and transmit a signal. The tag is used to store the information: UID (Unique ID) and data.
  • arduino rfid nfc system

To read the information on a tag, the tag must be in close proximity to the reader (does not require the direct line-of-sight). The reading processes:

  • The reader generates an electromagnetic field which causes electrons to move through the tag’s antenna and subsequently power the chip.
  • The chip inside the tag then responds by sending the requested information back to the reader in the form of another radio signal.
  • The reader detects the signal and transform the signal into data
  • Arduino reads the data from reader.

Wiring Diagram

Arduino RFID RC522 wiring diagram

Image is developed using Fritzing. Click to enlarge image

If you use the male-to-female jumper wires, you can connect Arduino UNO directly to RFID-RC522 module. If you use the male-to-male jumper wires, you need to connect Arduino UNO to RFID-RC522 module via a breadboard

Arduino RFID/NFC Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc */ #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 rfid(SS_PIN, RST_PIN); void setup() { Serial.begin(9600); SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522 Serial.println("Tap RFID/NFC Tag on reader"); } void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available if (rfid.PICC_ReadCardSerial()) { // NUID has been readed MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); //Serial.print("RFID/NFC Tag Type: "); //Serial.println(rfid.PICC_GetTypeName(piccType)); // print NUID in Serial Monitor in the hex format Serial.print("UID:"); for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(rfid.uid.uidByte[i], HEX); } Serial.println(); rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD } } }

Quick Steps

  • On Arduino IDE, Go to Tools Manage Libraries
  • Arduino add library
  • Search “MFRC522”, then find the library by GithubCommunity
  • Click Install button to install keypad library.
  • Arduino MFRC522 library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Tap several RFID/NFC tags on RFID-RC522 module
  • See UID on Serial Monitor
  • COM6
    Send
    Tap RFID/NFC tag on reader RFID/NFC tag Type: MIFARE 1KB UID: 3A C9 6A CB RFID/NFC tag Type: MIFARE Ultralight or Ultralight C UID: 04 64 34 5A 1E 4E 80
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES