Text Communication between PC and Mobile using HC-05 Bluetooth Module

 Here is illustration of how to send text string message from mobile to PC using HC-05 Bluetooth module and Arduino. The following picture illustrates this communication process.

text communication with HC-05 bluetooth module

HC-05 Bluetooth module

The HC-05 Bluetooth module has 6 pins which are the State, Rx, Tx, GND, +5V and EN pins as shown below.

 


The State pin is connected to a LED on the module board that indicate state of the module. This pin can used to read the state of the Bluetooth module by connecting it to a micrcontroller or Arduino. On the board is also a LED that indicates various state of the module. 

The Rx and Tx pins are used to connect to a microcontroller like Arduino. The GND and +5V pins for power connection. The enable pin, EN, is used to put the module into command mode. If it is left unconnected then it is in normal data mode. If it is pulled high then the module enters into the command mode where AT commands can be used to change the setting of the HC-05 module.

Interfacing Arduino and HC-05 Bluetooth module

 Below is the interfacing circuit diagram between Arduino and HC-05 bluetooth module.

Interfacing Arduino and HC-05 bluetooth module

The Arduino Tx(Pin 1) is connected to HC-05 Rx pin, the Arduino Rx(Pin 0) is connected to HC-05 Tx pin. The Arduino +5V pin is connected to HC-05 +5V pin and Arduino ground pin is connected to HC-05 GND pin. The state and EN pin of HC-05 are left unconnected.

Arduino Code

The following is the Arduino code which checks incoming data received by HC-05 and displays the text on the serial monitor.


 #include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1);

//receive buffer 
String msg = "";

void setup(){
Serial.begin(9600);
mySerial.begin(9600);
}

void loop(){
	while (mySerial.available() > 0){
		char character = mySerial.read(); // Receive single character at serial port coming from HC-05
		msg.concat(character); // Add each received character to the receive buffer
		if (character == '\n'){
			Serial.print("Received Text: ");
			Serial.println(msg);
			msg = "";	// Clear receive buffer so we're ready to receive the next			
		}
	}
}
 

In the above code we have used the SoftwareSerial.h library to use the serial port(pin 1 and pin 0) on Arduino. To use the library we have to create a SoftwareSerial object which is mySerial(0,1) here. We create a empty string called msg which will be used as buffer to store the received text. In the setup() we have initialized the serial ports. Then in the loop() we read in the serial data if there is any and store each received character in variable called character. Then we use the concat() method to add each received character to the msg string variable. If the received character is newline \n then the whole text stored in the msg string is displayed after the Received Text:. Then finally we clear the content of the msg buffer and repeat the cycle.

See other HC-05 Bluetooth tutorials below.

- ATmega328p, Bluetooth and IR sensor Interfacing and coding

- Bluetooth Controlled LED using Arduino

Post a Comment

Previous Post Next Post