Try Embedded

RFID door lock (MFRC522)

Unlock a servo latch for one known card and refuse others, counting each card's visits in its own memory.

Communication · Intermediate

In a real product: Office access control (Industrial): Badges unlock doors and each visit is counted.

Office doors, bus passes and hotel keys use 13.56 MHz MIFARE cards. An MFRC522 reader on SPI reads a card's unique ID and can store data on the card itself.

What you'll learn

  • Detecting a card and reading its UID over SPI
  • Authenticating to a sector and reading and writing a 16-byte block
  • Why a sketch halts a card when it is done with it

How it works

PICC_IsNewCardPresent() asks for cards in the field (REQA) and PICC_ReadCardSerial() runs the anticollision and SELECT exchange that returns the UID. The sketch compares it with the allowed card: a match swings the servo latch open for three seconds and lights the green LED.

Each card also keeps a visit counter in block 4. Reading or writing a block needs authentication to its sector with a key; every new card's key A is FF FF FF FF FF FF. PICC_HaltA() puts the card to sleep, so the same card is not read again until it is taken away and brought back.

Try this

  • Run it and click the reader to hold the white card to it, then click again to take it away.
  • Pick the blue fob in the reader's settings and hold it there: access denied.
  • Change ALLOWED to the fob's UID (3A F2 1C 95) so both cards open the door.

sketch.ino

// An RFID lock: MFRC522 on SPI (SS 10, RST 9), a latch servo on 6, a green LED on 7. Counts visits in block 4.
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

const byte ALLOWED[4] = {0xDE, 0xAD, 0xBE, 0xEF};
MFRC522 rfid(10, 9);
MFRC522::MIFARE_Key key;
Servo latch;

bool allowed() {
  return rfid.uid.size == 4 && memcmp(rfid.uid.uidByte, ALLOWED, 4) == 0;
}

// The visit counter in block 4, read, increased and written back. Returns 0 on any error.
long countVisit() {
  if (rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &rfid.uid) != MFRC522::STATUS_OK) return 0;
  byte block[18];
  byte size = sizeof(block);
  if (rfid.MIFARE_Read(4, block, &size) != MFRC522::STATUS_OK) return 0;
  long visits = 0;
  memcpy(&visits, block, sizeof visits);
  visits++;
  memcpy(block, &visits, sizeof visits);
  if (rfid.MIFARE_Write(4, block, 16) != MFRC522::STATUS_OK) return 0;
  return visits;
}

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  latch.attach(6);
  latch.write(0);
  pinMode(7, OUTPUT);
  Serial.println("Hold a card to the reader");
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
  Serial.print("Card");
  for (byte i = 0; i < rfid.uid.size; i++) { Serial.print(' '); Serial.print(rfid.uid.uidByte[i], HEX); }
  long visits = countVisit();
  Serial.print("  visit ");
  Serial.print(visits);
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
  if (allowed()) {
    Serial.println("  ACCESS GRANTED");
    digitalWrite(7, HIGH);
    latch.write(90);
    delay(3000);
    latch.write(0);
    digitalWrite(7, LOW);
  } else {
    Serial.println("  access denied");
  }
}

Lessons behind it