Interfacing Bluetooth HC-05 module with Raspberry Pi
Updated: Aug 21, 2020
Have you ever wondered how data is transferred between the devices without any physical contact? You would have shared many songs among your friends without the use of the internet. It's because of this module called #Bluetooth. While proceeding you will know about how the Bluetooth works? Which signal is carrying the data? The transferred data can be viewed using #raspberrypi as an output terminal.
The interfacing is pretty simple and can be used as a reference for various projects in the future. This article briefly explains the process involved in #interfacing. Starting from the list of required components.
The following list provides the hardware and software components required to perform this interfacing.
1.Bluetooth Module [HC-05]:
This module follows the IEEE 802.15.1 Standardized protocol. It helps us to construct Wireless Personal Area Network (PAN) and for the transmission of data, the device employs frequency hopping spread spectrum (FHSS). To interact with other devices, it uses the mode of serial communication. The module consists of 6 pins and it can be operated either in master or slave configuration.
KEY/EN pin: This pin ensures that the module is in AT command mode. It consists of data mode and command mode.
DATA MODE: Transmission and reception of data
COMMAND MODE: Execute the commands that help in changing the configurations of HC-05 and transmit those commands to the serial port is used.
· VCC pin: Power supply 3.3V. We can also supply 5V since the device has 5V to 3.3V regulator.
· GND pin: Ground pin of the module.
· TXD pin: Transmits the serial data.
· RXD pin: Receives the serial data.
· STATE pin: It indicates whether the module is paired or not.
2.Raspberry Pi 3:
We are using #Raspberrypi to connect with the Bluetooth HC-05 module and display the output. It contains several I/O pins using which we can connect the module and then by coding the Raspberrypi, we will receive the commands/signals that are sent from the mobile.
This cable is used to connect the Raspberrypi to the power supply. It also helps in the transmission of data.
These are simple connecting wires, that are used to connect the ends without any soldering. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end. In this interfacing, female to female wires is used.
The SHH Terminal is used in this project to connect the Raspberry pi module to the terminal of another computer.
#Python Programming is done in an IDE (Integrated Development Environment) is a platform where we can write the code, run, and test the working.
In this section, let's see the connections made to the interface Bluetooth module to the Raspberry pi device.
What goes where?
The RED wire connects the VCC of the Bluetooth module to the 5V (VCC) of the raspberry pi.
· BLACK wire connects the ground pin of #HC-05 to the ground pin of the raspberry pi.
·BLUE 1 wire connects the transmitting pin [TX] of HC-05 to the receiving pin [RX] of the raspberry pi device.
· BLUE 2 wire connects the receiving pin [RX] of HC-05 to the transmitting pin[TX] of the raspberry pi.
After establishing the connections, the power supply is given to the devices and raspberry pi gets boot up.
Note: To setup HC-05 on your system and interface it with Raspberry Pi, follow the instructions mentioned in the file below.
Baud rate is nothing but the speed or rate at which data is being transmitted between devices. The default baud rate of the raspberry pi serial port is 115200 bps but we need to reduce it to 9600 bps in order to interface it with #HC05. File /boot/cmdline.txt contains two options, they are console and kgdboc. Now they are reassigned as,
To edit the file we have to use the command called SUDO. VI and PICO are the two text editor commands. So finally, we have to run the following command for editing the file.
sudo pico /boot/cmdline.txt
These are the steps involved in interfacing HC-05 with raspberry pi.
First, import the serial library from python libraries which are employed mainly for performing serial communication. Then create a variable uart_channel for configuring baud rate and timeout. The variables data1 and data are initialized. The while loop is employed as it must return again after displaying one character until it covers all the data. The command inside the while loop reads the data given by us and assign it to the data1 variable and then prints it.
import serial uart_channel = serial.Serial("/day/ttyAMAO", baudrate=9600, timeout=2) data1=" " data=" " while 1; data = uart_channel.read(1) data1+=data print data1 uart_channel.flush() data=" " data1=" "
Download the code here,
Note: This file is in the text format and should be converted to python-format (.py) before downloading.
After the device gets a boot-up, We can install an app named BLUETOOTH TERMINAL HC-05 or BLUETERM so that, we can transfer the data from our mobile on to the Bluetooth module HC-05 and we can view the output on the raspberry pi terminal. The default PIN number of the Bluetooth module is 1234. Once the connection has been established, the LED of HC-05 will not stop blinking and remain lit. The data which is entered in the mobile application gets transferred to the end terminal with the help of an interfaced Bluetooth module.
The given input data now gets displayed on the raspberry pi terminal.
The Practical implementation of this interfacing is presented here as a video.
Video credits: Sai Parimal
This interfacing can also be performed with Arduino or other development boards but raspberry pi has more advantages when compared to others. It has wireless LAN and in-built Bluetooth facility by which we can achieve wireless connectivity and this plays an important role in the field of the Internet of things.