RFID door on an Arduino Uno
An MFRC522 reader and four cards. Tap one: two are on the list and light the green lamp, the other two light the red. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
IntermediateRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// RFID door: two of the four cards open it, the other two do not.
//
// Tap a card beside the reader to lay it on the antenna. The sketch reads its
// UID, looks it up in the list below, and lights the green LED for a card on
// the list or the red one for a card that is not. Every UID is printed, so a
// new card is added by tapping it, copying its UID from the serial monitor
// into ALLOWED, and running again.
//
// PICC_HaltA() at the end puts the card to sleep: it stays quiet until it
// leaves the field. That is why a card left on the reader opens the door once
// and not once a loop. Tap it off and on again to read it again.
#include <SPI.h>
#include <MFRC522.h>
MFRC522 reader(10, 9); // SDA (chip select) on 10, RST on 9
const int GREEN_LED = 7;
const int RED_LED = 6;
// The cards that open the door: the white card and the blue key fob.
const byte ALLOWED[][4] = {
{0xC3, 0x5A, 0x1E, 0x94},
{0x7B, 0x02, 0xD4, 0x19},
};
const int ALLOWED_COUNT = sizeof(ALLOWED) / sizeof(ALLOWED[0]);
bool onTheList()
{
if (reader.uid.size != 4) {
return false; // the seven-byte sticker is never on it
}
for (int card = 0; card < ALLOWED_COUNT; card++) {
bool same = true;
for (int i = 0; i < 4; i++) {
if (reader.uid.uidByte[i] != ALLOWED[card][i]) {
same = false;
}
}
if (same) {
return true;
}
}
return false;
}
void setup()
{
Serial.begin(9600);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
SPI.begin();
reader.PCD_Init();
reader.PCD_DumpVersionToSerial();
Serial.println("Tap a card on the reader.");
}
void loop()
{
if (!reader.PICC_IsNewCardPresent() || !reader.PICC_ReadCardSerial()) {
return;
}
Serial.print("UID:");
for (byte i = 0; i < reader.uid.size; i++) {
Serial.print(reader.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(reader.uid.uidByte[i], HEX);
}
Serial.println();
int led = onTheList() ? GREEN_LED : RED_LED;
Serial.println(led == GREEN_LED ? "Welcome in." : "Not this card.");
reader.PICC_HaltA();
digitalWrite(led, HIGH);
delay(1500);
digitalWrite(led, LOW);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × RFID reader, MFRC522
- 2 × Resistor, 220 Ω
- 1 × LED, green
- 1 × LED, red
How it is wired
12 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; RFID reader, MFRC522 pin GND
- Arduino Uno R3 pin 13; RFID reader, MFRC522 pin SCK
- Arduino Uno R3 pin 12; RFID reader, MFRC522 pin MISO
- Arduino Uno R3 pin 11; RFID reader, MFRC522 pin MOSI
- Arduino Uno R3 pin 10; RFID reader, MFRC522 pin SDA
- Arduino Uno R3 pin 9; RFID reader, MFRC522 pin RST
- Arduino Uno R3 pin 7; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 6; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 3V3; RFID reader, MFRC522 pin 3.3V
- Ground: Arduino Uno R3 pin GND; LED, green pin C; LED, red pin C
- Resistor, 220 Ω (1) pin 2; LED, green pin A
- Resistor, 220 Ω (2) pin 2; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.