Secure Lock With Arduino

Introduction: Secure Lock With Arduino

Our project goal is the implementationo of an electronic locking system which Arduino.


Basic knowledge with using Arudino and the IDE - Arduino Getting StartedAndroid Devices with BLE support and running Android Version 4.3 or later, such as: Nexus 4, Nexus 5 and Nexus 7

(please visit the Android Support page for list of compatible Android devices)Arduino or Arduino compatible board, with operating voltage at 3.3V or 5V, such as:

Uno, Mega and LeonardoA host machine with Arduino IDE installed. Able to upload basic sample sketches to Arduino Board, such as:

Mac, Windows or Linux PC

- Hardware Checklist

o Arduino UNO $29.95

o BLE Shield $29.99

o USB Cable $3.95

o Led $1.00

- Software Checklist

- · Arduino IDE version 1.0.5

- · Nordic Bluetooth low energy SDK for Arduino beta version 0.9.0 (download library / source code)

- · RedBearLab nRF8001 Library version 20140509 (download library / source code)

- Android BLE Controller App

For the installation in this step you can access to the next link:

Step 1: BLE Shield Controller

The first step goal is the libraries Setup and Update for the BLE Shield Controller and Download BLE Controller from Google Play Store

You can acces to the next link for more details:

Step 2: Add Security at Your Application Using the TOTP-Arduino Library

The Arduino TOTP (Time-based One Time Password) library implements the algorithm described in the RFC 6238 to generate time-based OTP codes.

A new code is generated every 30 seconds, from a password (better, a shared secret) and the actual timestamp value (= the number of seconds from the date 01/01/1970, called Epoch).

These OTP codes are commonly used to access web sites, home-banking applications… and are generated with tokens, that could be hardware or software ones

Image 1

The library can be easily
installed using the Arduino’s Library Manager dell’IDE (menu Sketch – Include Library – Manage Libraries…):

Image 2

Alternatively you can download the latest release from Github and install it manually.


First, define the password and create the TOTP object passing to its constructor the pointer to the password and the password’s length:

uint8_t hmacKey[] = {0x4d, 0x79, 0x4c, 0x65, 0x67, 0x6f, 0x44, 0x6f, 0x6f, 0x72};TOTP totp = TOTP(hmacKey, 10);

To convert a password in a byte array you can use the following webpage:

that also displays the password in base32 encoding and as a QRCode to configure the Google Authenticator app:

Image 3

To get a new code, call the
getCode() method passing the actual timestamp:

char* newCode = totp.getCode(timeStamp);

The timestamp value can be obtained from a RTC (real-time clock) IC, like the DS1307 from Maxim.

For more deails in this library you can acces to the next link:

Step 3: Changes in the RedBear Protocol

After the installation of Android Studio you must insert one BLE Controller inside your application project. This BLE Controller project have the necesary clases, services and protocols that you need for the communication with your arduino through the RedBear Shiel BlueTooth device.

Later you must make some changes to the RBLProtocol for the communication. The first change in the protocol is create a method for send the password to arduino.

public void SendPassword(char[] data)<br>{
 	String b = new String(data); 
	String str = "Y" + b ;
   	char buf[] = str.toCharArray();

Every time that you send a message with an initial character through the protocol you need to receive another message with the same character to avoid the lost of the communication. By this reason you need make some changes in the protocol for receive the response from Arduino. This changes are in the "parseData" method when all the data from Arduino are decoded. Here we can add the next sentece inside the switch:

		mIrblProtocol.protocolDidReceiveProtocolVersion(data[i++],data[i++], data[i++]); 
	}    break;

Step 4: Override the IRedBearServiceEventListener Interface

For the instance of this interface we need make an overrida of the methods "onDeviceFound" and the method


When found a new device you need to add this and create a new instance. Compare the device address with the address that you search and if are equal then you can connect to the device.

public void onDeviceFound(String deviceAddress, String name, int rssi, int bondState, byte[] scanRecord, ParcelUuid[] uuids) {
    Device mDevice = new Device();
    mDevice.address = deviceAddress; = name;
    mDevice.rssi = rssi;
    mDevice.bondState = bondState;
    mDevice.scanReadData = scanRecord;
    mDevice.uuids = uuids;
		mBearService.connectDevice(lockAddress, false);//Connect to the lock 
		conected = true; 

Now we need make an override over the method "onDeviceCharacterirsticFound" and create a new instance of the protocol when you are connected to the device:

public void onDeviceCharacteristicFound() {
	mProtocol = new RBLProtocol(lockAddress);

After the creation of one instance for the protocol you must acces to the pins of the arduino and set the mode how to send information. For this querys you can use the next sentence:

<strong>mProtocol.digitalWrite(7, isChecked ? 1:0); </strong>

The first params is the pin and the second is the value data that you send to his.

For unlok in a secure way you need send a password. For this we can use the method created in the last step.


Step 5: Arduino Code Exampe in Codebender

The arduino project be aviable in codebender at the next link:

Be the First to Share


    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    2 Discussions


    3 years ago

    Hey watch my youtube tutorial video on OTP door lock

    DIY Hacks and How Tos

    Cool idea. Do you have any more pictures of how it is set up.