Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

Bluetooth Control Password Protected Door Lock

3.17/5 (6 votes)
6 Aug 2018CPOL6 min read 23.3K   462  
A password protected door lock based on Arduino which can be controlled by a smartphone over Bluetooth

Image 1

Introduction

This smart Lock is secure, simple, and easy to manage your home’s lock. This lock needs no keys and the lock is attached inside the door and you can control it from outside the door using Bluetooth. As the lock is inside the door, there is no way in which a thief can break the door. An Android application is required to open and close the lock and I will explain the details of how you can develop an Android app in the later part of the tutorial. A password is sent to the lock using Android app and if the password is matched to your preset-lock password, then the lock will be open and will send a feedback to your phone like the lock is open. Before reading further, you can watch the demo video:

Bill of Materials

  1. Arduino UNO (buy it from gearbest.com): This is the main controller board for the lock.
  2. Bluetooth Module HC-05 (buy it from gearbest.com): This is a low-cost most popular Bluetooth module used in Arduino projects. Using this module, you can transfer data from your smartphone to Arduino microcontroller board to control any device over Bluetooth.
  3. A 9g Micro Servo (buy it from gearbest.com): This is a micro servo motor. This motor can be rotated from 0 to 180 degree very precisely using a microcontroller. This motor is used here to move the lock.
  4. A 3D Printed Door Lock (.stl file is attached): The original version of the lock was designed by Thingiverse user Michal Altair Valasek. Thanks to Mr. Valasek for his creative design. I have made a slight modification on his design.

To print the lock, you will be needed to access a 3D printer. If you have a 3D printer, that's good. If you don't have a 3D printer, you can buy it only at $145 from here. Actually, gerabest.com sells it as a kit. So, you can also get the joy of making a 3D printer by yourself. Making it is not hard.

Schematic Diagram

Image 2

The connection between the components is very simple. The HC-05 module has four pins to connect with Arduino and the servo motor has three pins to connect with Arduino. The schematic clearly shows the connection. For further clarification, follow the table below:

Connection between Arduino & HC-05 Module

Connection # Arduino Pin HC-05 Pin
1 5V VCC
2 GND GND
3 TX RX
4 RX TX

Connection between Arduino & Servo Motor

Connection # Arduino Pin Servo Motor Pin
1 5V VCC
2 GND GND
3 Digital Pin 9 Signal

Connect the HC-05 Bluetooth module accordingly. The connection between TX, RX pin is a little bit misleading. But the logic behind that, what Bluetooth module transmit is received by Arduino. For this reason, TX pin of the Bluetooth module is connected with the RX pin of Arduino and vice versa.

Another thing you should keep in mind that Arduino cannot upload any code when a device is connected to RX, TX pin. So, before uploading a sketch to the board, you must remove the connection of TX and RX pin.

Using the Code

The code for the bluetooth control smart door lock is very simple. I have designed an Android application which sends a command with the password. Actually, I set two commands, one is "OPEN=" and another is "CLOSE=" and password may contain any digit, later or symbol or combination of them as you like. In the app, I used two buttons - one for open the door and another for closing the door. When a password is given to password box and the open button is clicked, then app the joined the "OPEN=" command with the password and sent it to Arduino. I add '=' sign with the command and it works as a delimiter. After receiving the string from phone Arduino program separates command and password from the received string and save them into two separate variables. Then check the password first, if password matched with saved password, then open the door for "OPEN" command, and close the door for "CLOSE" command. "=" helps to separate command and password.

The following code snippet is used to receive the data from the Bluetooth module. Arduino communicates with the Bluetooth module using serial protocol. When the serial port receives any data, then a serial interrupt occurs and the following function is called automatically.

C++
void serialEvent() 
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    //Serial.write(inChar);
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline or a carriage return, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n' || inChar == '\r') {
      stringComplete = true;
    } 
  }
}

After receiving the data from the Bluetooth module, the following code snippet helps Arduino to separate command and password from the received data.

C++
// identified the position of '=' in string and set its index to pos variable
   int pos = inputString.indexOf('=');
   // value of pos variable > or = 0 means '=' present in received string.
   if (pos > -1) {
     // substring(start, stop) function cut a specific portion of string
     // from start to stop
     // here command will be the portion of received string till '='
     // let received string is open=test123
     // then command is 'open'
       command = inputString.substring(0, pos);
     // value will be from after = to newline command
     // for the above example value is test123
     // we just ignore the '=' taking first parameter of substring as 'pos+1'
     // we are using '=' as a separator between command and value
     // without '=' any other character can be used
     // we are using = menas our command or password
     // must not contains any '=', otherwise it will cause error
       value = inputString.substring(pos+1,
               inputString.length()-1);  // extract command up to \n excluded
       //Serial.println(command);
       //Serial.println(value);

Then an if else block is used to compare the received password with the preset password and according to this drive, the servomotor to open or close the lock.

C++
// password.compareTo(value) compare between password tring and value string, 
// if match return 0 
    if(!password.compareTo(value) && (command == "OPEN")){
          // if password matched and command is 'OPEN' than door should open
           openDoor();              // call openDoor() function
           Serial.println(" OPEN"); // sent open feedback to phone
           delay(100);
           }
    else if(!password.compareTo(value) && (command == "CLOSE")){
          // if password matched and command is 'CLOSE', then door should close
           closeDoor();
           Serial.println(" CLOSE"); // sent " CLOSE" string to the phone 
           delay(100);
           }
    else if(password.compareTo(value)){
          // if password not matched, then sent wrong feedback to phone
           Serial.println(" WRONG");
           delay(100);
           } 

If Arduino received wrong password, then it sent a message to the Android phone that the password is wrong. Actually, Arduino sent " WRONG" string. Android program then shows wrong password message. The complete sketch is attached within the zip file.

Door Lock 3D Design

All the parts of the lock are 3D printed. I would like to thank Thingiverse user ridercz for his nice design. He designed and published this servo based door lock in thingiverse.com. All the files except Rack_Holder.stl are taken from his design. You can download the files from below or from Thingiverse.com.

Image 3

Some files have LT version. Light version required less PLA compare to normal version and has less strength. I printed LT version and working good. I used Anet A8 for printing all the parts. Photos of the printed parts are attached.

Assembling 3D Printed Parts

Image 4

To assemble all the printed parts, you may follow the video instruction below. After assembling, use four screws to tightly fix all the parts.

Developing Android Application

Image 5

I used MIT App Inventor to develop the application for my Android pone. I am using App Inventor because it doesn't required any coding and no software installation. You only need a Google account. Go to http://ai2.appinventor.mit.edu/, you will ask to log in using google account.

Complete App Inventor source file (BTcontrol.aia) is attached herewith. If you did not like to make your own app or modify it, just download the BTcontrol.apk and install it to your Android phone.

You can also download it from Google Play Store using the link:

For modifying the source, click the "Projects" menu and select "Import project (.aia) from my computer" shown in Figure 24 and browse the BTcontrol.aia file from your computer. After importing, you can modify it easily.

If you want to know more about Android app developing using App Inventor, browse http://appinventor.mit.edu/explore/ai2/tutorials.html.
You can also try http://meta-guide.com/videography/100-best-appinventor-videos/.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)