ArduinoArduino UnoExact chip

RFID door (RC522)

The MFRC522 library reads a card’s UID: the known card lights green, any other red. Click the reader to hold a card to it.

RFID door (RC522) in Argynix Lab

The code

sketch.inoC++
// An RFID door: the RC522 reads the UID of the card held to it. The known card
// (DE AD BE EF) lights the green LED, any other the red one. Click the reader while it
// runs to hold the card to it; change the card's UID in the inspector.
#include <SPI.h>
#include <MFRC522.h>

MFRC522 rfid(10, 9);  // SDA (chip select), RST
const byte KNOWN[4] = {0xDE, 0xAD, 0xBE, 0xEF};
const int GREEN = 6, RED = 7;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  SPI.begin();
  rfid.PCD_Init();
  Serial.println("Hold a card to the reader");
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
  bool known = rfid.uid.size == 4 && memcmp(rfid.uid.uidByte, KNOWN, 4) == 0;
  Serial.print("Card ");
  for (byte i = 0; i < rfid.uid.size; i++) {
    if (rfid.uid.uidByte[i] < 0x10) Serial.print('0');
    Serial.print(rfid.uid.uidByte[i], HEX);
    Serial.print(' ');
  }
  Serial.println(known ? ": welcome" : ": not known");
  digitalWrite(known ? GREEN : RED, HIGH);
  delay(1500);
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, LOW);
  rfid.PICC_HaltA();
}