Tuesday, March 19, 2024

OTP-Based Smart Wireless Locking System Using Arduino

By Ashwini Kumar Sinha

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.

Before starting this project, we need to get these materials ready first.

- Advertisement -

Components Required

  • Arduino UNO
  • Bluetooth Hc05
  • LED
  • Some Wires/Jumper Wires
  • Servo Motor
  • 5V Battery/Power Bank
  • USB Type B for Programming Arduino

Bill of Materials

Smart Wireless Locking System Components

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.

- Advertisement -
Smart Wireless Locking System Arduino Code
Fig 1. Arduino 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)

Smart Wireless Locking System Code
Fig 2. Setting I/O and Bluetooth

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)

Arduino Code for Smart Wireless Locking System
Fig. 3 Arduino code checking device Id

Then we need to createcheck() 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).

OTP based Smart Wireless Locking System Code
Fig. 4 Arduino code for checking OTP

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.

APP Building

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.

Smart Lock App Design
Fig. 5 Layout of App in MIT app inventor

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).

Arduino Code Blocks
Fig. 6 MIT app inventor coding blocks

Now, export the app .apk and install it on your Android phone.

You can get .apk, .aia file, and code from the link below.

Download Source codes

We are almost done now, we have completed our coding and app building. Let’s set up the components and begin connections.

Connecting Components:

Arduino Pins Components and Pin
Arduino Pin 9(pwm) Servo Yellow Wire (signal input wire)
Rx Bluetooth Module TX
Tx Bluetooth Module RX
GND GND Bluetooth
VCC Bluetooth VCC
Pin 12 LED
GND Battery(-VE)

The whole circuit and connections are illustrated in the pic below (Fig. 7).

Arduino Smart Wireless Locking System Connection
Fig. 7 Connections of components.

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

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.

Smart Lock System App
Fig. 8 Showing connection status

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.

91 COMMENTS

    • You can downlode the code and .apk file by clicking on “Downlode code” written in red color. Whole project is in zip file when you click on that, the zip file automatically get downloaded in you PC. You will also get .aia file, you can import that file in MIT App inventor and make changes in app like changing the Device ID, and layout.

    • Turn on the Bluetooth of your phone
      Open app, Tap at the Bluetooth icon
      Tap at HC05 for connection,(wait few sec for connection).
      Tap on the key icon to send Unique Device Id ,
      when you got OTP from Arduino it will display in text box next to key Icon.
      Now tap on Lock Icon on Top to Unlock.

    • Have you used the same code given in link? if yes then try to reset board and reconnect the Bluetooth to fix it.If still not solve reply with full problem you are facing,Servo motor only move when you tap on lock icon, and also wait few second when you press key icon to get the OTP. When you press key icon servo motor moves back to original position to lock and wait for new OTP.

    • I have made this with Arduino uno and tested it on that Right now I don’t the circuit diagram for ATMEGA328P IC . Why don’t you try it with Arduino uno or Arduino pro mini, I think Arduino pro mini best fit if you are trying to reducing the size of circuit.Still u need some help ask me

  1. I powered the bluetooth module according to the circuit but it did not work. So we used the arduino to power the module and the module worked but the code won’t get uploaded on the board now. What changes should be made in code for this?
    Also to provide the motor and the BT module 5V, we used 7805 regulator and a 9V battery.

    • You can’t upload the code to Arduino when the RX and TX of Bluetooth is Connected to Arduino. First unplug the Bluetooth Power and the try to upload the code.When code get successfully uploaded to Arduino ,you can connect all the components as described.

  2. The app is not working properly .
    bluetooth got connected and the otp also came but the unlock key is not .
    i have checked thrice same problem is happening again and again.
    plz help me ASAP.

        • I have re-tested the whole project and it is working fine. Please check the following points:-

          1.Check your servo is it working using the sweep example
          2. check the Bluetooth connection are you getting OTP after tapping on key icon

          • sir can you share your contact number . please sir i am facing huge problems in this project…

          • what problem are you facing can you elaborate in details. If you have powered the servo with external battery and powered the Arduino with different battery then make a GND common between Servo and Arduino simply attach wire between GND of Servo to GND of Arduino. Be careful during powering.
            Please share the whole earrr in details what problem you are facing.

            You can also try another Lock based project Biometric Lock link.

    • yes, either you can add some extra reset function to it or you can upload the app to any Google drive so that you can download the app from any other phone to unlock. You can improve it and modify it to make it more reliable. You can also share your project story with us. We love to see what you have made?

    • To make the lock close and open you can use the servo shaft and move it to a different angle and position. For example, for locj=king you might need to move the servo shaft to 120 angle you can do this bu adding servo.write(120) in code.

  3. Where is OTP is used in this project?Means OTP send to our phone but what further?you didn’t used that OTP anywhere ,you just click on that key icon.So what is the use of that otp,can u plz explain.

    • Probably the app is made years ago and the new latest version of android SDK changed please recreate the app with kodular with the latest version of SDK using .aia file or code blocks example. Or you can use any Bluetooth terminal app from the play store to send the OTP and receive OTP .I still face problem the contact me on 7061350809

    • zEvery time you want to unlock you need new password to unlock the one time password received by app is top that you need to send back to lock. The apk is made to make it simpler so app itself receive the OTP and do all stuff instead of you . Ypu can use the Bluetooth terminal app to send the device id to get otp from lock and retype the otp to lock to open it.

    • zEvery time you want to unlock you need new password to unlock the one time password received by app is top that you need to send back to lock. The apk is made to make it simpler so app itself receive the OTP and do all stuff instead of you . Ypu can use the Bluetooth terminal app to send the device id to get otp from lock and retype the otp to lock to open it.

SHARE YOUR THOUGHTS & COMMENTS

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators