RFID Door Lock: 56 Lines to Open a Door With a Card
An RFID door lock sounds like a product you buy. It is also a project you can build with three components and 56 lines of Arduino code. An MFRC522 reader, a servo motor, and a microcontroller. That is the entire bill of materials.
The system reads the UID of any card placed near the reader. If the UID matches a hardcoded value, the servo rotates to 180 degrees. The door stays unlocked for five seconds. Then the servo returns to zero and the lock engages.
The Check
The MFRC522 communicates over SPI. The Arduino calls PICC_IsNewCardPresent and PICC_ReadCardSerial in each loop cycle. When a card is detected, the code concatenates the UID bytes into a string and compares it against the authorized ID.
A match triggers unlockDoor. The servo writes 180 degrees, delays for 5000 milliseconds, and writes zero. That is the entire function. Two lines of actuation and one blocking delay.
What I Would Change
The authorized ID is hardcoded as 719a228. Changing the card means editing the code and reuploading. I would store authorized IDs in EEPROM and add a pairing mode that learns new cards at the press of a button.
The delay function blocks everything for five seconds. The Arduino cannot read a new card, respond to other inputs, or report its status while the door is unlocked. A non-blocking timer using millis would allow concurrent behavior without adding complexity.
The code builds the UID string using concatenation inside the loop. On an Arduino with limited memory, repeated string allocation can fragment the heap over time. Using a fixed character array would be more reliable for continuous operation.
It is 56 lines and it does one thing. Tap a card. Open a door. Wait. Close it. That is enough to understand how access control works at the hardware level.