Bluetooth technology has become an essential aspect of modern communication, and it is widely used in various applications such as personal and industrial devices. In this blog post, we will be discussing the use of the HC-05 Bluetooth module to establish text communication between a PC and a mobile device.
The HC-05 Bluetooth module is a popular and low-cost module that can be used to establish wireless communication between devices. It can be easily interfaced with microcontrollers such as Arduino, and it supports various communication protocols such as serial communication, which makes it suitable for text communication.
To establish text communication between a PC and a mobile device, we will be using the serial communication protocol provided by the HC-05 Bluetooth module. This protocol allows the user to send and receive data using the serial communication ports of the devices.
We will be using an Arduino microcontroller as a bridge between the PC and the mobile device. The Arduino will be connected to the HC-05 Bluetooth module, and it will be programmed to establish a serial communication link between the PC and the mobile device.
To send and receive text messages, we will use a simple Serial Monitor and a terminal app on mobile device. Once the serial communication link is established, data can be transmitted between the devices using the serial communication ports.
In this blog post, we will be discussing the circuit diagram, working principle, and programming code of the system, as well as the advantages and disadvantages of using the HC-05 Bluetooth module for text communication. By the end of this blog post, you will have a good understanding of how to establish text communication between a PC and a mobile device using the HC-05 Bluetooth module.
It is important to note that, before starting to use the HC-05 module
you should check your country's regulations regarding the use of
Bluetooth modules, as there may be restrictions or requirements.
Here we will show how to interface HC-05 bluetooth module and ATmega328p and how to use the bluetooth module to turn on/off a LED connected to the ATmega328p microcontroller. To do this we will use a cell phone to communicate with the microcontroller. The cell phone and the bluetooth module will be paired via bluetooth. Then entering character y or n on the cell phone will to transferred to the bluetooth module wirelessly, then the bluetooth module will send the received character to the ATmega328p via USART communication and then ATmega328p will turn on or off the LED accordingly. This simple example can be extended to more complex projects. Once you have learned how to setup the interfacing between the microcontroller and bluetooth module and learned how to communicate with it, other control mechanism can be easily added.
The HC-05 bluetooth module is as shown below.
HC-05 Bluetooth Module has six pins-
- EN(Enable) pin is used put the module into either data mode or command mode. When low the module is in data mode. This is also the default mode. When high the module is put into command mode
- Vcc or +5V pin is the power supply pin
- GND pin is the ground pin
- TX pin is the transmit serial pin where the bluetooth module will transmit data. This pin is 3.3V logic level pin.
- RX pin the receive serial pin where the bluetooth module will receive data. Note that is 3.3V pin w
hich means the signal coming into this pin must be 3.3V logic level.
- State pin is pin which indicates the state of the bluetooth module like whether it is working properly or not. This pin is also connected to the board led so this is quite unnecessary to connect.
HC05 Bluetooth interfacing with ATmega328p schematic diagram
Below picture shows schematic diagram of interfacing the HC-05 bluetooth module with ATmega329p microcontroller.
In the schematic diagram, the ATmega328p USART Tx pin is connected to the Rx pin. Here a voltage divider is used to fed 3.3V logic level to the bluetooth module using the 1KOhm and 2.2KOhm resistors. The ATmega328p USART Rx pin is directly connected to the Tx pin since the ATmega328p is 5V logic and can detect the 3.3V logic signal coming from the HC05 bluetooth module. A LED with resistor is connected to the PORT B pin 0. When the Bluetooth module receives message from mobile handset or other device to turn on the LED or turn off the LED, the microcontroller will receive the same message from the bluetooth module and turn on/off the LED.
On a breadboard the connection looks like this.
Programming
The C program code for the bluetooth communication with HC05 module is below.
/*https://ee-diary.com*
/#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#define UBRRVAL 51
void usart_init(void);
void sendbyte(unsigned char);
void sendstr(unsigned char *);
unsigned char receivebyte(void);
void receivestr(unsigned char*);
unsigned char onmsg[] = "ON\n";
unsigned char offmsg[] = "OFF\n";
unsigned char defaultmsg[] = "LED Status:\n";
unsigned char rxdata;
int main(){
DDRB |= (1<<PB0);
usart_init();
while(1)
{
rxdata = receivebyte();
if(rxdata == 'y')
{
PORTB |= (1<<PB0);
sendstr(defaultmsg);
sendstr(onmsg);
}
else if(rxdata =='n')
{
PORTB &= ~(1<<PB0);
sendstr(defaultmsg);
sendstr(offmsg);
}
else{
}
}
return 0;
}
void usart_init(void){
UBRR0H= (unsigned char)(UBRRVAL>>8); //high byte
UBRR0L=(unsigned char)UBRRVAL; //low byte
UCSR0B |= (1<<TXEN0) | (1<<RXEN0); //Enable Transmitter and Receiver
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00); //Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
}
void sendbyte(unsigned char MSG){
while((UCSR0A&(1<<UDRE0)) == 0); // Wait if a byte is being transmitted
UDR0 = MSG;
}
void sendstr(unsigned char *s){
unsigned char i = 0;
while(s[i] != '\0'){
sendbyte(s[i]);
i++;
}
}
unsigned char receivebyte(void){
while(!(UCSR0A & (1<<RXC0)));
return UDR0;
}
The HC05 bluetooth module operates at 9600 baud rate with 8 bit data and 1 stop bit. So accordingly we have set up the ATmega328p USART for the same serial communication settings. The ubrr value of 51 used in the program setup up the baud rate for 9600 using 8MHz CPU frequency, see datasheet for this.
The codes used here are explained in our earlier tutorial Programming ATmega328p USART. It explains basic of ATmega328p USART programming by showing example of how to use a PC terminal to send command to the microcontroller usart to turn on/off a led. Also see Communication between Microcontrollers using USART-ATmega328P and ATmega32A tutorial which explains how to use the Atmega328p USART to communicate with another micrcontroller ATmega32A.
After uploading code into ATmega328p the next step is to install bluetooth terminal on the mobile phone. Search for google play store for bluetooth terminal and install it. There are many free bluetooth terminal application anyone will work. After installation, you should search for the HC-05 device in your mobile phone and pair with it. Once the terminal is open, send character y to turn on the LED and send character n to turn off the LED.
The following video demonstrates this.
Hi, thanks for this post.
Did you try to do the same thing with an ATMega328P programmed with an Arduino UNO programmator?
I don't succeed to simply transmit characters to/from a HC05.
hi,
from first look, maybe you should check the frequency as in the above code the frequency is set to 8MHz, so for baud rate of 9600 symbols/sec the UBRR value is calculated to be 51. Arduino uses frequency of 16MHz. The UBRR value is calculated using the cpu frequency and the required baud rate(see the formula in datasheet).