Arduino and HC-05 Bluetooth Module Complete Tutorial

Arduino and HC-05 Bluetooth Module Complete Tutorial

Adding Bluetooth to your Arduino is the simplest way to give your Arduino projects the power of wireless possibilities. 

You can get rid of messy wires between the Arduino and the moisture sensor, or even better, you can read the moisture value on your phone directly. 

You can control a remote car, a toy crane, or a robotic arm using your mobile with a Bluetooth module and Arduino. 

You will be able to create interesting Bluetooth wireless projects by the end of this tutorial.

HC-05 Module is a versatile, low-cost, and very easy-to-use device. I will show you how to use the HC-05 Bluetooth module with Arduino. 

I have given all the critical information about HC-05, such as pin details, voltage, current specifications, communication protocol information, etc.

I will give you working project examples with connection diagrams and code for HC-05 projects, which you can use straight away.

Let us get started!

Components Needed To Connect HC-05 To Arduino

Hardware Components 

Software


Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

What Is The HC-05 Module, And Why Is It So Famous?

HC-05 is a serial Bluetooth Serial port protocol module that is easy to use. You can communicate with HC-05 over serial protocol.

The UART-style communication mode makes it a favorite choice for Arduino enthusiasts. 

I will show you the HC-05 pins and the descriptions in the table below:

Sl. NoPin NamePin Description
1VCCThis is the Power supply pin of the module. You can connect 5 V from the Arduino UNO to this pin.
2GNDGND pin of the HC-05 should be connected to the ground connection of your project. This can be connected to the GND pin on the Arduino UNO
3TXDTXD is the transmit pin of the Bluetooth module. Connect the TXD pin of HC-05 to the RXD pin of the MCU (Arduino UNO PIN 0)
4RXDRXD is the receive pin of the module. You will send the commands to the module via this RXD pin. Connect TXD Pin of the MCU (Arduino UNO PIN 1)
5KEYThe KEY pin enables you to put the HC-05 Bluetooth module in AT command mode. 
6STATEThe STATE pin tells the Arduino UNO whether the HC-05 is connected to a Bluetooth device or not. The STATE pin will be high when it is connected to a Bluetooth device. It will be low when it si not connected to any device. 

I will share with you more details about the AT modes and the KEY pin functionality in the sections below.

HC-05 Module can act like a Master as well as a slave. You can use AT commands to put the HC-05 module in the master mode. 

The master mode helps you connect the HC-05 module with other Bluetooth slave devices.

AT mode also allows you to change the name of the module (as it appears in the list of Bluetooth devices).

-> Read our guide about What You Can Build with Adruino.

Step-By-Step Instructions To Connect HC-05 Bluetooth Module To An Arduino

I will show you how to connect your HC-05 module to an Arduino UNO in a few simple steps. 

HC05 module comes with a 2.54 mm bergstick connector. You can use a male-to-female Dupont cable to connect HC-05 and the Arduino UNO.

Dupont cable to connect HC-05 and the Arduino UNO

Step 1: How to connect HC-05 to Arduino UNO?

1) Start with the GND pin connection

Start with the GND pin connection

My advice is to connect the GND pin first all the time. Never power on the board until you have completed all the connections. Make sure you cross-check the connections again.

2) Connect the TXD Pin

Connect the TXD Pin

Connect the TXD pin of the Bluetooth module to the RXD pin of the Arduino (Yellow cable).

TXD is the output pin of the HC-05 Module.

RXD on UNO is the input pin of the Arduino. 

3) Connect the RXD Pin

Connect the RXD Pin

Connect the TXD pin of the Arduino (PIN 1) to the RXD pin of the HC-05 module. You can refer to the orange cable in the above image.

4) Connect the Power Pin

Connect the Power Pin

Connect the 5V pin from the Arduino to the VCC pin of the HC-05 module. These steps complete the critical connections needed to communicate with the Bluetooth module.

We will see how to program and start working with the HC-05 module in the following sections.

Note: Since the serial terminal also uses the standard UART pins (PIN 1 and PIN 0), many projects and examples recommend you adapt the SoftwareSerial feature. 

SoftwareSerial feature allows you to configure any two pins of the Arduino as RXD and TXD pins.

Please visit the official documentation page for more information about the software serial.

Step 2: Learn about the STATE and KEY Pins of the HC-05 Bluetooth Module

In this section, I will share all the necessary details to understand the functionalities of STATE and KEY pins. 

1) Purpose of STATE Pin on HC-05 Bluetooth Module

The STATE pin of the HC-05 Bluetooth module is connected to the onboard LED. The STATE pin is also terminated on the connector.

The STATE pin tells you whether there is a Bluetooth connection present or not.

Purpose of STATE Pin on HC-05 Bluetooth Module

By looking at the LED, you can know whether the connection is active or not. If there is an active connection, the LED will be ON. The LED will be off if there is no connection. 

You can connect the STATE pin to any Arduino GPIO pin. The STATE helps you read the connection status in your code later. 

If you read a logic HIGH, there is an active connection. If there is no active connection, you will read logic zero. 

Connecting the STATE pin to the Arduino is not mandatory but helps provide more debugging data for the user.

2) Purpose of the KEY Pin on HC-05 Bluetooth Module

The purpose of the KEY pin is to toggle the HC-05 Bluetooth module from Data mode to command mode and vice versa. 

If no connections are made, the HC-05 module will be in data mode by default.

If you want to put the module into command mode, use an Arduino GPIO pin to drive the KEY pin to logic High.

Project 1: A simple LED blink application using HC-05 Bluetooth Module

In this project, you will program the Arduino so that you will turn on and off an LED connected to Arduino UNO using your Mobile. 

The HC-05 Bluetooth module communicates with the mobile wirelessly. This project is equivalent to the “Hello World” program.

Let us get started!

1) Connection diagram

Note that the serial lines are connected to PIN 2 and PIN 3 instead of the standard RXD and TXD Pins of the Arduino. 

Having a separate serial port helps you avoid conflict between the Serial Terminal of the Arduino and the HC-05. 

You can configure the Arduino PIns to act like UART pins in the code using the SerialSOftware function.

Refer to the code section for more details.

Connection diagram

2) Arduino Code 

Reference: https://create.arduino.cc/projecthub/electropeak/getting-started-with-hc-05-bluetooth-module-arduino-e0ca81

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
Home
*/ #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"); } }

3) Code walkthrough

SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int LED = 8;

An object named MyBlue is created using the SoftwareSerial library. The flag is a variable used to store the input from Bluetooth. 

The LED is connected to PIN 8 of the Arduino. You will toggle the LED using a Bluetooth serial app on the phone.

 Serial.begin(9600); 
 MyBlue.begin(9600); 
 pinMode(LED, OUTPUT);

There are two serial ports enabled. The first line activates the hardware serial port on the UNO. The baud rate is set to 9600. 

The second line enables the software serial port (for the HC-05 Bluetooth module). The PIN 8 of the Arduino is configured as Output. 

The rest of the code checks whether the softwareSerial port has some data available. Id there is a data, you will verify whether it is a zero or a one. 

You need to install an app on your phone to communicate with the HC-05 module. There are several apps available.

Here are a few recommended mobile apps:

Hint: You can use the same concept in the project above, to increase the number of options such as UP, DOWN, RIGHT, LEFT, A, B, C, D as in game pads. “A” is treated as a separate entity when compared to “a”.  

I had done a mistake of not considering this and had spent a lot of time debugging.

It was a good learning experience.

Project 2: How To Rename The HC-05? (How To Put HC-05 In Command Mode)?

I will show you how to put the HC-05 into command mode and change a few parameters. 

To do this, you need to access the KEY button. You can access the KEY pin using Arduino GPIO and manually using the KEY Button. 

1) Connection diagram

Notice that PIN 9 of the Arduino is connected to the KEY pin of the HC-05. This gives you the ability to control the mode of the HC-05 using software on the Arduino.

PIN 9 of the Arduino is connected to the KEY pin of the HC-05

2) Arduino Code 

Reference: https://droiduino-cc.medium.com/changing-arduino-bluetooth-name-if-you-are-using-hc05-module-6f184a242467

/*
 * Changing HC05 module's default device name using AT Command
 * by Droiduino.cc
 *
 * Pinout:
 * Key --> pin 9
 * VCC --> Vin
 * TXD --> pin 10
 * RXD --> pin 11
 * GND --> GND
 *
 * AT Command : AT+NAME=<new device name>
 *
 */
 
#include <SoftwareSerial.h>
 
SoftwareSerial BTSerial(10, 11);   // RX | TX
 
void setup()
{
  pinMode(9, OUTPUT);
  digitalWrite(9, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command mode
}
 
void loop()
{
  // Read from HC05 and send to Arduino
  if (BTSerial.available())
    Serial.write(BTSerial.read());
 
  // Read from serial monitor and send to HC05
  if (Serial.available())
    BTSerial.write(Serial.read());
}

3) Code walkthrough

I will highlight the key sections of the code.

digitalWrite(9, HIGH);

Writing HIGH to the PIN 9 puts the HC-05 into command mode. PIN 9 of the Arduino is connected to the KEY pin of the HC-05 module.

 BTSerial.begin(38400);  // HC-05 default speed in AT command mode

Once you put the HC-05 into command mode, you can start sending the AT commands. The default baud of the HC-05 is 38400. 

I will show you how to frame and send the commands in the following steps. Open the serial terminal in the Arduino IDE.

You must set the option as shown in the image below (select Both “NL and CR”).

Make sure the baud rate is set to 9600. 9600 is the baud rate corresponding to the Arduino serial port (not the Bluetooth serial port).

Now, type AT and hit the send button. You will get “OK” as a response.

type AT and hit the send button

Send the below message in the terminal. You can choose your name. Once you send the below message, you will get a response as OK.

AT+NAME=<new device name>

Congratulations, your HC-05 Bluetooth module is now ready with a new name. Power off everything and power on again. 

When you scan the list of devices on your mobile now, the Bluetooth module will appear in the new name you have just updated.

-> Read our article about How Easy Is It To Learn Arduino?

Interesting Projects People Have Made Using HC-05 Bluetooth Modules

I have listed a couple of exciting Bluetooth wireless Arduino projects here.

I hope you like them too and try to build something similar for your upcoming projects.

1) Drive a four-wheel-drive car using Bluetooth using Arduino

Drive a four-wheel-drive car using Bluetooth using Arduino

Link: https://create.arduino.cc/projecthub/andriy-baranov/smartphone-controlled-arduino-4wd-robot-car-14d239

Brief description

You will control a 4-wheel drive car using your smartphone via a Bluetooth module.

Using the phone, you will be able to drive the car in all directions, control headlights, and even can horn.

A very easy-to-use and fun project based on HC-06 Bluetooth Module.

2) Upload Arduino Sketch Using Bluetooth Module

Upload Arduino Sketch Using Bluetooth Module

Link: https://create.arduino.cc/projecthub/mansurkamsur/upload-sketch-arduino-over-bluetooth-using-android-f1ce55

Brief description

In this project, you can upload the new sketch to your Arduino without connecting the Arduino to the computer.

A very helpful use-case when Arduino is already installed into a device, and there is no possibility to connect Arduino directly to the PC.

A must-try!

FAQs About The Arduino and HC-05 Bluetooth module Connection

How to connect Arduino Mega to HC-05?

There is no difference in how the code behaves when you connect Arduino Mega instead of an Arduino UNO to the HC-05.

You can use the code above and still make the project work. Make sure the serial pins are appropriately mapped. 

Note: Arduino Mega has more than one serial port. It means you don’t have to use the SoftwareSerial function. It is one of the advantages of using Arduino Mega for this project instead of Arduino UNO. 

How do I connect Bluetooth to Arduino?

There is no Bluetooth on the Arduino UNO. You can connect Arduino to a Bluetooth link using add-on cards. HC-05, HC-06, and HM-10 are a few examples.

These add-on cards help you enable the Arduino UNO’s Bluetooth option.

These are very helpful in creating wireless low-energy projects. 

Can I control the Arduino with Bluetooth?

Yes. You can control Arduino using Bluetooth. In the examples above, you can see that it is straightforward to use HC-05 Bluetooth modules to control the Arduino using your smartphone.

> Check out our guide to the Top 12 Best Arduino Online Courses

Conclusion

In this tutorial, we learned that the HC-05 is a handy add-on. The low-cost device adds excellent features to your projects and enables us to create beautiful applications.

I have used the module to control a water dispenser.

I hope you have enjoyed the article, learned about the HC-05 module, and are now empowered to create Bluetooth wireless projects.

I will be glad to know the projects you will build. Please drop a link to your projects in the comments.

If you have any questions or suggestions, please drop them in the comments sections below.

I will make sure that I answer all of them as the earliest.

What would you love to hear about next?

You can propose your favorite topics!

Thomas Kane

Sunday 4th of December 2022

The"Serial Monitor" and the IDE do NOT show a "Send" "button: on the screen. Instead, the Serial Monitor displays "Message (Ctrl+Enter to send message to 'Arduino Uno' on 'COM9')". This results in NO display from the hc05/Sketch. Can I send the command by hard-coding it in the sketch? How do I read the HC05 response?

Tom Kane / Vancouver, BC, Canada - Dec. 3, 2022