Arduino RFID Door Lock with RC522: Wiring, Code and Simulator

To build this Arduino RFID Door Lock with RC522 project you need an Arduino Uno and an RC522 RFID reader, a green LED, a red LED and two 220 Ω resistors. Only the white card’s UID opens the lock: green LED for access granted, red for denied. Make the 13 connections in the wiring table, upload the Arduino C++ code, and tap each card. Then copy the blue fob’s UID into ALLOWED[] and run again.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1RC522 RFID reader13.56 MHz RFID/NFC reader on SPI: 3.3V, GND, SDA (chip select) and RST to any pins, SCK/MOSI/MISO to the board’s SPI pins. Tap a card from the Modules tab (or set "Card on the reader"): a white MIFARE Classic card, a blue key fob, or an NTAG213 sticker.
1green LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.
1red LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.
2220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.

Wiring

#FromTo
1Arduino Uno 3.3VRC522 RFID reader 3.3V
2Arduino Uno GNDRC522 RFID reader GND
3Arduino Uno pin 10RC522 RFID reader SDA
4Arduino Uno pin 9RC522 RFID reader RST
5Arduino Uno pin 13RC522 RFID reader SCK
6Arduino Uno pin 11RC522 RFID reader MOSI
7Arduino Uno pin 12RC522 RFID reader MISO
8Arduino Uno pin 5green LED anode (A, long leg)
9green LED cathode (C, short leg)220 Ω resistor (r1) leg 1
10Arduino Uno GND220 Ω resistor (r1) leg 2
11Arduino Uno pin 6red LED anode (A, long leg)
12red LED cathode (C, short leg)220 Ω resistor (r2) leg 1
13Arduino Uno GND220 Ω resistor (r2) leg 2

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an RC522 RFID reader, a green LED, a red LED and two 220 Ω resistors from the parts palette.
  3. Wire 1. Connect Arduino Uno 3.3V to RC522 RFID reader 3.3V.
  4. Wire 2. Connect Arduino Uno GND to RC522 RFID reader GND.
  5. Wire 3. Connect Arduino Uno pin 10 to RC522 RFID reader SDA.
  6. Wire 4. Connect Arduino Uno pin 9 to RC522 RFID reader RST.
  7. Wire 5. Connect Arduino Uno pin 13 to RC522 RFID reader SCK.
  8. Wire 6. Connect Arduino Uno pin 11 to RC522 RFID reader MOSI.
  9. Wire 7. Connect Arduino Uno pin 12 to RC522 RFID reader MISO.
  10. Wire 8. Connect Arduino Uno pin 5 to green LED anode (A, long leg).
  11. Wire 9. Connect green LED cathode (C, short leg) to 220 Ω resistor (r1) leg 1.
  12. Wire 10. Connect Arduino Uno GND to 220 Ω resistor (r1) leg 2.
  13. Wire 11. Connect Arduino Uno pin 6 to red LED anode (A, long leg).
  14. Wire 12. Connect red LED cathode (C, short leg) to 220 Ω resistor (r2) leg 1.
  15. Wire 13. Connect Arduino Uno GND to 220 Ω resistor (r2) leg 2.
  16. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  17. Run it. Press Run. Tap each card. Then copy the blue fob’s UID into ALLOWED[] and run again.

Arduino C++ code

// RFID door lock: only the white card opens it.
// RC522 as in the UID example; green LED on pin 5, red LED on pin 6 (with resistors).
#include <SPI.h>
#include <MFRC522.h>

MFRC522 rfid(10, 9);
const byte ALLOWED[4] = { 0xA3, 0x5F, 0x21, 0x1B };  // the white card
const int GREEN = 5;
const int RED = 6;

void setup() {
  Serial.begin(9600);
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  SPI.begin();
  rfid.PCD_Init();
  Serial.println(F("Door locked. Tap a card."));
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;

  bool ok = rfid.uid.size == 4;
  for (byte i = 0; i < 4 && ok; i++) ok = rfid.uid.uidByte[i] == ALLOWED[i];

  if (ok) {
    Serial.println(F("Access granted"));
    digitalWrite(GREEN, HIGH);
  } else {
    Serial.println(F("Access denied"));
    digitalWrite(RED, HIGH);
  }
  rfid.PICC_HaltA();
  delay(1500);
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, LOW);
}

What to try

Tap each card. Then copy the blue fob’s UID into ALLOWED[] and run again.

Frequently asked questions

How do I wire the RFID Door Lock with RC522 project on an Arduino Uno?

Make 13 connections: Arduino Uno 3.3V to RC522 RFID reader 3.3V; Arduino Uno GND to RC522 RFID reader GND; Arduino Uno pin 10 to RC522 RFID reader SDA; Arduino Uno pin 9 to RC522 RFID reader RST; Arduino Uno pin 13 to RC522 RFID reader SCK; Arduino Uno pin 11 to RC522 RFID reader MOSI; Arduino Uno pin 12 to RC522 RFID reader MISO; Arduino Uno pin 5 to green LED anode (A, long leg); green LED cathode (C, short leg) to 220 Ω resistor (r1) leg 1; Arduino Uno GND to 220 Ω resistor (r1) leg 2; Arduino Uno pin 6 to red LED anode (A, long leg); red LED cathode (C, short leg) to 220 Ω resistor (r2) leg 1; Arduino Uno GND to 220 Ω resistor (r2) leg 2.

What parts do I need for this Arduino project?

An Arduino Uno and an RC522 RFID reader, a green LED, a red LED and two 220 Ω resistors.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Only the white card’s UID opens the lock: green LED for access granted, red for denied.

Is an RFID UID safe for a door lock?

Only for learning: a MIFARE Classic UID can be copied onto a "magic" card. Real access systems authenticate with keys or use secure cards such as MIFARE DESFire.

More Arduino Uno projects