Toggle Nav

Getting Started with HC05 Bluetooth Module & Arduino [Tutorial]

Table of Contents

Introduction

In this tutorial you will learn how to communicate and send data with Bluetooth using Bluetooth HC05 Module and arduino board.

What You Will Learn

  • General information about Bluetooth protocol
  • How to send data using Bluetooth
  • How to send AT-Command to HC05

A Brief Introduction to Bluetooth Communication and Protocol

There are several ways  for wireless communication such as NRF, ZigBee, Wi-Fi and Bluetooth. 

Bluetooth protocol; an affordable communication method in PAN network, with maximum data rate of 1Mb/S, working in nominal range of 100 meters using 2.4 G frequency is a common way of wireless communicating.

HC05 module is a Bluetooth module using serial communication, mostly used in electronics projects.

You can see the HC05 module specifications in the table below:

Sending Data from Bluetooth to Arduino

HC05 module has an internal 3.3v regulator and that is why you can connect it to 5v voltage. But we strongly recommend to use a 3.3V voltage, since the logic of HC05 serial communication pins is 3.3V. Supplying 5V to the module can cause damage to the module. In order to prevent module from damages and make it work properly, you should use a resistance division circuit (5v to 3.3v ) between arduino TX pin and module RX pin. When master and slave are connected, blue and red LEDs on the board blink every 2seconds.If they aren’t connected, only blue one blinks every 2 seconds.

Circuit

Code

In order to communicate with HC05 using Bluetooth, you need a Bluetooth terminal application on your phone.  You can use this one:

link

Now for start transferring data, upload this code on your arduino and connect HC05 using the app you have just installed. Communication name is HC05, the password is 1234 or 0000 and the transfer boud rate is 9600 by default.

HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learnfarsi/ 
*/ 
#include <SoftwareSerial.h>
SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int LED = 8; 
void setup() 
{   
Serial.begin(9600); 
MyBlue.begin(9600); 
pinMode(LED, OUTPUT); 
Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 
void loop() 
{ 
if (MyBlue.available()) 
flag = MyBlue.read(); 
if (flag == 1) 
{ 
digitalWrite(LED, HIGH); 
Serial.println("LED On"); 
} 
else if (flag == 0) 
{ 
digitalWrite(LED, HIGH); 
Serial.println("LED Off"); 
} 
}

Let’s take a deeper look at the code and see what each line means:

#include <SoftwareSerial.h>

library you need for software serial communication

SoftwareSerial MyBlue(2, 3);

Software definition for serial pins; RX2 & TX3

MyBlue.begin(9600);

Configuring software serial baud rate at 9600

void loop() 
{
  if (MyBlue.available()) 
    flag = MyBlue.read(); 
  if (flag == 1) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED On"); 
  } 
  else if (flag == 0) 
  { 
    digitalWrite(LED, HIGH); 
    Serial.println("LED Off"); 
  } 
}
Reading serial data and Turning LEDs On/Off accordingly.

Sending AT-Commands to HC05 Bluetooth Module and Changing Its Settings

By pressing and holding the button the module switches into AT-command mode; Otherwise it works in the communication mode.

Some modules have a push button in their packages and there is no need to add one anymore.

The default baud rate to enter At-command mode is 38400.

 Now upload this code on your board and set commands using Serial Monitor.

/* 
HC05 - Bluetooth AT-Command mode 
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learnfarsi/guides
*/ 
#include "SoftwareSerial.h"
SoftwareSerial MyBlue(2, 3); // RX | TX 
void setup() 
{ 
  Serial.begin(9600); 
  MyBlue.begin(38400);  //Baud Rate for AT-command Mode.  
  Serial.println("***AT commands mode***"); 
} 
void loop() 
{ 
  //from bluetooth to Terminal. 
  if (MyBlue.available()) 
    Serial.write(MyBlue.read()); 
  //from termial to bluetooth 
  if (Serial.available()) 
    MyBlue.write(Serial.read());
}
You will receive the RESPONSE by sending a COMMAND to the module.
Command Response Description
AT OK Just test
AT+RESET OK Reset module
AT+VERSION? +VERSION: OK Firmware version of module
AT+ORGL OK Restore to default
AT+ADDR? +ADDR: OK Module address
AT+NAME? +NAME: OK Module name
AT+NAME=your name\r\n OK Change module name
AT+RNAME?param1\r\n +NAME: OK Bluetooth device name
AT+ROLE? +ROLE: Module role(0 is slave & 1 is master)
AT+ROLE=(0 as slave,1 as master)\n\r OK Change module role
AT+PSWD? PSWD: OK Module password
AT+PSWD=your pass\r\n OK Change module password
AT+UART? P3: Parity +UART= OK P1: Baud rate P2: Stop bit P3: Parity
For more commands refer to the HC05 datasheet:

HC05 Bluetooth Module

Liked What you see?

Get updates and learn from the best

More To Explore

Comments (2)

  • sankar Reply

    I am Trying to controlling the light using Text Message Via Bluetooth and i worked out using bt.write and read command the serial data will displaying but in receiving side of blue-tooth module but led was not operating (ON & OFF ) can any solution to workout this Code

    December 2, 2019 at 3:55 am
    • Saeed Hosseini Reply

      Did you fallow the tutorial completely?

      December 2, 2019 at 5:02 am

Leave a Reply

Your email address will not be published. Required fields are marked *