Compact, Low-cost RFID Door Lock

by typing10 in Circuits > Arduino

30 Views, 0 Favorites, 0 Comments

Compact, Low-cost RFID Door Lock

IMG_0696.jpg

RFID cards are used everywhere, but one of the most common places to find them is in door locks (such as those in a hotel). This easy DIY project will walk you through the process of creating your own compact RFID door lock, without the need of a 3D printer or CNC machine. Besides the electronics, this door lock is 100% recyclable and was made from easily salvaged materials, which keeps the price and difficulty of the build down.

Supplies

IMG_0680.jpg
  1. Microcontroller (I'm using an off-brand Arduino Nano, but any microcontroller should work - keep in mind that you may have to modify the code!)
  2. RFID module (RC522)
  3. 9g servo
  4. LED + resistor (anything at or above 220 Ohms is fine)
  5. Breadboard and wires (you can skip this if you have some other way to wire everything up; depending on where you want your LED to go you may need another two female-to-male jumper wires)

Other items

  1. Cardboard (8" by 8")
  2. Something to cut cardboard; e.g. scissors, Exacto knife, razorblade
  3. Hot glue gun (or other fast-drying glue)

With all your items collected, let's start building!

Wiring

IMG_0683.jpg

The wiring for this project is a little like spaghetti, but taking it one step at a time makes it easy.

To begin, place the microcontroller on the breadboard. Connect the RFID module like so:

  1. RFID module --> Microcontroller
  2. 3.3V --> 3.3V (Also sometimes labeled as 3V3)
  3. RST --> D9
  4. GND --> GND
  5. IRQ --> nothing (this pin is unused)
  6. MISO --> D12
  7. MOSI --> D10
  8. SCK --> D13
  9. SDA --> D10

Now, connect the servo:

  1. Servo --> Microcontroller
  2. +5V --> 5V
  3. GND --> GND
  4. Signal --> D2

Finally, connect the LED and resistor (you may want to use jumper wires between the LED + resistor and microcontroller:

  1. LED Cathode (shorter) --> Microcontroller GND
  2. LED Anode (longer) --> Resistor
  3. Resistor --> D3

Congrats; the hardest part is over!

Construct the Enclosure

IMG_0690.jpg
IMG_0687.jpg

This enclosure is designed to be a simple and easy to build as possible, and has the dual effect of re-using scrap material and being 100% recyclable. It requires no 3D printing, just a little cardboard. The main 4-segment section of the enclosure is 8" long (four 2" segments) and 5.25" wide, and the small square protrusion (which will become the roof) is 2" long and 2" wide. Warning: do not cut along the black lines.

Once you have cut out the shape, it is time to make a hold for the LED on the "front" segment (which segment this is doesn't matter). I used a dremel tool, but a pencil or screwdriver punched through works just as well.

Next, fold the shape up into a rectangular prism, following the sharpied lines. The four rectangular sections will form the walls, and the square will form the roof. You can fold the cardboard as is, or you can make a shallow cut along the lines. This will make it eaisier to fold and produce a cleaner enclosure. Don't glue anything yet; we still need to add the electronics. Instead, after folding the enclosure up, lay it back down flat.

Inserting the Electronics

IMG_0688.jpg
IMG_0693.jpg

Let's combine the electronics with the enclosure. This step will require hot glue, so make sure to get that ready. Firstly, attach the breadboard with the microcontroller on it to the back of the RFID module (I used duct tape, but hot glue works better). Push the LED into the hole in the enclosure (you may need to extend the LED's reach using jumper wires), then place the RFID module face-down just below the LED. Glue the RFID module to the cardboard and wait for it to harden. Once you are done with this step, make sure that everything is sitting securely on the enclosure. Don't put away the hot glue yet; we'll need it in the next step.

Bringing It All Together

IMG_0696.jpg

Let's take our flat enclosure and turn it into a 3D shape. Fold the four rectangular segments into a rectangular prism. Pull the servo out of the top, then fold the square segment down as a roof. Use hot glue to stick everything together. If you cut joints into the cardboard, you may want to reinforce them with a little hot glue to increase their durability.

The main body of the lock is now complete!

Coding the Lock

Before we attach the lock to the door, we need to upload all the code to the microcontroller. You will need Arduino IDE or some other microcontroller programmer to upload this code. For an install guide, go here. Once you have Arduino IDE, create a new sketch and paste this code into it.

/*
*
* Code based off of: https://randomnerdtutorials.com/
* Modified by typing10
* Modified by Rui Santos
*
* Created by FILIPEFLOP
*
*/
#include <SPI.h>
#include <MFRC522.h>
#include <ServoEasing.hpp>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

ServoEasing servo;

void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();

servo.attach(2, 500, 2500);
servo.easeTo(0, 90);

pinMode(3, OUTPUT);
}
void open() {
Serial.println("Opening...");
servo.easeTo(90, 90);
delay(5000);
Serial.println("Closing...");
servo.easeTo(0, 90);
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();

//copy the UID of your RFID card off the serial monitor, then paste it here (it should look like this: XX XX XX XX)
if (content.substring(1) == "your UID here" || content.substring(1) == "your UID here" /*to add more cards, paste this before this comment: || content.substring(1) == "your UID here"*/) {
open();
} else {
//display red LED when we use an invalid card
digitalWrite(3, HIGH);
Serial.println("Invalid card");
delay(3000);
digitalWrite(3, LOW);
}
}

Once your sketch is complete, plug in your microcontroller and hit upload. Pull up the serial monitor and tap a RFID card or tag to the front of the lock. You should see the UID pop up. Copy the UID (which will look like XX XX XX XX) onto line 65 of the code, then re-upload it. Your RFID card or tag should now make the servo move. All other cards or tags will make the red LED light up, signaling that they are invalid. If you're still having trouble, I've included a troubleshooting guide at the bottom of the page.

Adding the Locking Mechanism

IMG_0699.jpg

The last part of the lock is the locking mechanism. The locking mechanism is purposefully barebones because all doors are different, and a complex design would not be able to adapt to all types of doors. You will need to adapt this design to your specific door. The only requirement is that the bar is long enough to extend from your door to the doorframe and that the end of the bar is flexible (so it doesn't break if the door opens without the lock activating). I used a popsicle stick and some flexible cardboard to form the bar, and angled it upward to fit over my doorframe. It helps to dry-fit the lock and servo to the door to see where everything should sit.

Attach the Lock to the Door

IMG_0702.jpg

To finish out the project, let's attach the lock to the door. I used hot glue, as it will peel off easily and not damage the paint (you may want to test this on your specific door, just to be safe. Start by gluing the main door lock to the door, then glue the servo down. Plug everything in and give it a test. If you've done everything right, the lock should function and you have completed the project! If you're having trouble, scroll down to the troubleshooting guide.

Have Fun!

If you've made it this far, great job! The door lock is now finished. Have some fun with it - there's nothing more satisfying than having a DIY project finally working. Still can't figure it out? Scroll down to the troubleshooting guide for troubleshooting tips. If you want to take this project a step farther, I've included a list of possible upgrades.

  1. Fingerprint sensor
  2. Mobile notifications (for failed and successful entries)
  3. Converting the lock into a safe
  4. Failed-entry alarm
  5. "Too many attempts" lockout
  6. Remote opening / closing

I hope you enjoyed this Instructable! I really enjoyed making this lock and tackling the challenges I stumbled upon, and I hope to revisit it in the future. Until then, have a fantastic rest of your day!

Downloads

Troubleshooting

If you're reading this, your lock likely is not working, so let's go over some possible issues and how to fix them.

My microcontroller won't turn on

Test your power source - make sure it is working. If you are using a computer USB port, try switching to a different port. Try using a different cable to connect the microcontroller. Check for misplaced wires; you may have a short in your circuit. If none of these options work, your microcontroller may be broken. See if you can get it to turn on outside of the door lock circuit. If not, you may need a new microcontroller.

The lock will not accept my RFID card or tag

Check to make sure the allowed UID matches your card UID (to find your card UID, use the serial monitor). If the serial monitor shows that the lock is opening, but the servo isn't moving, refer to "My servo isn't moving when I present the right card".

My RFID reader isn't detecting any cards

Thoroughly check all your wiring; you may have a misplaced wire. Make sure all the wires have good connections. Check to see if your cards are RFID cards (holding them up to a bright light will reveal an antenna in RFID cards, but no antenna in normal cards). You may need to individually test the RFID module; there are many tutorials online for this.

My servo isn't moving when I present the right card

Check the serial monitor when you are presenting the card. If if doesn't say anything, refer to "My RFID reader isn't detecting any cards". If it says "Opening...", check your servo connections to make sure they are correct. You may need to test the servo individually to see if it is broken; a simple Google search of "controlling a servo with Arduino" should yield a plethora of tutorials.

I don't see anything on the serial monitor

Check that your microcontroller is on. This seems pretty obvious, but sometimes the issue is that the microcontroller is not powered on. Usually, an indicator light will show that it is working, and the serial monitor should have a 'connected' status. If there are still connection issues, try using a different cord, using a different USB port, or restarting your device.