Make Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your Smartphone

Make Your Own Arduino RFID Door Lock – Part 2: Unlock Using Your Smartphone

arduino rfid door lock

Click here to read Part 1 of this article >

arduino rfid door lock

 

In the previous article Make Your Own Arduino RFID Door Lock, we made an Arduino RFID door lock. We were able to unlock a door using a RFID tag or a keypad. Well, what if you want to simplify the process even more? In this second part, we will add an extra feature: unlocking the door using your smartphone. It is an easier way to control the door, especially when everyone prefers having a single device that can give you access to everything. So how can we do this? We’ll connect a bluetooth module to the previous set-up and also connect the electrical door.

Hardware

  • HC-05 Bluetooth Module
  • ABK-704L Electrical
  • Arduino Nano
  • RFID RC522
  • Piezo Buzzer
  • 2x 330 Resistor
  • Keypad 4×4
  • Adapter I2C for LCD
  • LCD16X2BL
  • DC-DC Step-Down Module MP1584EN

Software

  • MIT App Inventor 2
  • Arduino IDE
  • Github

Step 1:Connecting the bluetooth

In this step we will take close look at the HC-05 Bluetooth module.

Arduino-info Wiki

There are two modes of operation:

  • Command Mode – sends AT commands to the bluetooth module;
  • Data Mode –  receives and transmits data from and to another bluetooth module. The default mode is DATA Mode and the default configuration is as follows:
    • Baud Rate: 9600 bps, Data : 8 bits, Stop Bits: 1 bit, Parity : None
    • Passkey: 1234
    • Device Name: HC-05

For our application, we will use the DATA Mode because we only need to use the serial communication to receive information from our mobile phone.

For more information about serial communication, please refer back to Arduino Serial Communication Tutorial.

arduino rfid door lock

Figure 1: The pins of the bluetooth module

In this application we need to use the following pins-Arduino-info Wiki:

  • VCC: +5 Power
  • GND: System / Arduino Ground
  • TX: Transmit Serial Data from HC-05 to Arduino Serial Receive
  • RX: Receive Serial Data from Arduino Serial Transmit
arduino rfid door lock

Figure 2: Wire diagram of HC-05 and Arduino Nano

 

arduino rfid door lock

Figure 3: Connection between Bluetooth and Arduino

The code

The bluetooth uses the serial communication; “Serial.write(Serial.read());” command will work without any problem with the RX and TX pins connected. In case of the bluetooth module where this rule doesn’t apply, the code below will return the same texts that you write in Arduino IDE.

You need to find the Arduino programming port in Device Manager; after you connect the bluetooth the “Standard serial over Bluetooth link” will appear and you must select “USB-SERIAL” in order to program the Arduino. You need to use a serial cable to upload your program. You cannot use bluetooth wireless to upload programs to the board.

arduino rfid door lock

With RX and TX connected you will receive lots of errors like shown below:

arduino rfid door lock

In order to avoid these errors, you need to upload the program WITHOUT the Bluetooth TX pin connected to the Arduino Board. The TX pin of the Bluetooth has a LOW impedance and the RX input of the Arduino has a HIGH impedance. The resulting impedance will be a LOW one and the RX input will be bypassed (the current generated from the USB port is directed through the Bluetooth’s output, instead of the Arduino Board). This is the reason why the data will not be transmitted to its desired destination. In addition, pushing a current into an output of a device will create a wrong connection between the modules, and this will lead to an electrical error. What we’re trying to do is to program the Arduino, not to send data to the bluetooth module.

After you disconnect the TX pin of the Bluetooth, everything will work fine and you can upload the code.

arduino rfid door lock

Figure 4: Electrical error that occurs when the pin TX of Bluetooth is connected

 

arduino rfid door lock

Figure 5: Wire diagram of HC-05 and Arduino Nano showing correct connection before uploading the code

 

After the code is uploaded, the next step will be to connect to the bluetooth on your computer. You need to reconnect the TX pin of the Bluetooth because now we will use wireless communication and the serial cable is only used for power.

Follow the following steps to connect the bluetooth to PC (on Windows 10):

  1. Open Settings
  2. Click on Devices
  3. Select Bluetooth
  4. Turn it ON
  5. Find “HC-05” and click “PAIR”
  6. Type in the password “1234”
arduino rfid door lock

Figure 6: Connecting the Bluetooth module on Windows 10

If you’ve followed the steps above, your bluetooth connection should work and you can test your module. Here is a simple way to check if the connection has been successfully established: Type a word in Arduino IDE and see if it returns the same word. If it does, then you’re good to go! If not, then go back and try to re-pair the bluetooth.

Because we are no longer using the serial communication through cable, we need to find the COM of the Bluetooth. We need to go back to Device Manager at the PORTS section and search for the bluetooth device. In my case it is PORT 17:

arduino rfid door lock

After you find the COM in Device Manager, you need to do the same (i.e. set COM port to COM 17) in Arduino IDE. You need to click on TOOLS → PORT → COM 17.

arduino rfid door lock

Figure 7: Setting COM in Arduino IDE

After you select the right COM, you can test your module to see if it works correctly.

Make sure you have the following two selected in the Serial Monitor:

  • Both NL & CR
  • 9600 baud

For our demonstration, we will type in the message “Hello!”:

arduino rfid door lock

The output should show the same message:

arduino rfid door lock

Step 2: The device

Let’s refer back to the previous article Make Your Own Arduino RFID Door Lock! We’ll be using the same components as before and adding one more which is the bluetooth module (HC-05). In order to transfer data from your smartphone to the Arduino we need the bluetooth module.

It is important to disconnect the TX pin of the Bluetooth because otherwise we’ll get the same errors like in the first step.

There are 3 ways to unlock the door:

  1. Typing in the passcode from keypad
  2. Placing the tag near RFID
  3. Typing in the passcode from your smartphone
arduino rfid door lock

Figure 8: Wire diagram of all the components

The lock has a very high current consumption (800mA). Let’s add a green LED to see if the code works. The LED will be HIGH when the “*123456#” code is typed in and the system is unlocked.

arduino rfid door lock

Figure 9: Connections between the components

In the next step, we’ll add a battery and a relay. We’ll be using two 9V batteries to power the door. If you decide to mount the device to a fixed place you need to buy a device that gives you 5V from the 220V from the switching power supply.

Because the current consumption of the lock is high, the batteries are in parallel connections. In this case, the Arduino needs to be powered from the batteries. We need to use a DC-DC Step-Down Module that converts 9V to 5V. The input voltage enters in the Vin pin of the Arduino Board. We will no longer use the supply from the USB from the computer.

The step-down module needs to be fixed on the output voltage of 5V. You need to have a multimeter for calibration; the input voltage is 9V and we will spin the potentiometer until the output voltage is 5V.  

arduino rfid door lock

Figure 10: DC-DC step-down module (MP1584EN)

 

arduino rfid door lock

Figure 11: Measuring the batteries

 

arduino rfid door lock

Figure 12: Wire diagram of the final setup

 

arduino rfid door lock

Figure 13: The final setup

 

Full code

 

Step 3: The application

In this step, we will create the application for our system. I used the program MIT App Inventor 2. This is a simple program which doesn’t require a high level of programming skills. It can be downloaded from here: MIT AppInventor. In order to open this application you need to:

  1. Download the application
  2. Log In with your Google account
  3. Start a new Project

After you install the program, you will find a graphic interface like the following:

arduino rfid door lock

Figure 14 : MIT AppInventor

 

In order to test your application, you need to download “MIT AI2” app on your phone from Google Play. This is a free application made specifically for syncing the web apps with your smartphone.

 

arduino rfid door lock

Figure 15: MIT AppInventor on Google Play

 

If you want to see the screen of the app on your phone, you need to click on the Connect → AI Companion

arduino rfid door lock

Figure 16 : Connecting the app

After you click on it, the app will give you two ways of operation:

  • You can scan the barcode (Launch MIT AI2 Companion on your device and then scan the barcode or type in the code to connect for live testing of your app.)
  • Type in the 6 letter code on your app

I’ll use the second method and type in the 6 letter code generated by the computer into the phone app.

arduino rfid door lock

Figure 17: Syncing both web and phone

 

I chose this application because it is easy to use; you don’t need to have programming skills to implement a simple code like this one. You just need to sort of imagine how the application will look like and it gets very simple from there.

Let’s begin with the user interface:

arduino rfid door lock

Figure 18: User interface

 

 

arduino rfid door lock

Figure 19: My app for unlocking door wirelessly

 

You can add all elements of your app on the screen (buttons, images, text box, label). On the right menu you can select what characteristics you want from the screen (Align horizontal, App name, Title). If you want to have a background image you can select it from “Background Image” menu. In this app I chose the green background if the connection between bluetooth and the phone is on, and the red background if it is not.

3 elements for this app are indicated on the screen :

arduino rfid door lock

Figure 20: Non-visible components

Sensor Components & User Interface Components – MIT App Inventor

  1. Bluetooth Client1 – component that detects bluetooth devices. A function of the system that returns the following:
    1. The addresses and names of paired Bluetooth devices – AddressesAndNames
    2. Whether Bluetooth is available on the device – Available
    3. Whether Bluetooth is enabled – Enabled
  2. Clock1 – Non-visible component that provides the instant in time using the internal clock on the phone. It can fire a timer at regularly set intervals and perform time calculations, manipulations, and conversions.
  3. Notifier1The Notifier component displays alert dialogs, messages, and temporary alerts, and creates Android log entries through the following methods:
    1. ShowMessageDialog: displays a message which the user must dismiss by pressing a button.
    2. LogError: logs an error message to the Android log.
    3. LogInfo: logs an info message to the Android log.
arduino rfid door locks

Figure 21: Blocks for screen

 

You will receive an error if the bluetooth is not connected, and this will appear in the “Link Status” section. I typed in the passkey for opening the door without connecting a device and the app notified me that the device is not turned on. This is a very good notifier because you can forget to pair your device and you can’t open the door.

 

arduino rfid door lock

Figure 22: The notifier showing error

The app has 3 buttons:

 

  • 1. Connect: A button that, when clicked on, displays a list of texts for the user to choose from. This button will act as a bluetooth finder. If you activate more than one bluetooth near the phone with the app, these will be shown in the search box. I have only one device that has bluetooth turned on, the HC-05 module.

 

arduino rfid door lock

Figure 23: Searching for bluetooth

 

arduino rfid door lock

Figure 24: Blocks for listpicker

 

  • 2. Disconnect : This button will disable the connection between the app and the phone. After you click on the button the background color will be red.

 

arduino rfid door lock

Figure 25: Blocks for disconnect

 

  • 3. Send text : This command will work only if the connection is on. To send text data, the program uses the “SendText” function of the Bluetooth. In the status part will be displayed the message ”Message Sent”

 

arduino rfid door lock

Figure 26: Blocks for sending texts

 

How the app works –  Pevest App Inventor 2: Learn to Code

“The app checks to see if Bluetooth is on when enters in the Clock1.Timer instruction. The variables ByteAvailable and CommandByte will be initialized with value 0, making them ready for transmission. If data is available, the first byte of the incoming data is read using ReceiveSigned1ByteNumber. This is the command byte. Depending on the value of the command the appropriate Bluetooth method is used to read the next bytes.”

arduino rfid door lock

Figure 27: Blocks for transmission / © Pevest App Inventor 2

 

Conclusion

This project was a challenging one because of the multitude of communications between peripherals, which took me some time to find the perfect way of wiring all the components. It helped me understand some of the errors that can occur during the process of making a complex device and how to avoid them.