RFID RC522 Arduino Tutorial

RFID, Radio Frequency Identity as the name implies, is a unique identity that can be read over radio frequency. Because over radio frequency (RF), the data is transferred wirelessly, which means between RFID tags / cards with RFID reader not need intersect / stick. Quite approximated about 1-5 cm, then the RFID card can be read by an RFID reader.

RFID we are talking about is the type of passive RFID. This means that RFID card does not have its own power supply. This kind of RFID cards gets power from the RFID reader which is brought near to it wirelessly. Thus the type of passive RFID can only be used for short distances (up to 5 cm).

Note: in its development, then emerge RFID active tag that has its own power supply. For such kind of RFID ‘slave’ can be read more distant (> 1 m), depending on its power level and carrier frequency.

RFID Mifare RC522 at a Glance

RC522 RFID is a RFID module product from NXP. This module works at a frequency of 13.56 MHz. The price is relatively cheap, below $2. The module has several options of communication such as SPI, I2C and UART. You can choose one of them which is compatible with your current system. For example, if you have a lot of unused Arduino pins, you can use SPI protocol (it takes at least 5 pin to work with SPI), or if you want to save Arduino’s pinouts, you can use the I2C / UART protocol (only need 2 pins). Just keep in mind if you're using an Arduino Uno board, it has only one UART port, of course, you have to re-think UART protocol to control the RC522 module (usually Arduino Uno’s UART port is used to debug program in Serial Monitor Window)

Note: SPI = Serial Peripheral Interface, I2C = Inter-Integrated Circuit, UART = Universal Asynchronous Receiver Transmitter). SPI and I2C (also called IIC) is a synchronous serial protocol, whereas UART is an asynchronous serial communication.

RC522 RFID Module Specification

Voltage: DC 3.3V
Flow: 13-26mA (10-13mA idle, sleep: <80uA, Peak: <30mA)
Working frequency: 13.56MHz
Supported Cards: Mifare1 S50, S70 Mifare1, MIFARE UltraLight, MIFARE Pro, mifare Desfire
Dimensions module: 40mm × 60mm
Ambient operating temperature: - 20-80 degrees centigrade
Ambient storage temperature: - 40-85 degrees centigrade
Ambient relative humidity: 5% -95%
Data Transfer Rate (SPI): Max. 10Mbit / s

RFID RC522 Arduino Configuration

To read RC522 MIFARE RFID data with Arduino, connect the module with Arduino as follows. In this tutorial I will cite how to communicate with the module using SPI protocol. Here is the configuration of all hardware.

Arduino
Modul RFID RC522
9
RST
10
SDA
11
MOSI
12
MISO
13
SCK
3,3V
3,3V
GND
GND

RFID RC522 Arduino Circuit

Here is a series of Arduino - RC522 Mifare RFID module using the SPI protocol to communicate with. Note that the module uses 3.3 V, do not try connects it to Arduino’s 5V pin because it can cause damage the module.


RFID RC522 Arduino Sketch Handler

After the hardware connection is already assembled, please upload sketch handler for RC522 RFID module. For convenience you can download a special library for this module here. Add this library into your Arduino IDE. Inside the library there are also examples of the source code, so it can be learned for your custom needs. To try your RFID module, you can upload sketch 'ReadNUID.ino' in menu File-examples-MFRC522-ReadNUID. The sketch is as follows:

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN);

MFRC522::MIFARE_Key key;

byte nuidPICC[4];

void setup() {
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  Serial.println(F("Kode NUID RFID"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}

void loop() {
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  if ( ! rfid.PICC_ReadCardSerial())
    return;

  Serial.print(F("PICC : "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // checkk PICC
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("kartu Anda bukan MIFARE Classic..."));
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("kartu RFID baru terdeteksi"));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
  
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}


void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

Upload the script above, and enable Serial Monitor window to view data in the RFID cards. This card usually included in purchasing package. Good luck.
Previous
Next Post »