Skip to main content

Using an MFRC522 RFID reader

What is an MFRC522 RFID reader

RFID means radio-frequency identification. RFID uses electromagnetic fields to transfer data over short distances. RFID is useful to identify people, to make transactions, etc…

You can use an RFID system to open a door. For example, only the person with the right information on his card is allowed to enter. An RFID system uses tags with each identification and a two-way radio transmitter-receiver as a reader.

rfid.png

Wiring

**Caution**Caution
You must power this device to 3.3V! This tutorial is based on Arduino UNO, if you are using a different module, please check the specific pinout of that model.

  1. SDA to- Digital 10 (SS)
  2. SCK to- Digital 13 (SCK)
  3. MOSI to- Digital 11 (MOSI)
  4. MISO to- Digital 12 (MISO)
  5. IRQ (unconnected)
  6. GroundGND - GND (GND to GND)Ground)
  7. RST to- Digital 9
  8. Power (3.3V to 3.3V)3V (Power)

RFIDwiring.png

Library

We will be using the MFRC522 library. Please see this tutorial to learn how to install libraries.

Getting started

We will be using the example code, DumpInfo, from the library to read an RFID Tag.

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

String previous_card_number;
unsigned long readTimeout;

// A struct used for passing the UID of a PICC.
typedef struct {
  byte      size;      // Number of bytes in the UID. 4, 7 or 10.
  byte      uidByte[10];
  byte      sak;      // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
} RFIDUid;

// A struct used for passing a MIFARE Crypto1 key
typedef struct {
  byte      keyByte[6];
} MIFARE_Key;

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
	Serial.begin(115200)9600);		// Initialize serial communications with the PC
	while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522
	delay(4);				// Optional delay. Some board do need more time after init to be ready, see Readme
	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
	rfidRead();
}
// CheckReset forthe loop if no new card andis getpresent uidon voidthe rfidRead()sensor/reader. {This Stringsaves card_number;the MIFARE_Keyentire key;process MFRC522::Uidwhen *uid = &(mfrc522.uid);

  // Look for cardidle.
	if ( ! mfrc522.PICC_IsNewCardPresent() ) {
		return;
	}

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial() ) return;

  // UID
  for ( byte i = 0; i < uid->size; i++ ) {
		String byteVal = String( uid->uidByte[i], HEX );
    if (byteVal.length() <= 1) {
      byteVal = "0" + byteVal;
    }
    card_number = card_number + byteVal;
    //    Serial.print( uid->uidByte[i] < 0x10 ? "0" : "" );
    //    Serial.print( uid->uidByte[i], HEX );return;
	}

	// OnlyDump printdebug cardinfo if notabout the samecard; as last read
  if ( card_number != previous_card_number PICC_HaltA() {is Serial.println(card_number)automatically called
	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  }

  // Remember card for next read
  previous_card_number = card_number;
}

We will be using the example code, DumpInfo, from the library to read an RFID Tag.