In this project, we are going to make an OTP-based smart wireless locking system as you can see in (Fig. 1). This smart lock can generate a new password every time you unlock it, which further enhances your security level.
This DIY device is much safer than the traditional key-based system and electronic wireless lock system. If you are still using the key-based system, you are likely to land in a big problem if your key gets lost or stolen. The electronic wireless lock system is not safe either. You might forget the password and there is also a high risk of being hacked.
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.
- 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.
Smart Wireless Locking System – Coding
First, we need to include the library and declare the variables needed, as in a 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 the code.
In the second part of coding, we need to set up serial and Braud rates for Bluetooth. Here I have used the 9600 Braud rate but if it didn’t work, you can use the default Braud rate of Hc 05 i.e. (38400). Then, we have to set up a pin for the 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 the code, we will create a loop and check the data coming from Bluetooth. Then we create an if() statement to check the device id. If it matches, then it calls otp() function for the generation of OTP(refer to Fig. 3)
Then we need to create a check() function to check whether OTP is correct or not. If it is correct then it turns the servo to an open position (refer to Fig. 4).
Congrats! We have completed the coding part.
Now, let’s make Android APP for our project. We can make an 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.
Recommended: Check more such interesting Arduino Projects.
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 initialize the Bluetooth list available for connection. Then we have to set the button function to send the device id according to our Arduino Code as in the pic below (Fig. 6).
Now, export the 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|
The 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 a 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 an OTP-based lock system. Just plug it in 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 the device id to match. If the device id is matched, it will send an OTP to your app that you can see in the App text bar between Bluetooth and the lock icon.
Now, you can tap on the lock icon to unlock your Smart Lock. If everything is ok then the servo moves the unlocking mechanism and the 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 which helps in the unlocking mechanism of the lock.