For your safety and security, we bring to you a DIY smart lock that has the capability to remove all these security threats and problems.
Before starting this project, we need to get these materials ready first.
- Arduino UNO
- Bluetooth Hc05
- Some Wires/Jumper Wires
- Servo Motor
- 5V Battery/Power Bank
- USB Type B for Programming Arduino
Bill of materials
As we have all the components now, we can start our project.
First, we need to include the library and declare variables needed, as in snippet of code. We include a servo library, then create a string array to generate a password. After this, we need to create a few more string variables to store password, OTP and LED pin numbers as in snippet Fig.1 of code.
In the second part of coding, we need to set up serial and Braud rate for Bluetooth. Here I have used 9600 Braud rate but if it didn’t work, you can use default Braud rate of Hc 05 i.e (38400). Then, we have to set up a pin for servo using servo.attach (PWM pin number). After that, we can define pin mode as output for led refer (Fig.2)
In the third part of code, we will create a loop and check the data coming from Bluetooth.Then we create if() statement to check device id. If it matches, then it calls otp() function for generation of OTP(refer Fig. 3)
Then we need to create check() function to check whether OTP is correct or not. If it is correct then it turns servo to open position (refer Fig. 4).
Congrats! We have completed the coding part.
Now, let’s make Android APP for our project. We can make App with two different platforms, either Android Studio or MIT app inventor. For this project, let’s choose the MIT app inventor because it’s easy to make an App with blocks without coding. Comment below if you want me to build App with Android Studio.
First, we need to create a layout as in the pic (Fig. 5). Just drag and drop or download the .aia file for it from the link below.
Now let’s get into the code Blocks tab, for coding.
First, we need to initialise Bluetooth list available for connection. Then we have to set button function to send device id according to our Arduino Code as in the pic below (Fig. 6).
Now, export app .apk and install it on your Android phone.
You can get .apk, .aia file, and code from the link below.
We are almost done now, we have completed our coding and app building. Let’s set up the components and begin connections.
|Arduino Pins||Components and Pin|
|Arduino Pin 9(pwm)||Servo Yellow Wire (signal input wire)|
|Rx||Bluetooth Module TX|
|Tx||Bluetooth Module RX|
Whole circuit and connections are illustrated in the pic below (Fig. 7).
We have connected all the components now. You might not like to fry costly Arduino board with the wrong connection. So, always crosscheck your setup to ensure that all connections are ok.
Hurray! We have now completed our awesome project on OTP-based lock system. Just plug it and follow the steps in the video for testing
First, connect the Arduino and components to a power supply. Here I have used 5V Power Bank for it. Next, open the installed App, then turn on the Bluetooth of the phone. When you tap on the Bluetooth icon, you will get the list of Bluetooth connections for pairing. Now, tap on HC 05. On successful pairing, you will get a ‘connected’ message on the App as in Fig 8.
After that, tap on the key icon to send device id to match. If the device id is matched, it will send an OTP to your app that you can see in App text bar between Bluetooth and lock icon.
Now, you can tap on the lock icon to unlock your Smart Lock. If everything is ok then servo moves unlocking mechanism and onboard LED of Arduino lights up indicating successful unlock. I have used servo because of its high torque, and also because we can control the angle of its movement that helps in unlocking mechanism ofthe lock.
Can I supply power to Servo from Arduino?
No, never do that. You might have seen in different sites, where the servo is directly connected to 5V pin of Arduino, but it not safe. Arduino is not made for that amount of current. Don’t try to fry your costly board.