Using an RFID module

Passive RFID tags are basically just little antennas with an extremely low power integrated circuit. When the IC is activated by power induced by the antenna, it generates a signal to transmit a small amount of data, such as an ID number.

This RC522 RFID module can read RFID tags and write to RFID tags that support writing. The RFID tag has a 4-byte (or 32-bit) UID, which means there are about 4.3 billion unique possibilities. Although that sounds like a lot, there are some situations where it's not considered enough to prevent collisions or brute-force guessing.

RFID tags, on their own, are not very suitable for access control in a modern high-security environment. However, it will typically be harder for an intruder to guess a 32-bit UID than, for example, to pick a traditional house lock using standard lockpicking tools and a little practice.

The RFID tag is called a PICC, short for Proximity Integrated Circuit Card because "tag" is apparently too easy.

This sample code will print the PICC type and UID to the serial port:


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

const byte RST_PIN = 9;
const byte SS_PIN  = 10;

MFRC522 reader(SS_PIN, RST_PIN);

void printUID(byte* bytes, byte len)
{
	Serial.print("UID: ");

	for (byte i = 0; i < len; i++) {
		// Pad single-digits with 0
		if (bytes[i] < 0x10) {
			Serial.print("0");
		}

		Serial.print(bytes[i], HEX);
	}

	Serial.println();
}

void printPiccType(byte sak)
{
	Serial.print("SAK: ");
	Serial.println(sak);

	auto piccType     = reader.PICC_GetType(sak);
	auto piccTypeName = reader.PICC_GetTypeName(piccType);

	Serial.print("PICC type: ");
	Serial.println(piccTypeName);
}

void setup()
{
	Serial.begin(9600);
	SPI.begin();
	reader.PCD_Init();
}

void loop()
{
	delay(50);

	bool cardFound = reader.PICC_IsNewCardPresent();
	bool cardRead  = reader.PICC_ReadCardSerial();

	if (cardFound && cardRead) {
		auto uid = reader.uid;

		printUID(uid.uidByte, uid.size);
		printPiccType(uid.sak);

		delay(200);
	}
}