All about HC-05 Bluetooth Module | Connection with Android
Ever wanted to control your Mechanical Bots with an Android Phone or design the robots with custom remote, here in this tutorial we will learn about a Bluetooth Module HC-05 used for the above mentioned and many other cases. Here we will be understanding the connection and working of a HC-05 module and also its interfacing with custom android app.
Wireless communication is swiftly replacing the wired connection when it comes to electronics and communication. Designed to replace cable connections HC-05 uses serial communication to communicate with the electronics. Usually, it is used to connect small devices like mobile phones using a short-range wireless connection to exchange files. It uses the 2.45GHz frequency band. The transfer rate of the data can vary up to 1Mbps and is in range of 10 meters.
The HC-05 module can be operated within 4-6V of power supply. It supports baud rate of 9600, 19200, 38400, 57600, etc. Most importantly it can be operated in Master-Slave mode which means it will neither send or receive data from external sources.
Description of pins
Enable - This pin is used to set the Data Mode or and AT command mode (set high). VCC - This is connected to +5V power supply. Ground - Connected to ground of powering system. Tx (Transmitter) - This pin transmits the received data Serially. Rx (Receiver) - Used for broadcasting data serially over bluetooth. State -Used to check if the bluetooth is working properly.
Modes of Operation
The HC-05 Bluetooth Module can be used in two modes of operation: Command Mode and Data Mode.
In Command Mode, you can communicate with the Bluetooth module through AT Commands for configuring various settings and parameters of the Module like get the firmware information, changing Baud Rate, changing module name, it can be used to set it as master or slave.
A point about HC-05 Module is that it can be configured as Master or Slave in a communication pair. In order to select either of the modes, you need to activate the Command Mode and sent appropriate AT Commands.
Coming to the Data Mode, in this mode, the module is used for communicating with other Bluetooth device i.e. data transfer happens in this mode.
Programming HC-05 with Microcontroller
Technical specs of the code:
- Arduino-Uno is used as the microcontroller.
- Name: HC-05
- Password: 1234 (or 0000)
- Type: Slave
- Mode: Data
- Baud Rate: 9600 with 8 data bits, no parity and 1 stop bit
Android App interfacing with HC-05
Now, we will develop a small android application to demonstrate the connection of bluetooth module and the android app. We will be using Android Studio for this purpose and above mentioned C code on the microcontroller.
Algorithm: Create an empty project on Android Studio Create a ListView containing all the available bluetooth devices. Get the name and MAC-address of HC-05 module. Open connection with HC-05 module. Instruct the module with data as bytes.
Understanding the code
1.Getting all the Bluetooth devices in ListView
This code is for the MainActivity or first activity where the list will be displayed then after that when device will be selected, Control activity will come to give the command.
2.Getting the name and MAC address of device
Now we will create a OnClick Listener for the list so that thename and MAC address can be extracted from the device.
3.Establishing a connection between both of them
The connect() function in the Control.java will help to make the connection between both.
4.Finally commanding the HC-05 module
Here first we will check if our socket is connected, then only we will proceed to avoid the Null Pointer exception.
Now finally as we have done our first basic project with the HC-05 module and the Android programming, we can move on to complex electronic bots and make there wired connection to wireless using this amazing module HC-05 and also we now know to make a custom app.
You can find the above code in form of Android Studio Project committed here.