Getting Started with HC-05 Bluetooth Module: Interfacing with Arduino Uno

Getting Started with HC-05 Bluetooth Module: Interfacing with Arduino Uno

0 comments / Posted on by Quartz Components

Getting Started with HC-05 Bluetooth Module: Interfacing with Arduino Uno

Bluetooth is a communication protocol which provides an affordable communication in PAN (Personal Are Network). It provides a maximum data rate of 1Mb/S, working in a nominal range of 100 meters using 2.4 GHz frequency.

Due to these several factors, Bluetooth is popularly used all the way from hobby grade projects to industrial ones.

 

Devices which commonly harnesses Bluetooth communication:

  • Wireless speakers and headphones
  • Phones and computers
  • Car or home stereo system
  • Smart door locks
  • Wireless keypads and mouse
  • And many more

 

Getting started with HC-05 Bluetooth Module:

HC05 module is a Bluetooth module which uses serial communication. It is mostly used in electronic projects. It can work either as a slave or a master.

In,

Slave mode: It will connect to master device and will share data from him only.

Master mode: Several devices can connect to it and it can share data with all of them.

 

Bluetooth module specifications:

  • Model: HC-05
  • Communication Method: Serial Communication
  • Working voltage: 3.6V – 5V
  • Internal antenna: Yes
  • Automatic connection to the last device: Yes
  • Can switch between master mode and slave mode: Yes

HC-05 Bluetooth Module Specifications


HC-05 Bluetooth module consists of 6 pins which are STATE, RXD, TXD, GND, VCC and EN as mentioned on the back of the module.

HC-05 Bluetooth Module Pins

Functions of these pins are as follows:

HC-05 Bluetooth Module Pinout Configuration

 

Note: The two pins STATE and EN are of no use in this project.

 

HC-05 can be used with a number of microcontrollers like Arduino boards (Arduino UNO, Arduino Nano, Arduino Pro Mini etc.), ESP8266 Boards, STM32 Boards and any other development boards available out there.

In this article we will be using Arduino UNO for demonstrating the functions of HC05.

Arduino with HC05 Bluetooth Module

 

Connecting the HC-05 module with Arduino:

While connecting the module with Arduino you can either use the pre-defined RX, TX (0, 1) pins of the board or you can define any other pin as RX, TX pin using the SoftwareSerial library.

I find the second way more convenient in view of the fact that:

  • It not only prevents the hustle of removing the RX, TX connections every time while uploading the code
  • But also makes the code neater and crispier. I promise that to you. :D

In this example I am using pin number 10 and 11 of Arduino as the RX and TX pins respectively. Remember that RX of Arduino gets connected to TX of the module and vice versa. Other than RX and TX pins you just need to connect the power pins i.e. VCC and GND of module to 5v and GND of Arduino.

Arduino HC-05 Bluetooth Module Circuit Connections

Note: The 1k and 2k resistors connected to the RX pins are to convert 5v to 3.3VDC. As RX pin of the module works on 3v3 logic.

But, I have also tried connecting the module directly to 5v and it works flawless. But if you want to use your module for long durations then must connect the voltage divider.

 

Now, if you have made the connections properly. Let’s jump to the coding part. I had promised that the code will be simple and neat. No? Let me make one more. You won’t have any difficulty in understanding the code on your own.

 

Writing the Arduino HC-05 bluetooth module code:

For checking that our module is working or not. What we may do is, write a code for exchanging strings from our phone and displaying them in the serial monitor.

Let’s quickly write a code for that:- 

In the first two lines of the code, we just have to import the library and define a variable name for our BT module (I have named it QuartzBT, you can change it to whatever you like) Now, we have to define the pins through which our module is connected. Remember, it was 10 and 11 in our schematics.

#include <SoftwareSerial.h>

SoftwareSerial QuartzBT(10, 11); // RX | TX

 

Moving ahead to the setup part of the code.

Just initialize the Serial communication through the monitor as well as the module. Along with that, I am also printing a small message just to ensure that the module is ready to be paired.

 

void setup()

{

  Serial.begin(9600);

  QuartzBT.begin(9600);  //Default Baud for comm, it may be different for your Module.

  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");

}

 

Finally, the loop section. Well, frankly speaking we have nothing much left to do. Just send whatever data you are reading through Serial Monitor to your phone and from your phone to Serial monitor. That is it.

 

void loop()

{

  // Feed any data from bluetooth to Terminal.

  if (QuartzBT.available())

    Serial.write(QuartzBT.read());

  // Feed all data from termial to bluetooth

  if (Serial.available())

    QuartzBT.write(Serial.read());

}

 

We are done. Simple and neat. Isn’t it?

Just upload the code to your board after choosing the correct board and port. If you are using default RX|TX pins i.e. 0 and 1, then do remember to pluck out the connections. Otherwise it will give errors.

 

Action time!

Ah! We have already done everything now just install any app in your phone which lets you send strings over Bluetooth. You can either download them from Play Store or App Store or can make your own via MIT App Inventor. Let us stick to the easier way (xD). I installed an app named “Arduino Bluetooth Controller” by Giumig Apps.

Arduino Bluetooth Controller App

Now, you just need to connect with the module through your phone. As you normally connect other Bluetooth devices (remember the passcode is either 1234 or 0000). Open the app you just installed and select the terminal mode. Start the serial monitor in the Arduino IDE too.

Mobile App for Arduino HC 05 Connecting

Arduino LED Control via Android App

Connecting Android App with Arduino HC05

Connect mode

 

Arduino HC05 through Android App

Try sending anything from the app. You will find it all getting printed in the serial monitor. It also works the other way around. 

Congratulations again. You have successfully made your first project using HC05 Bluetooth module. Now, let’s do some more stuff. How about switching an LED on and off.

 

Required Components:

 

Adding LED to Control using HC-05 Module and Arduino Uno

We have already done every connections. Don’t do any changes to them. Just add an LED between any pin that you like and ground. I have added it to pin 7. Don’t forget to add a resistor to prevent the LED getting damaged by over-current.

Here is the schematic of the updated connections.

Arduino HC-05 Bluetooth Module Circuit Diagram

Made the connection changes? Now just add a simple line in the loop section of your code and upload the code again.

Here is the code for that:

 

#include <SoftwareSerial.h>

SoftwareSerial QuartzBT(10, 11); // RX | TX

int LED = 7;

int val;

void setup()

{

  Serial.begin(9600);

  QuartzBT.begin(9600);

  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");

  pinMode(LED, OUTPUT);

}

void loop()

{

  if (QuartzBT.available()) {

    val = QuartzBT.read();

    if (val == '1') {

      digitalWrite(LED, 1);

      QuartzBT.println("LED  On D07 ON ! ");

    }

    else if (val == '0') {

      digitalWrite(LED, 0);

      QuartzBT.println("LED  On D07 Off ! ");

    }

    else QuartzBT.println("Enter 0 for off or 1 for on :)"); 

  }

}

 

After successfully uploading the code you will see that when you send 1 from your phone the LED connected to digital pin 07 turns on and if you send 0 it turns off.

You can further tweak the UI experience via going to the toggle mode and configuring the button to send 0 for off and 1 for on and Voila! You create your crude Bluetooth remote.

  

This was it for this tutorial. I hope you learned something new and enjoyed making your first Bluetooth projects.

Thanks for Reading. Consider sharing the article too.

Bluetooth is a communication protocol which provides an affordable communication in PAN (Personal Are Network). It provides a maximum data rate of 1Mb/S, working in a nominal range of 100 meters using 2.4 GHz frequency.

Due to these several factors, Bluetooth is popularly used all the way from hobby grade projects to industrial ones.

 

Devices which commonly harnesses Bluetooth communication:

  • Wireless speakers and headphones
  • Phones and computers
  • Car or home stereo system
  • Smart door locks
  • Wireless keypads and mouse
  • And many more

 

Getting started with HC-05 Bluetooth Module:

HC05 module is a Bluetooth module which uses serial communication. It is mostly used in electronic projects. It can work either as a slave or a master.

In,

Slave mode: It will connect to master device and will share data from him only.

Master mode: Several devices can connect to it and it can share data with all of them.

 

Bluetooth module specifications:

  • Model: HC-05
  • Communication Method: Serial Communication
  • Working voltage: 3.6V – 5V
  • Internal antenna: Yes
  • Automatic connection to the last device: Yes
  • Can switch between master mode and slave mode: Yes

HC-05 Bluetooth Module Specifications


HC-05 Bluetooth module consists of 6 pins which are STATE, RXD, TXD, GND, VCC and EN as mentioned on the back of the module.

HC-05 Bluetooth Module Pins

Functions of these pins are as follows:

HC-05 Bluetooth Module Pinout Configuration

 

Note: The two pins STATE and EN are of no use in this project.

 

HC-05 can be used with a number of microcontrollers like Arduino boards (Arduino UNO, Arduino Nano, Arduino Pro Mini etc.), ESP8266 Boards, STM32 Boards and any other development boards available out there.

In this article we will be using Arduino UNO for demonstrating the functions of HC05.

Arduino with HC05 Bluetooth Module

 

Connecting the HC-05 module with Arduino:

While connecting the module with Arduino you can either use the pre-defined RX, TX (0, 1) pins of the board or you can define any other pin as RX, TX pin using the SoftwareSerial library.

I find the second way more convenient in view of the fact that:

  • It not only prevents the hustle of removing the RX, TX connections every time while uploading the code
  • But also makes the code neater and crispier. I promise that to you. :D

In this example I am using pin number 10 and 11 of Arduino as the RX and TX pins respectively. Remember that RX of Arduino gets connected to TX of the module and vice versa. Other than RX and TX pins you just need to connect the power pins i.e. VCC and GND of module to 5v and GND of Arduino.

Arduino HC-05 Bluetooth Module Circuit Connections

Note: The 1k and 2k resistors connected to the RX pins are to convert 5v to 3.3VDC. As RX pin of the module works on 3v3 logic.

But, I have also tried connecting the module directly to 5v and it works flawless. But if you want to use your module for long durations then must connect the voltage divider.

 

Now, if you have made the connections properly. Let’s jump to the coding part. I had promised that the code will be simple and neat. No? Let me make one more. You won’t have any difficulty in understanding the code on your own.

 

Writing the Arduino HC-05 bluetooth module code:

For checking that our module is working or not. What we may do is, write a code for exchanging strings from our phone and displaying them in the serial monitor.

Let’s quickly write a code for that:- 

In the first two lines of the code, we just have to import the library and define a variable name for our BT module (I have named it QuartzBT, you can change it to whatever you like) Now, we have to define the pins through which our module is connected. Remember, it was 10 and 11 in our schematics.

#include <SoftwareSerial.h>

SoftwareSerial QuartzBT(10, 11); // RX | TX

 

Moving ahead to the setup part of the code.

Just initialize the Serial communication through the monitor as well as the module. Along with that, I am also printing a small message just to ensure that the module is ready to be paired.

 

void setup()

{

  Serial.begin(9600);

  QuartzBT.begin(9600);  //Default Baud for comm, it may be different for your Module.

  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");

}

 

Finally, the loop section. Well, frankly speaking we have nothing much left to do. Just send whatever data you are reading through Serial Monitor to your phone and from your phone to Serial monitor. That is it.

 

void loop()

{

  // Feed any data from bluetooth to Terminal.

  if (QuartzBT.available())

    Serial.write(QuartzBT.read());

  // Feed all data from termial to bluetooth

  if (Serial.available())

    QuartzBT.write(Serial.read());

}

 

We are done. Simple and neat. Isn’t it?

Just upload the code to your board after choosing the correct board and port. If you are using default RX|TX pins i.e. 0 and 1, then do remember to pluck out the connections. Otherwise it will give errors.

 

Action time!

Ah! We have already done everything now just install any app in your phone which lets you send strings over Bluetooth. You can either download them from Play Store or App Store or can make your own via MIT App Inventor. Let us stick to the easier way (xD). I installed an app named “Arduino Bluetooth Controller” by Giumig Apps.

Arduino Bluetooth Controller App

Now, you just need to connect with the module through your phone. As you normally connect other Bluetooth devices (remember the passcode is either 1234 or 0000). Open the app you just installed and select the terminal mode. Start the serial monitor in the Arduino IDE too.

Mobile App for Arduino HC 05 Connecting

Arduino LED Control via Android App

Connecting Android App with Arduino HC05

Connect mode

 

Arduino HC05 through Android App

Try sending anything from the app. You will find it all getting printed in the serial monitor. It also works the other way around. 

Congratulations again. You have successfully made your first project using HC05 Bluetooth module. Now, let’s do some more stuff. How about switching an LED on and off.

 

Required Components:

 

Adding LED to Control using HC-05 Module and Arduino Uno

We have already done every connections. Don’t do any changes to them. Just add an LED between any pin that you like and ground. I have added it to pin 7. Don’t forget to add a resistor to prevent the LED getting damaged by over-current.

Here is the schematic of the updated connections.

Arduino HC-05 Bluetooth Module Circuit Diagram

Made the connection changes? Now just add a simple line in the loop section of your code and upload the code again.

Here is the code for that:

 

#include <SoftwareSerial.h>

SoftwareSerial QuartzBT(10, 11); // RX | TX

int LED = 7;

int val;

void setup()

{

  Serial.begin(9600);

  QuartzBT.begin(9600);

  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");

  pinMode(LED, OUTPUT);

}

void loop()

{

  if (QuartzBT.available()) {

    val = QuartzBT.read();

    if (val == '1') {

      digitalWrite(LED, 1);

      QuartzBT.println("LED  On D07 ON ! ");

    }

    else if (val == '0') {

      digitalWrite(LED, 0);

      QuartzBT.println("LED  On D07 Off ! ");

    }

    else QuartzBT.println("Enter 0 for off or 1 for on :)"); 

  }

}

 

After successfully uploading the code you will see that when you send 1 from your phone the LED connected to digital pin 07 turns on and if you send 0 it turns off.

You can further tweak the UI experience via going to the toggle mode and configuring the button to send 0 for off and 1 for on and Voila! You create your crude Bluetooth remote.

  

This was it for this tutorial. I hope you learned something new and enjoyed making your first Bluetooth projects.

Thanks for Reading. Consider sharing the article too.

0 comments

Leave a comment

All blog comments are checked prior to publishing