Getting Started with HC-05 Bluetooth Module & Arduino

Getting Started with HC-05 Bluetooth Module & Arduino © GPL3+

Learn how to communicate and send data over Bluetooth using an HC-05 module and an Arduino board.

  • 3,378 views
  • 0 comments
  • 4 respects

Components and supplies

Apps and online services

About this project

You can read this and other amazing tutorials onElectroPeak's official website

In this tutorial, you will learn how to communicate and send data with Bluetooth using HC05 Bluetooth Module and Arduino board. At the end of this article, 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 a maximum data rate of 1Mb/S, working in a 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.

HC05 Bluetooth module important specifications:

Working voltage: 3.6V – 5VInternal antenna: YesAutomatic connection to the last device: Yes

Sending Data to Arduino via Bluetooth

HC05 module has an internal 3.3v regulator and that is why you can connect it to 5v voltage. But we strongly recommend 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 the 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.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 baud rate is 9600 by default.

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#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. You can download it here.

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/learn/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. Here are some of the most important AT commands:

Code

code 1Arduino
/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#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"); 
  } 
} 
code 2Arduino
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"); 

  } 

 

} 
code 3Arduino
/* 
HC05 - Bluetooth AT-Command mode 
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/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());
}

Comments

Similar projects you might like

Bluetooth HM-10 Module

by Ingo Lohs

  • 1,956 views
  • 2 comments
  • 13 respects

Bluetooth Control Multitap

Project showcase by gledel and 윤원호

  • 1,623 views
  • 0 comments
  • 5 respects

Morse Code Communication Using Laser Module (Both)

Project tutorial by SURYATEJA

  • 1,111 views
  • 1 comment
  • 9 respects

Otto DIY+ Arduino Bluetooth Robot Easy to 3D Print

Project tutorial by Team Otto builders

  • 56,729 views
  • 122 comments
  • 185 respects
Add projectSign up / Login