Introduction: Arduino RFID Deadbolt Modification: Using an Arduino UNO Microcontroller and an RFID Sensor to Lock and Unlock a Deadbolt With a Servo

Picture of Arduino RFID Deadbolt Modification: Using an Arduino UNO Microcontroller and an RFID Sensor to Lock and Unlock a Deadbolt With a Servo

Purpose

The purpose of this document is to assist anyone in modifying and modernizing existing deadbolts with a microcontroller RFID module that will allow keyless locking and unlocking. This guide steps through the process of gathering parts, prototyping a circuit, utilizing pre-written code, and finalizing the circuit to get the device working properly. After completing this Instructable, you will have a working device that can be used to modify an existing deadbolt to lock and unlock with a keycard or keyfob. Mounting the device is not included in this particular guide.

Disclaimers

  1. There will be some soldering involved with this project as it requires the user to move from the prototyping stage to actually building the device in a way that allows for rigidity and durability. If you don't have soldering skills or are a little rusty when it comes to soldering, check out some tutorials on it on YouTube or find a friend with the skills that is willing to help you out.
  2. I will not be going over how to code or installing the Arduino IDE in this guide, it is assumed that the user has a very basic knowledge in programming and already has the Arduino IDE installed and properly configured for their computer and controller. Now that we've gotten that out of the way, let's move on to the actual fun!

Step 1: Materials Needed

Picture of Materials Needed

The materials listed each have a link to their corresponding amazon page. You can also use the part number and search other areas online to see if there is cheaper price available. You will need 14 items to complete this project.

1. Arduino Uno - https://www.amazon.com/Elegoo-Board-ATmega328P-ATM...$10.90

2. Arduino compatible RFID Sensor (RC522) - https://www.amazon.com/HiLetgo-MFRC-522-MFRC522-Re...$5.49

3. Breadboard (solderless) - https://www.amazon.com/Solderless-Breadboard-jumpe...$7.49

4. Servo (TowerPro MG995) - https://www.amazon.com/REES52-MG995-TowerPro-Digit...$10

5. Power Supply (RAVPower 10000mAh Power Bank) - https://www.amazon.com/Portable-RAVPower-10000mAh-...$15.99 (with deal)

6. 3 total LEDs - 1 of each LED (green, red, and yellow) - https://www.amazon.com/Haobase-Assorted-Diffused-E...$9.99

7. 3 total 330 Ohm resistors - https://www.amazon.com/Projects-25EP512330R-330-Re...$5.84

8. Ribbon cable (16pc, length variable) - https://www.amazon.com/uxcell-1-27mm-16-Pin-Conduc...$8.25

9. Arduino shields "Header Boards" (Can buy pre-soldered if desired) - https://www.amazon.com/dp/B01J1KM3RM/ref=sxbs_sxwd...$13.80

10.Male Header Pins - https://www.adafruit.com/product/400?gclid=Cj0KCQiA0vnQBRDmARIsAEL0M1mjiEdZByRi6sgPeKH_iWeatDHCGDEZIM7jeGzf7lCK0Q-1AisvxlMaAnSCEALw_wcB$3.00

Total Cost: $90.75


Optional Materials:

1. Project Box(es) (size varies with configuration)

2. A Deadbolt

3. Mounting material of choice (velcro, superglue, screws, etc.)

Step 2: Understanding the Components

Overview

The device itself consists of an Arduino UNO microcontoller that controls an RFID and Servo attachment. Other components such as LEDs are added to the design to allow for understanding what state the door is currently in. Once all these components are properly configured, the device itself is ready to be used on items such as a standard deadbolt.

  • The servo is contained within a mounting box, along with the controller, that then surrounds the deadbolt allowing for a point of contact between the servo and the deadbolt.
  • Wire is run from the deadbolt mounting box to an external mounting box that contains the LEDs and RFID reader.
  • The box is placed on the outside of the door so that the user can easily scan a keycard for access.
  • All of this is wired together and run off of an external power supply to allow for continuous use.
  • Once everything has been configured properly, the door lock should be automated given that the door only uses a deadbolt as its locking mechanism.

The image shown at the beginning of the guide is the basic design for the device. Each component has a certain number of inputs and outputs and those each correspond with a pin on the Arduino Microcontoller. These pins are the suggested connections for each of the components as the code will reflect these specific pin numbers for each and the servo requires a PWM (pulse-width modulation) capable pinout to be able to function properly. The PWM output allows a pulse of a certain duration to be sent to the servo, depending on the size of the pulse the servo is able to determine what angle it should be turned to. Smaller pulses mean smaller angles and larger pulses allow for larger angled turns. The connections from the Arduino to each component can be made via a solderless breadboard and some jumper wires. This method is best for prototyping because it allows for quick changes to be made with pin connections and also allows for easier tracing and debugging of the circuit. The picture above shows how to set up the rails of the breadboard with grounds and voltages (3.3, 5, etc.) to allow for multiple power connections to be made right on the board without having to clutter up the Arduino pins.

Step 3: Assembling the Hardware

The pins for both the components and the arduino are also specified below.

Step 1: Connect the pins from the RFID sensor to the corresponding Arduino Pins as shown below

RFID RC522 ---> Arduino[1]

MOSI -------------> Pin 11

MISO -------------> Pin 12

SCK ---------------> Pin 13

SS (SDA) --------> Pin 10

RST ---------------> Pin 9

3.3V ---------------> 3.3V

GND --------------> GND

IRQ ---------------> (No pin)

Step 2: Connect the pins from the Servo to the corresponding Arduino Pins as shown below

TowerPro MG995 SERVO ---> Arduino

The servo has three different colored wires protruding from it which allow the connections to be made, the colors for this particular servo are specified, however, other servos should contain a similar color scheme.

(ORANGE) PWM --------------> Pin 6

(RED) Power -------------------> 5V

(BROWN) GND ----------------> GND

Step 3: Connect the pins from the LEDs to the corresponding Arduino Pins as shown below

LEDs ---> Arduino

Each LED is connected to its respective microcontoller pin via its anode (long leg, positive) while the cathode (short leg, negative) is connected to a 330 Ohm resistor that is then run to ground. The reason for the 330 Ohm resistor is to lower current flow through the LED to ensure that the LED does not burn up. The resistor does not need to be omitted but the value can be changed; a larger resistor means a dimmer LED and a smaller resistor means a brighter LED. Just make sure not to make the resistor too small as it can allow too much current flow and burn up the LED, as previously stated.

RED --------------> Pin 2

GREEN ----------> Pin 4

YELLOW --------> Pin 7

Once all the connections have been made, it's time to connect the controller up to your computer and upload some code to test it out!

Step 4: Understanding the Software

This section is, in my opinion, one of the best parts, because we can now see how everything plays with each other. Once all the steps above are completed it's time to work on the "brains" of the device.

  1. The first thing we will need to do is find out, one, if your RFID reader is working and, two, find out what the ID values are for the keycards that will be used on this device. The first picture below shows the code (it is also pasted below) and gives explanations about what each section of the code is doing. What you will want to do is open up the serial monitor (Top right corner) from the IDE when running this code so that it can output the ID values and you can write them down for the next section of code.
  2. Once the values have been found for your card(s) we can then move on to the "meat" of the code. Open up a new sketch and paste the code below into it. This code will help interface everything you have built thus far. It is not terribly complicated but it is lengthy so be careful when changing things or adding to it and make sure you don't make any small errors (trust me, those will always be the hardest and most time consuming ones to find). Save often and make copies if you're just starting out! I will put pictures of what the below code looks like in the IDE to help with any confusion that may arise when it comes to copying and pasting it from here into your personal software.

Overall Pseudo-Code

The whole idea behind the code above is actually quite simple when translated into sentence form.

  • Begin by pulling in any libraries that may be utilized during the coding process.
  • Then you initialize any variables that may be needed throughout the code, this includes the servo and RFID components. Notice they are in the "global space" of the code to allow easy access from anywhere within the code. Also, notice that this is where the vector IDs for the cards are found. As you accumulate more and more card IDs, more can be added to the vector so that each of the IDs is granted access to the door, all you have to do is follow the same format for each card.
  • In the setup section, we make sure that all initial values (myservo.write(0) means set the initial position of the servo to 0) are set and that the serial monitor and rfid reader have been initialized.
  • The loop code is next. This code is what is executed continuously while the device is on.
    • First, the code reads in each of the five values from the keycard and stores them in a vector.
    • It then compares those values to the existing vector IDs that have been granted access. If it finds that the values do not match with one of the vector IDs in the previously declared variable then the loop breaks and jumps down to where the Red LED is turned on. (Notice that the serial monitor has something printed to it to show us that everything is working properly, this is a great way to step through code and debug when something is not working properly.)
    • However, if the scanned ID is a match, then we look at the state of the lock.
      • If it is true (meaning the door is locked), we move to the appropriate section of code, turn on the Green LED and unlock the door.
      • If the state of the lock is false (meaning it has already been opened), then we move to the other section which turns on the Yellow LED and locks the door back.

The code itself may look a little overwhelming especially to those that are new to this, but if you can step through it as we have done and reason what each section is doing then it is not so difficult and can actually be fun!

Step 5: Creating and Uploading the Code

Picture of Creating and Uploading the Code

Step 1: Checking Card IDs for future use

Card ID Code [2]

#include SPI.h (Add <> around when using in IDE)

#include MFRC522.h (Add <> around when using in IDE)

#define RST_PIN 9

#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {

Serial.begin(9600);

Serial.print("The Monitor is working!");

SPI.begin();

mfrc522.PCD_Init(); }

void loop() {

RfidScan(); }

void dump_byte_array(byte *buffer, byte bufferSize) {

for (byte i = 0; i < bufferSize; i++) {

Serial.print(buffer[i] < 0x10 ? "0" : " ");

Serial.print(buffer[i], HEX); } Serial.print("\n"); }

void RfidScan() {

if ( ! mfrc522.PICC_IsNewCardPresent())

return;

if ( ! mfrc522.PICC_ReadCardSerial())

return;

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); }

Step 2: Writing and Repurposing Code for Device Use

Complete Device Code [1]

#include SPI.h (Add <> around when using in IDE)

#include RFID.h (Add <> around when using in IDE) [3]

#include Servo.h (Add <> around when using in IDE)

#define SS_PIN 10

#define RST_PIN 9

RFID rfid(SS_PIN,RST_PIN);

int RED = 2;

int GREEN = 4;

int YELLOW = 7;

Servo myservo;

int serNum[5];

int cards[][5] = { {72,133,173,137,233} //{195,3,85,217,76} };

bool access = false;

bool lock = true;

void setup(){

Serial.begin(9600);

SPI.begin();

rfid.init();

pinMode(RED, OUTPUT);

pinMode(GREEN, OUTPUT);

pinMode(YELLOW, OUTPUT);

myservo.attach(6);

myservo.write(0); }

void loop(){

if(rfid.isCard()){

if(rfid.readCardSerial()){

Serial.print(rfid.serNum[0]);

Serial.print(",");

Serial.print(rfid.serNum[1]);

Serial.print(",");

Serial.print(rfid.serNum[2]);

Serial.print(",");

Serial.print(rfid.serNum[3]);

Serial.print(",");

Serial.print(rfid.serNum[4]);

Serial.println("");

for(int x = 0; x < sizeof(cards); x++){

for(int i = 0; i < sizeof(rfid.serNum); i++ ){

if(rfid.serNum[i] != cards[x][i]) {

access = false;

break;}

else {

access = true;}

}

if(access) break;}

}

if(access and lock){

Serial.println("Welcome!");

lock = false;

digitalWrite(GREEN, HIGH); // HIGH with regular relay

myservo.write(125);

delay(1000);

digitalWrite(GREEN, LOW);}

else if(access and !lock){

Serial.println("Goodbye!");

lock = true;

digitalWrite(YELLOW, HIGH); // HIGH with regular relay

myservo.write(0);

delay(1000);

digitalWrite(YELLOW, LOW);}

else {

Serial.println("Not allowed!");

digitalWrite(RED, HIGH); // LOW with regular relay

delay(1000);

digitalWrite(RED, LOW);}

}

rfid.halt();}

Step 6: Understanding the Final Product

Picture of Understanding the Final Product

After you have tested your device and everything works properly with minimal issues, then you can move on to finalizing the design and soldering everything together. The suggested ribbon cable in the materials list will come in handy when soldering because it can be cut to the length needed and used to connect the LEDs and RFID reader to the arduino from a reasonable distance.

  • My plan is to have the cable go from the microcontroller mounted on the deadbolt, down the door and under it, and back up to the area around the keyhole where the reader and LEDs could be mounted within a clear project box. The reason for this is because the reader does not have enough range to read through the door and the clear project box allows for the LEDs to be seen while being protected from any outside elements. It then only makes sense for the access indicators to be seen and that was my solution for both problems at once.
  • Once you have soldered one side of the ribbon cable ends to the LEDs and reader, the other ends can be soldered to the respective pin slots on the header board for the Arduino. The header board is used because it allows for the Arduino to be repurposed easily in the future if you decide to do a different project and no longer need the locking device. Soldering header pins into the slots of the of the header board allows it to fit snuggly into place above the Arduino and then other soldered connections can be made to those specific pins. The picture above shows where to put the header pins and shows how extra solder connections can be made to those specific pins.
  • The servo can be soldered in the same way but does not require the ribbon cables because of its close proximity to the Arduino. It will be housed in a project box along with the Arduino so that the brains and brawn of the circuit can be within close proximity of each other and live right above the deadbolt. This allows for easy connection and minimal loss between the two devices and ensures they will not be tampered with since they are on the inside of the door. After all the connections have been made then you can house the circuit and mount it to the door.

As of now, and shown in the picture above, I only have a finalized working "prototype" because the ribbon cable is far too short and the Arduino does not yet have a mounting box that can contain all the parts needed to fit around the lock on the door. But the steps below will give you the basic direction of how to get as far as I have or can be used as a springboard for you to finish the design and make it fully functional and mounted on a door.

Step 7: Creating Final Product

Picture of Creating Final Product

This is the step mentioned in the overview where it will be handy to either have soldering skills or have a friend that is reasonable with a soldering iron.

Step 1: Solder the Header Pins onto the the Header Board

  • Cut the proper amount of header pins from the prepackaged line to fit into the slots on the header board.
  • Solder the cut Male header pins on the header board so that the pins will fit into the Female slots on the Arduino.
  • Or just purchase Header boards with the pins pre-soldered if you want to skip this step.

Step 2: Cut the Ribbon Cable to the length desired and remove excess cables from the group to save from clutter

  • I only used 11 out of the 16 cables for my connections between the RFID/LED board and the header board for the Arduino but use as many as is necessary to make the connections needed.
  • I made the length shorter than was needed as I am not ready to mount the device but you can measure the length needed for mounting it on the door and use that length of cable.

Step 3: Solder the RFID Sensor and LEDs with Resistors onto another header board or perfboard

  • I measured the size of the board and the size of the clear mounting box I had to ensure that the board would fit into the box.
  • Solder on the RFID in a way that will still allow the board to fit into the box if you have one.
  • Finally, solder the LEDs and resistors to the remaining part of the board not covered by the RFID sensor and make sure that the resistors are terminated close enough together that a ground connection can be made easily to all of them via solder.

Step 4: Solder the ribbon cable to the pins of the RFID sensor and LEDs (Do not forget the ground connections for the resistors!)

  • You may have to peel the ends of the ribbon cable wires apart so that you have enough individual wire to reach the pin holes, but don't peel the wires apart completely. Also, you will have to strip the ends of the wire and twist the strands together so that they can be pushed through the pin holes easily.
  • Solder the connections in a way that makes sense to you and allows for the easy connection between this board and the header board that will connect to the Arduino.
  • Remember to solder a ground to the termination points of the resistors so that there will be current flow through the LEDs. Without this connection there will be no light in your device.

Step 5: Solder the other end of the ribbon cables to the proper pins on the Header Board

  • If you have a project box, go ahead and cut out a hole that will allow the ribbon cable to fit through it and put the RFID/LED board into it.
  • Then do the same thing as stated above for the the connections to the header board of the Arduino.
    • Peel the wires.
    • Strip the ends of the wires and twist the strands together.
    • Solder the wire to the appropriate pin on the header board. This can be tricky since the ribbon cable wires are so close together. Always double check and ensure you are soldering the proper wire to the right pin on the header board so the correct connections can be made otherwise the board won't work or you'll have to do some tweaking with the code.

Step 6: Solder the wires onto the Header Board to complete the Servo connection

  • Solder three extra wires onto the correct header pins for the Servo as stated in one of the previous sections about the connections of the components.
  • Then connect aforementioned wires into their proper pinholes on the servo wires.

Step 7: Connect the Header Board to the Arduino

  • Pretty self explanatory, but just push the header board down onto the Arduino and make sure all the pins are making a connection with the female connectors.

Once you have completed the above steps, you can plug your battery pack into the USB port of the Arduino and everything should light up or give some indication that it is receiving power. From here you can test the final device and ensure everything is working properly. If not, go back through the steps and make sure all the connections have been made properly and you didn't get any of the wires mixed up. Also, check the code to ensure it reflects all the pin numbers you have selected for your device and nothing is wrong in that regard.

Final Thoughts
This step can be modified to fit your needs, but the whole reason I have implemented my device this way is to allow for the most minimal alterations to be done to the existing door and deadbolt so that the device is easy to apply and remove when needed. Unfortunately, I will not be covering the mounting process in this tutorial but will hopefully be able to make another guide in the near future that steps through housing and mounting the device to a door so that it may be utilized properly. Even though this may be the case you can always mount the device in any way you choose now that you have the completed working device!

Lastly, good luck on your future projects and thank you for following along with me on this journey of simple automation!

Step 8: References

Comments

Swansong (author)2017-11-29

Great first instructable, thanks for sharing!

About This Instructable

610views

18favorites

More by parkercdavis:Arduino RFID Deadbolt Modification: Using an Arduino UNO Microcontroller and an RFID Sensor to Lock and Unlock a Deadbolt With a Servo
Add instructable to: