Door Locking with Arduino and RFID Tags: Tutorial

Noah Omdal
7 min readNov 5, 2018

--

If you live in a modern apartment building, you may be familiar with key fobs that look something like this:

I use them all the time to get in and out of my building, and every time the little light on the entry pad lights up and the lock clicks open, I find myself wondering what sort of wizardry is at hand here. And you know what? I’ve had enough. It’s time to break down RFID technology and how you can take advantage of it yourself. We’ll start with explaining the underlying tech before walking through a tutorial of how you, armed with little more than an Arduino and some RFID tags and a sensor, can implement your own door lock. We’ll go over potential use cases, talk about the future of this sort of technology, and everyone will go home happy and ready build.

How Do These Doors Lock and Unlock? RFID Explained

Radio Frequency Identification (RFID) was born in the 1970s, created by Mario Cardullo as a potential tool of the New York Port Authority. As explained on Wikipedia:

It consisted of a transponder with 16 bit memory for use as a toll device. The basic Cardullo patent covers the use of RF, sound and light as transmission media. The original business plan presented to investors in 1969 showed uses in transportation (automotive vehicle identification, automatic toll system, electronic license plate, electronic manifest, vehicle routing, vehicle performance monitoring), banking (electronic checkbook, electronic credit card), security (personnel identification, automatic gates, surveillance) and medical (identification, patient history).

As explained above, the initial ideas for RFID’s use were immense and diverse. As the decades went on, the technology would be deployed in many of these areas, proving itself to be a powerful tool for light-weight identification. One of these areas is building security, with many apartment complexes issuing the tags to residents as a means of entering the locked building. It was here that my interest was sparked and that our design will be built around.

The Tutorial

Before we get started, you’re going to need a few supplies:

It’s that simple! Now let’s get started.

If you’re new to Arduino, you’re going to have to download the software and IDE here. This will provide you with an environment to write and upload code to the Arduino. I will be providing you with some code to get it off and running (code I am using from another source), but it’s easy to take it and extrapolate into new territory. What we will be creating today will look something like this: we have our arduino connected to our computer, where we upload the code to have it function with the RFID reader. The reader is wired to the Arduino, and when we hold any of our test RFID tags in front of it, it will register with the machine and our code will make the arduino turn the motor of our servo, which will then be connected to a locking mechanism. As we make the motor turn back and forth, it will cause the lock to open and close. This basic mechanism is all that is needed to block doors from opening, and can easily be implemented in a variety of different places.

The first thing to do is wire the Arduino to the components we’re using for this project. First we’ll worry about the RFID reader. Follow the diagram pasted below, which I made using Fritzing, a software for mapping out wiring for electronics hobbyists:

Use this model for your own implementation

It’s a pretty simple wiring, and if you want to spice things up by adding lights and what not (a fun experiment I did on my own), feel free. However, for this I am keeping it as simple as possible in order to help people get the structure down and open the door for their own creativity. It makes things easier if you solder the RFID reader’s connecting piece to it, though if you’re only doing it for demo purposes you can get by without doing so. Here is a photo of my own implementation of the above diagram:

My Arduino setup

Now that you’ve got it setup, it’s time to run the code on it. I wrote this simple script and have reproduced it below. Note that the specific ID of your tag is ignored at this point. Any tag will work on it. This is for ease of implementation on your part. Later on I will talk about how to allow only certain tags to have access to open and close your lock.

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define SERVO_PIN 8
Servo myservo; // Create instance of our motor

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create instance of our reader
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(“Arduino RFID lock”);
myservo.attach(SERVO_PIN);
myservo.write(90);
}
void loop(){
//Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent() ){
return;
}
if ( ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// If a card is detected, execute the following: Serial.println(“Time to open”);// Print the card's IDString content= “”;
byte letter;
for( byte i = 0; i < mfrc522.uid.size; i++ ){
content.concat(String(mfrc522.uid.uidByte[i], HEX));
if( i < mfrc522.uid.size-1 ) content+=”-”;
}
content.toUpperCase();
Serial.println();
Serial.println(“UID tag :’” + content + “‘“);
Serial.println(“Authorized access”);
myservo.write(90); // Flip open lock
myservo.write(0); // Close lock

}

When you execute this code with the wiring as described above, you’ll have something that looks a lot like this:

You can adjust the rotation in the code

Congrats! You have achieved the basic functionality of an RFID-enabled lock. Next up, let’s update the code a little bit to ensure that only certain tags can trigger the lock opening.

Make note of the RFID identification string that is printed out from the code above, and then insert it in the code below in the place where I wrote “INSERT ID HERE” (the code below begins where “Authorized Access” was printed in the code above)

if(content == "INSERT ID HERE"){    Serial.println(“Authorized access”);
myservo.write(90); // Flip open lock
myservo.write(0); // Close lock
}
else{
Serial.println("Access Denied");
}

Nice and simple, right? Furthermore, if you want to have this content content displayed on an LED screen, all you have to do is wire the screen to the board (see here) and print there instead of to the Serial Monitor.

So now what? Well, if you want to be done, you’ve certainly accomplished something great. However, now is where the creativity really begins. We have created a mechanism where you can hold a tag up to a reader and make something physical happen. How can we translate that physical motion into real world value to us? Well here are a few ideas:

  • Connect the motor to a string, and the string to the end of a deadbolt lock. Power the arduino by an outlet or batteries and have the code running on it. Mount the RFID reader on the outside of the door, and BAM you have a working deadbolt lock.
  • Change the code so the motor spins more continuously for a predefined amount of time whenever the tag connects, and then spins the opposite way the next time the tag connects. Connect a string from the motor blade to some curtains, and BAM you’ve created automatic opening and closing curtains on your windows. If you wire the reader so it’s next to your bed, you’ve really got an easy life coming.
  • Repeat the first bullet except customize it to work for a small safe or box, allowing you to better protect your private and valuable belongings.

The ideas are endless, and I’m sure you can think of plenty more yourself. Feel free to add them down in the comments! What I implemented was the door lock as explained above. The code I added to the loop was as follows:

for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}

Which will cause the lock to open and the close in quick succession, giving you time to open up the door and close it. I used a makeshift deadbolt with some cardboard and paper clips, but implementing it with the real thing would be very similar and not too difficult. Here is my design in action:

Locking and unlocking my door with a RFID tag

And that’s it for today! Feel free to comment below with your own implementation experiences. Happy making!

--

--