0

RFID Door Lock with Arduino and the RC522 Reader

Build an RFID door lock with Arduino, the RC522 reader and a servo latch. Full wiring diagram, code and how to register your card.

Make a working RFID door lock that unlocks only when the right card or tag is scanned. It uses the RC522 reader and a small servo as the latch — a brilliant project for learning SPI, security logic and access control.

You’ll get the full wiring diagram (RC522 uses seven pins), the Arduino code, and how to register your own card. All parts are in stock at Arduino Corner.

What you’ll need

A breadboard makes the many connections tidier.

Wiring diagram

Wiring diagram showing an RC522 RFID reader and a servo connected to an Arduino Uno over SPI
RC522 (SPI, 3.3V) and the servo latch wired to the Arduino.

The same connections as a quick reference table:

From (module)To (Arduino Uno)
RC522 — 3.3V3.3V (not 5V!)
RC522 — RSTD9
RC522 — GNDGND
RC522 — MISOD12
RC522 — MOSID11
RC522 — SCKD13
RC522 — SDA (SS)D10
Servo — VCC5V
Servo — SignalD3
Servo — GNDGND

Tip: The RC522 runs on 3.3V — connecting it to 5V can damage it. The servo, however, takes 5V.

How it works

RFID cards contain a tiny chip and antenna with a unique UID (identity number). When you tap a card, the RC522 reads its UID and sends it to the Arduino over the SPI bus (the MISO, MOSI, SCK and SS pins). The code compares that UID to your allowed one; if they match, it turns the servo to unlock, waits, then locks again.

The Arduino code

Install two libraries from Manage Libraries: “MFRC522” by GithubCommunity, and the built-in Servo library.

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

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
Servo lock;

// Scan your card once, copy the UID from the Serial Monitor, paste it here:
String allowedUID = "A1B2C3D4";

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  lock.attach(3);
  lock.write(0);    // start locked
}

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

  String uid = "";
  for (byte i = 0; i < rfid.uid.size; i++) {
    uid += String(rfid.uid.uidByte[i], HEX);
  }
  uid.toUpperCase();
  Serial.println(uid);

  if (uid == allowedUID) {
    lock.write(90);    // unlock
    delay(3000);
    lock.write(0);     // lock again
  }
  rfid.PICC_HaltA();
}

Understanding the code

The first time you run it, open the Serial Monitor and scan your card — its UID is printed. Copy that value into allowedUID and re-upload. lock.write(0) and lock.write(90) move the servo between the locked and unlocked positions — adjust the angles to suit your latch.

Troubleshooting

  • Card never reads (UID blank): almost always the RC522 is on 5V instead of 3.3V, or an SPI wire (MISO/MOSI/SCK/SS) is on the wrong pin.
  • Servo jitters or resets the board: power the servo from a separate 5V supply if it’s straining the Arduino.
  • Right card is rejected: UID letter-case must match — the sketch calls toUpperCase(), so store the UID in capitals.

Take it further

  • Add a buzzer that beeps on access granted vs denied.
  • Store several allowed cards in an array for a whole family.
  • Swap the servo for a solenoid lock driven through a relay for a real door.

Frequently asked questions

How do I find my card’s UID?

Upload the sketch, open the Serial Monitor at 9600 baud and tap the card — its UID prints on screen. Paste it into allowedUID.

Why must the RC522 use 3.3V?

Its chip is a 3.3V device. Feeding it 5V on the power pin can permanently damage it — only the logic pins tolerate the Arduino’s 5V signals.

Can it store more than one card?

Yes — keep a list of allowed UIDs in an array and check the scanned UID against each one.

Related project guides

Get the parts

Everything for this build is in stock at Smart Home | Arduino Corner — Shaheen Market, DAV College Road, Rawalpindi — and ships across Pakistan with cash on delivery. Grab the RC522 RFID reader and an SG90 servo. Message us on WhatsApp and we’ll put the whole kit together for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us on WhatsApp