RFID door lock with an RC522 and servo on Arduino: design worksheet and code

A ₹964 design worksheet for a small RFID-card lock using an RC522 reader and a servo latch, with the UID-matching logic, lock-out after repeated failures, and an honest note on how weak a UID check is.

About the author · Editorial & verification standards

  • Parts₹964
  • Bench time4–5 h
  • LevelIntermediate
  • ReviewedNov 2026

A ₹964 card-operated latch for a cupboard, a box or a model door. An RC522 reader reads the card’s ID, the sketch compares it against a short list, and a servo moves the latch for five seconds. It is a good project for learning SPI and state handling, but the page is clear that a card ID check is a weak form of security.

Scope: intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The UID and lock-out figures below were computed by script. Do not use this to secure anything valuable. The card ID on common MIFARE Classic cards is sent in the clear and can be copied with cheap tools, so it is a convenience, not a lock. The reader runs on 3.3 V; connecting it to 5 V can damage it.

How it works

The RC522 reads 13.56 MHz cards and tags. The common cards give a 4-byte UID, which can take 2^32 = 4,294,967,296 values. That sounds big, but the UID is not a secret and many cheap cards let it be rewritten, so anyone who sees it can copy it. A UID check only keeps honest people out and lets you give family members their own cards.

The sketch compares the UID with a stored list. A match opens the latch for five seconds, then closes it. Three wrong cards in a row start a ten second lock-out where every card is ignored, which slows down guessing a UID by trying many cards.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
RC522 RFID reader module1₹120₹120
RFID card or tag2₹20₹40
SG90 micro servo1₹120₹120
Green and red LEDs1₹5₹5
220 ohm resistors2₹2₹4
Active buzzer module1₹15₹15
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹964

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

ItemValue
UID length4 bytes
UID values possible4,294,967,296
Open time5 seconds
Wrong cards before lock-out3
Lock-out time10 seconds
Fastest guessing rateabout 14 tries a minute
A UID is an identifier, not a password. The lock-out only slows blind guessing; it does not stop copying a card you have seen.

The lock-out arithmetic: each cycle allows 3 tries, then 10 seconds are ignored. Tapping as fast as possible, say one card per second, gives 3 tries in 3 seconds plus 10 seconds of lock-out, so about 3 tries per 13 seconds, or about 14 a minute. Against 4.3 billion possible UIDs this is no protection against someone who can copy a UID.

Power and wiring

PartConnects toNote
RC522 3.3V and GND3V3 and GNDNot 5 V
RC522 SDA (SS)D10
RC522 SCK / MOSI / MISOD13 / D11 / D12Hardware SPI
RC522 RSTD9
Servo signalD6Servo power from the 5V pin
Green and red LEDsD2 and D3Through 220 ohm
BuzzerD8
If the servo makes the reader misbehave when it moves, power the servo from its own 5 V supply.

The sketch

// RFID door lock: Uno + RC522 + servo
// Design worksheet: a UID check is weak security. Do not protect valuables with it.

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

const int SS_PIN = 10, RST_PIN = 9, SERVO_PIN = 6;
const int LED_OK = 2, LED_BAD = 3, BUZ = 8;
const int LOCKED = 0, UNLOCKED = 90;                  // degrees
const unsigned long OPEN_MS = 5000UL, LOCKOUT_MS = 10000UL;

MFRC522 reader(SS_PIN, RST_PIN);
Servo latch;

// Replace with your own cards: print the UID to Serial to learn it.
const byte ALLOWED[][4] = { {0xDE, 0xAD, 0xBE, 0xEF}, {0x12, 0x34, 0x56, 0x78} };
const int N_ALLOWED = sizeof(ALLOWED) / sizeof(ALLOWED[0]);

int failures = 0;
unsigned long lockoutUntil = 0;

bool isAllowed() {
  if (reader.uid.size != 4) return false;
  for (int i = 0; i < N_ALLOWED; i++) {
    bool same = true;
    for (int b = 0; b < 4; b++) if (reader.uid.uidByte[b] != ALLOWED[i][b]) same = false;
    if (same) return true;
  }
  return false;
}

void beep(int ms) { digitalWrite(BUZ, HIGH); delay(ms); digitalWrite(BUZ, LOW); }

void setup() {
  Serial.begin(9600);
  SPI.begin(); reader.PCD_Init();
  latch.attach(SERVO_PIN); latch.write(LOCKED);
  pinMode(LED_OK, OUTPUT); pinMode(LED_BAD, OUTPUT); pinMode(BUZ, OUTPUT);
}

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

  Serial.print("UID:");
  for (byte i = 0; i < reader.uid.size; i++) { Serial.print(' '); Serial.print(reader.uid.uidByte[i], HEX); }
  Serial.println();

  if (millis() < lockoutUntil) {
    beep(300);
  } else if (isAllowed()) {
    failures = 0;
    digitalWrite(LED_OK, HIGH); latch.write(UNLOCKED); beep(100);
    delay(OPEN_MS);
    latch.write(LOCKED); digitalWrite(LED_OK, LOW);
  } else {
    digitalWrite(LED_BAD, HIGH); beep(400); digitalWrite(LED_BAD, LOW);
    if (++failures >= 3) { lockoutUntil = millis() + LOCKOUT_MS; failures = 0; }
  }
  reader.PICC_HaltA();
  reader.PCD_StopCrypto1();
}

Print each card’s UID to the serial monitor, then paste the bytes into the ALLOWED list. The sketch is blocking while the latch is open, which is fine for this one job. Keep the physical latch design in mind: a servo can be forced by hand, so a servo latch is for convenience only. The sketch has been proofread but not compiled on this site.

Test plan

  1. Reader check: tap each card and confirm the UID prints and is stable.
  2. Allowed card: confirm the latch moves and closes after five seconds.
  3. Wrong card: tap a card that is not on the list and check the red LED and beep.
  4. Lock-out: tap three wrong cards and confirm that even an allowed card is ignored for ten seconds.
  5. Range: note how close a card must be and whether a thick wallet blocks it.
  6. Power: check the servo moving does not reset the reader.

What usually goes wrong

SymptomLikely causeFix
Reader does not respondPowered from 5 V or wrong SPI pinsUse 3.3 V and check D10, D11, D12, D13
Card read once then ignoredCard not haltedKeep PICC_HaltA and StopCrypto1
Servo jittersSupply noiseSeparate 5 V supply
Allowed card refusedUID typed wronglyPrint the UID and paste it exactly
Works at bench but not in the boxMetal near the antennaKeep metal away from the reader

Upgrades

  • Logging: record each tap with a timestamp on an SD card.
  • Master card: add a way to enrol new cards by tapping a master card.
  • Stronger lock: use a solenoid bolt and a stronger authentication scheme.
  • Door sensor: add a reed switch to detect whether the door is closed.

Cost and parts notes

The ₹964 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

No. UIDs of common cards are sent in the clear and cheap tools can copy them. It is fine for convenience, not security.

The module is a 3.3 V part. Many boards tolerate 5 V logic on the data pins but not on the supply; check yours.

Print its UID on the serial monitor and add it to the ALLOWED list, then upload again.

Not with this design. It is a model or cupboard lock.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.