How to Handle HC-05 Bluetooth Module with Arduino

One of bluetooth module which is able to handle bluetooth communication is HC-05. This module is widely used because it is relatively easy to find on the market and the price is fairly cheap. By using this module, you can deal with all Bluetooth-enable devices. But, remember, the HC-05 module only supports Bluetooth serial protocol (SPP = Serial Profile Protocol). It can’t support other services such as Bluetooth-audio, file transfer, OBEX or recently technology, BLE (Bluetooth Low Energy). The last one is a new technology in bluetooth which need low power so it’s widely used in portable devices such as smartphone, EDC or others.

The HC-05 module has 6 pinouts, but actually you only need 4 of them to interfacing with Arduino. If you wish to connect it into Arduino, you can refer to picture below



Arduino – HC-05 Connection Pinout Configuration

The HC-05 is powered by 5V DC voltage, so you must connect it to pin Vcc Arduino. It drains current 25-40 mA in peak usage (active communication) while just drains 8 mA approximately in standby condition. So, please be aware if your circuit uses a lot of active component, you must recalculate your power source.

Arduino
HC-05 module
8
TxD
9
RxD
Vcc
Vcc
Gnd
Gnd

Notice there are at least 4 pinout is used for interfacing with an Arduino Bluetooth module HC-05, ie, Vcc, Gnd, TxD and RxD. In principle, this module communicates using serial protocols at TTL level that can be directly with Arduino (not RS-232 voltage level). In sketch example I gave, I use digital pins 8 and 9 are emulated as a serial port using SoftSerial.h built-in library. Why not use serial port hardware on pins 0 and 1? Actually you can also use these pins, but you will not be able to monitor serial data stream over your computer (using Serial Monitor tools) when Arduino serial port is already used to communicate with a Bluetooth module. So if you use pins 8 and 9 as a serial port, you can debug serial data exchange through Serial Monitor window (Tools-Serial Monitor menu in Arduino IDE).

Arduino - HC-05 Sketch Handler

In order to refresh, I write back sketch handler for HC-05 Bluetooth module. Here they are:

#include <SoftwareSerial.h> // import softwareserial library

SoftwareSerial BlueSer (8, 9); // pin 8 as RX, pin 9 as TX
int led = 13;
int BluetoothData;

void setup () {
  BlueSer.begin (9600);
  BlueSer.println ( "Bluetooth ready, press "1" or "0" or on / off LED");
  pinMode (LED, OUTPUT);
  Serial.begin (9600);
}

void loop () {
if (BlueSer.available ()) {
BluetoothData = BlueSer.read ();
Serial.println (BluetoothData);
   if (BluetoothData == '1') {
   digitalWrite (led, HIGH);
   BlueSer.println ( "LED ON");
   }
  if (BluetoothData == '0') {
  digitalWrite (led, LOW);
   BlueSer.println ( "LED OFF");
  }
}
delay (100);
}

Explanation about sketch above:
Line [1]: declare SoftwareSerial.h library that is used to emulate a regular digital pin into a serial pin
Row [2]: defines digital pin to be emulated into serial pin
Row [3]: defines variables to be used in sketch
Line [4]: ​​initializing pin 8 and 9 in serial mode with 9600 bps baudrate
Row [5]: write 'greeting' when communication has been established with the Bluetooth device paired
Line [6]: initializing pin 13 with a mode pin OUTPUT
Line [7]: initializes the default serial port (pin 0 and 1) with 9600 bps baudrate. This serial port will be used to debug/monitor data exchange via Serial Monitor window.
Line [8]: if there is no data on pins 8/9 will be copied to 'BluetoothData' variable
Line [9]: featuring 'BluetoothData' variable for the purposes of debug / monitor data exchange through Serial Monitor window
Line [10]: turn on or turn off interna LED on pin 13 if any data are entered in 'BluetoothData' variable
Line [11]: delay time of 100ms before the sketch back to first line of void loop

Finish. But please remember, firstly, you must pair your Bluetooth HC-05 module with other Bluetooth-enable device before they can establish data exchange. The HC-05 module will be known as ‘HS-05’ in your Bluetooth device (ex: smartphone) while searching. For pairing example using Android smartphone, I will explain in next tutorial. I hope my tutorial give some benefits for you, thanks for reading, if any questions please comment below
Previous
Next Post »