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.
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
Figure 2: Wire diagram of HC-05 and Arduino Nano
Figure 3: Connection between Bluetooth and Arduino
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.
Serial.println("What did you say?:");
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.
With RX and TX connected you will receive lots of errors like shown below:
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.
Figure 4: Electrical error that occurs when the pin TX of Bluetooth is connected
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):
Click on Devices
Turn it ON
Find “HC-05” and click “PAIR”
Type in the password “1234”
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:
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.
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
For our demonstration, we will type in the message “Hello!”:
The output should show the same message:
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:
Typing in the passcode from keypad
Placing the tag near RFID
Typing in the passcode from your smartphone
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.
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.
Figure 10: DC-DC step-down module (MP1584EN)
Figure 11: Measuring the batteries
Figure 12: Wire diagram of the final setup
Figure 13: The final setup
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
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:
Download the application
Log In with your Google account
Start a new Project
After you install the program, you will find a graphic interface like the following:
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.
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
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.
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:
Figure 18: User interface
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 :
Bluetooth Client1 – component that detects bluetooth devices. A function of the system that returns the following:
The addresses and names of paired Bluetooth devices – AddressesAndNames
Whether Bluetooth is available on the device – Available
Whether Bluetooth is enabled – Enabled
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.
Notifier1 – The Notifier component displays alert dialogs, messages, and temporary alerts, and creates Android log entries through the following methods:
ShowMessageDialog: displays a message which the user must dismiss by pressing a button.
LogError: logs an error message to the Android log.
LogInfo: logs an info message to the Android log.
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.
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.
Figure 23: Searching for bluetooth
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.
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”
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.”
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.
Tiberia is currently in her final year of electrical engineering at Politehnica University of Bucharest. She is very passionate about designing and developing Smart Home devices that make our everyday lives easier.