Arduino and HC-05 Bluetooth Module Complete Tutorial

In this Arduino Bluetooth Tutorial we will learn how use the HC-05 module for controlling Arduino via Bluetooth communication. You can watch the following video or read the written tutorial below for more details.

Overview

For this tutorial I made two example, controlling the Arduino using a smartphone and controlling the Arduino using a laptop or a PC. In order not to overload this tutorial, in my next tutorial we will learn how we can configure the HC-05 Bluetooth module and make a Bluetooth communication between two separate Arduino Boards as master and slave devices.

HC 05 Bluetooth Modulehttps://howtomechatronics.com/wp-content/uploads/2016/02/HC-05-Bluetooth-Module-300x138.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" class="clear" data-reader-unique-id="20" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Before we start with the first example, controlling an Arduino using a smartphone, let’s take a closer look at the HC-05 Bluetooth module. Comparing it to the HC-06 module, which can only be set as a Slave, the HC-05 can be set as Master as well which enables making a communication between two separate Arduino Boards. There are several different versions of this this module but I recommend the one that comes on a breakout board because in that way it’s much easier to be connected. The HC-05 module is a Bluetooth SPP (Serial Port Protocol) module, which means it communicates with the Arduino via the Serial Communication.

You can get the components needed for this Arduino tutorial from any of the sites below:

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

Circuit Schematics


Here’s how we need to connect the module to the Arduino Board.

Arduino and HC 05 Bluetooth Module Circuit Schematics - Arduino Bluetooth Tutorialhttps://howtomechatronics.com/wp-content/uploads/2016/02/Arduino-and-HC-05-Bluetooth-Module-Circuit-Schematics-300x117.png 300w" sizes="(max-width: 700px) 100vw, 700px" class="clear" data-reader-unique-id="48" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

The particular module that I have can be powered from 3.6 to 6 volts, because it comes on breakout board which contains a voltage regulator. However, the logic voltage level of the data pins is 3.3V. So, the line between the Arduino TX (Transmit Pin, which has 5V output) and the Bluetooth module RX (Receive Pin, which supports only 3.3V) needs to be connected through a voltage divider in order not to burn the module. On the other hand, the line between the Bluetooth module TX pin and the Arduino RX pin can be connected directly because the 3.3V signal from the Bluetooth module is enough to be accepted as a high logic at the Arduino Board.

Arduino Bluetooth Communication Example Source Code


So, now we are ready to make the Arduino code for enabling the communication between the Arduino board and the smartphone. We will make a simple example, just turning on and off a LED but it will be good enough for understanding the communication.

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

Serial.begin(38400); // Default communication rate of the Bluetooth module

if(Serial.available()> 0){ // Checks whether data is comming from the serial port

state = Serial.read(); // Reads the data from the serial port

digitalWrite(ledPin, LOW); // Turn LED OFF

Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"

elseif(state == '1'){

digitalWrite(ledPin, HIGH);

Serial.println("LED: ON");;

Description: First we need to define the pin to which our LED will be connected and a variable in which we will store the data coming from the smartphone. In the setup section we need to define the LED pin as output and set it low right away. As mention previously, we will use the serial communication so we need to begin the serial communication at 38400 baud rate, which is the default baud rate of the Bluetooth module. In the loop section with the Serial.available() function we will check whether there is available data in the serial port to be read. This means that when we will send data to the Bluetooth module this statement will be true so then using the Serial.read() function we will read that data and put it into the “state” variable. So if the Arduino receive the character ‘0’ it will turn the LED off and using the Serial.println() function it will send back to the smartphone, via the serial port, the String “LED: OFF”. Additionally we will reset the “state” variable to 0 so that the two above lines will be executed only once. Note here that the “state” variable is integer, so when we receive the character ‘0’ that comes from smartphone, the actual value of the integer “state” variable is 48, which corresponds to character ‘0’, according to the ASCII table.. That’s why in the “if” statement we are comparing the “state” variable to a character ‘0’. On the other hand, if the received character is ‘1’, the LED will light up and the String “LED: ON” will be sent back.

Now the code is ready to be uploaded but in order to do that we need to unplug the TX and RX lines because when uploading the Arduino uses the serial communication so the pins RX (digital pin 0) and TX (digital pin1) are busy. We can avoid this step if we use the other TX and RX pins of the Arduino Board, but in that case we will have to use the SoftwareSerial.h library for the serial communication.

Related: How To Configure and Pair Two HC-05 Bluetooth Modules as Master and Slave | AT Commands

Connecting the Smartphone to the HC-05 Bluetooth Module and the Arduino


Now we are ready to connect the smartphone to the Bluetooth module and the Arduino. What we need to do here is to activate the Bluetooth and the smartphone will find the HC-05 Bluetooth module.

Smartphone Bluetooth Settingshttps://howtomechatronics.com/wp-content/uploads/2016/02/Smartphone-Bluetooth-Settings-300x169.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" class="clear" data-reader-unique-id="245" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Then we need to pair the devices and the default password of the HC-05 module is 1234. After we have paired the devices we need an application for controlling the Arduino. There are many application in the Play Store for this purpose which will work with the Arduino code that we wrote. However, I made my own custom application for this tutorial using the MIT App Inventor online application. This is a great and easy to use application for building Android application and in my next tutorial you can find a detailed step by step guide how to build your own custom Android application for your Arduino Project.

Arduino and Android Bluetooth Apphttps://howtomechatronics.com/wp-content/uploads/2016/02/Android-Bluetooth-App-300x169.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" class="clear" data-reader-unique-id="250" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

You can download the app that I made for this example here:

With the connect button we will connect the smartphone to the Bluetooth module and the status text below the button will tell us whether we have successfully connected. Using the “Turn ON” and “Turn OFF” buttons we can turn on and off the LED. The text above the buttons is the one that the Arduino is sending back to the smartphone when a particular button is pressed.

Controlling Arduino Using a Laptop or PC via Bluetooth Communication


Let’s see how we can control the Arduino via Bluetooth using a Laptop or a PC. So, first we need to pair our laptop to the HC-05 Bluetooth module and we can do that from the Laptop Bluetooth Settings. The laptop will discover the HC-05 module and using the ‘1234’ password we will pair the devices.

Arduino and HC-05 Bluetooth Module - Laptop Settings 1https://howtomechatronics.com/wp-content/uploads/2016/02/Arduino-and-HC-05-Bluetooth-Module-Laptop-Settings-1-300x88.jpg 300w" sizes="(max-width: 750px) 100vw, 750px" class="clear" data-reader-unique-id="272" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Once we will pair the devices in the Laptop Device Manager, under Ports (COM & LPT), two new entities will appear named “Standard Serial over Bluetooth link”. From here we can see the COM Port number of the serial port through which the devices will communicate.

Arduino and HC-05 Bluetooth Module - Laptop Settings 2https://howtomechatronics.com/wp-content/uploads/2016/02/Arduino-and-HC-05-Bluetooth-Module-Laptop-Settings-2-300x168.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" class="clear" data-reader-unique-id="275" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

We will stick with the same example as previously, turning on and off a LED and sending back a string to the laptop, so we will use the same Arduino code as previously described.

Processing IDE Source Code


Now using the Processing IDE we will make a program for controlling the Arduino. Here’s the source code.

import processing.serial.*;

String ledStatus="LED: OFF";

myPort = newSerial(this"COM5", 38400); // Starts the serial communication

myPort.bufferUntil('\n'); // Defines up to which character the data from the serial port will be read. The character '\n' or 'New Line'

voidserialEvent(Serial myPort){ // Checks for available data in the Serial Port

ledStatus = myPort.readStringUntil('\n'); //Reads the data sent from the Arduino (the String "LED: OFF/ON) and it puts into the "ledStatus" variable

background(237, 240, 241);

fill(20, 160, 133); // Green Color

rect(50, 100, 150, 50, 10)// Turn ON Button

rect(250, 100, 150, 50, 10); // Turn OFF Button

text("Turn ON",60, 135);

text("Turn OFF", 255, 135);

text("Status:", 180, 200);

text("Program made by Dejan Nedelkovski,\n www.HowToMechatronics.com", 80, 320);

text(ledStatus, 155, 240); // Prints the string comming from the Arduino

// If the button "Turn ON" is pressed

if(mousePressed && mouseX>50 && mouseX<200 && mouseY>100 && mouseY<150){

myPort.write('1'); // Sends the character '1' and that will turn on the LED

// Highlighs the buttons in red color when pressed

rect(50, 100, 150, 50, 10);

// If the button "Turn OFF" is pressed

if(mousePressed && mouseX>250 && mouseX<400 && mouseY>100 && mouseY<150){

myPort.write('0'); // Sends the character '0' and that will turn on the LED

rect(250, 100, 150, 50, 10);

Description: We need to include the Serial library and create a serial object in order to enable the serial communication, as well as, define a String variable for the led status. In the setup section we need to set the window size of the program and start the serial communication. As for the COM Port Number here we need to try one of the two COM Port numbers we previously noticed in the device manager. The next line defines the buffering of the serial port and in our case that’s until there is a new line and actually there is a new line each time the Arduino sends the String “LED: OFF” or “LED ON” because of the println() function. Next, using the serialEvent() function we check whether there is available data in the serial port to be read. If so, using the readStringUntil() function we will read that data from the serial port which has been sent from the Arduino and in our case that’s the String “LED: OFF” or “LED: ON”.

In the main draw() function, which constantly repeats, we make all the graphics and functions of the program. So first we need set the background color, the fill color, the stroke size and color and using the rect() function we draw the two buttons. Using the text() function we print all the text, including the ledStatus string that’s coming from the Arduino. What’s left now is to make the buttons functional. So using the first “if” statement we confine the area of the “Turn ON” button, so when the button is pressed the character ‘1’ will be sent over the serial port to the Arduino and that will turn on the LED. The next for lines are used to highlight the button when it’s pressed. The same procedure goes for the “Turn OFF” button.

Arduino and Laptop Bluetooth Communication via Processing IDEhttps://howtomechatronics.com/wp-content/uploads/2016/02/Arduino-and-Laptop-Bluetooth-Communication-via-Processing-IDE-300x168.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" class="clear" data-reader-unique-id="686" style="max-width: 100%; margin: 0.5em auto; display: block; height: auto; clear: both;">

Now the program is ready, so when we will click the run button, the program will automatically activate the Bluetooth communication between the laptop and the Arduino. The HC-05 Bluetooth module will start to flash every two seconds, which indicates that the module is connect and we will be able to control the LED using our Laptop.

Thant’s all for this tutorial, but don’t forget to check my next tutorial where we will learn how we can configure the HC-05 Bluetooth module and make a Bluetooth communication between two separate Arduino Boards as master and slave devices.

Also, feel free to ask any question in the comments section below and don’t forget to check out my collection of Arduino Projects.