You are currently viewing Arduino and HC-05/HC-06 Bluetooth Module Complete Tutorial
HC-05 Bluetooth module used in Arduino projects.

Arduino and HC-05/HC-06 Bluetooth Module Complete Tutorial

In this arduino bluetooth tutorial, we will explore how to use the HC-05/HC-06 Bluetooth module to control an Arduino via Bluetooth communication. We will start with a simple example of controlling an LED and then move on to controlling a 230V lamp using a relay. Follow the steps below to create a functional Bluetooth control system with your Arduino. You can visit this tutorial to learn how to program Arduino and control an LED with it.

Arduino Bluetooth Tutorial HC-05 Bluetooth Module

This tutorial will guide you through understanding the HC-05/HC-06 Bluetooth modules and using them to control an Arduino. We will create a simple Android app using MIT App Inventor to send commands to the Arduino, first to control an LED and then a lamp.

Key Features of the HC-05 and HC-06 Bluetooth Modules

In this section of our arduino Bluetooth tutorial, we will explore the key features of the HC-05 and HC-06 Bluetooth modules. These modules are popular choices for wireless communication in Arduino projects, offering reliable connectivity and ease of use. Below is a comparison of their main characteristics:

Feature HC-05 Bluetooth Module HC-06 Bluetooth Module
Bluetooth Protocol
Bluetooth Specification v2.0+EDR
Bluetooth Specification v2.0+EDR
Frequency
2.4GHz ISM Band
2.4GHz ISM Band
Modulation
GFSK (Gaussian Frequency Shift Keying)
GFSK (Gaussian Frequency Shift Keying)
Transmit Power
4dBm, Class 2
4dBm, Class 2
Sensitivity
-84dBm at 0.1% BER
-84dBm at 0.1% BER
Data Rate
Asynchronous: 2.1Mbps (Max) / 160 kbps, Synchronous: 1Mbps/1Mbps
Asynchronous: 2.1Mbps (Max) / 160 kbps
Supported Profiles
Bluetooth Serial Port (Master & Slave)
Bluetooth Serial Port (Slave only)
Operating Voltage
3.3 - 5 V DC / 50mA
3.3 - 5 V DC / 50mA
Operating Temperature
-5°C to +45°C
-5°C to +45°C
Baud Rate
Configurable from 2400 to 1382400
Fixed at 9600

HC-05/HC-06 Bluetooth Modules in the Arduino Tutorial

Before we start with the practical examples, let’s understand what these Bluetooth modules are and how they function.

HC-05 vs. HC-06
HC-05: Can be configured as both master and slave.
HC-06: Can only be configured as a slave.
Both modules use Serial Port Protocol (SPP) to communicate with the Arduino, making them easy to use for wireless communication.

Components Needed
HC-05 Bluetooth Module
Arduino Board
Breadboard and Jump Wires
Relay Module
Lamp (230V)

Part 1: Controlling an LED

Circuit Schematics

Here’s how to connect the HC-05/HC-06 Bluetooth module to the Arduino Board:

1- HC-05/HC-06 Bluetooth Module:
VCC to 5V on the Arduino.
GND to GND on the Arduino.
TXD to RX (Pin 0) on the Arduino (use a voltage divider for safety).
RXD to TX (Pin 1) on the Arduino.
2- LED:
Connect the anode (longer leg) of the LED to Pin 7 on the Arduino (with a current-limiting resistor).
Connect the cathode (shorter leg) of the LED to GND on the Arduino.

Arduino Bluetooth Tutorial HC-05 Module LED Control Circuit Diagram

Arduino Bluetooth Communication Example Source Code

We will write Arduino code to enable communication between the Arduino board and the smartphone to turn an LED on and off.

/**
 * Author: Omar Draidrya
 * Date: 2024/06/07
 * This code controls the forward and backward movement of a motor using an H-bridge.
 */

#define ledPin 7 // Pin for the LED
char state = 0; // Variable to store incoming data

void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as output
  digitalWrite(ledPin, LOW); // Initialize the LED state to OFF
  Serial.begin(9600); // Baudrate set to 9600 for consistency
}

void loop() {
  if (Serial.available() > 0) { // Check if data is coming from the serial port
    state = Serial.read(); // Read the data from the serial port
    if (state == '0') { // If the received data is '0'
      digitalWrite(ledPin, LOW); // Turn LED OFF
      Serial.println("LED: OFF"); // Send back to the phone
    } else if (state == '1') { // If the received data is '1'
      digitalWrite(ledPin, HIGH); // Turn LED ON
      Serial.println("LED: ON"); // Send back to the phone
    }
  }
}


    

Description: This code sets up the Arduino to communicate with a smartphone via Bluetooth to control an LED. The LED is connected to pin 7. The Serial.begin(38400) starts the serial communication at 38400 baud rate. The loop function checks for incoming data and turns the LED on or off based on the received command (‘0’ or ‘1’).

Developing a Simple App with MIT App Inventor

Step-by-Step Guide to Creating the App

1- Register and Log In:
Go to MIT App Inventor.
Log in with your Google account.

2- Create a New Project:
Click on “Start New Project” and name it “BluetoothControl”.

3- Design the User Interface:
Add a Label and set the text to “Bluetooth Control”.
Add a ListPicker and set the text to “Select Bluetooth Device”. Name it “ListPicker_Bluetooth”.
Add two Buttons:
Name: “Button_ON”, Text: “Turn ON”.
Name: “Button_OFF”, Text: “Turn OFF”.
Add a BluetoothClient component from the Connectivity palette (non-visible component).

4- Program the Blocks:
Go to the Block Editor.
Add the following blocks:

when ListPicker1.BeforePicking
  set ListPicker1.Elements to BluetoothClient1.AddressesAndNames

when ListPicker1.AfterPicking
  if call BluetoothClient1.Connect address ListPicker1.Selection
    then set Label1.Text to "Connected"
    else set Label1.Text to "Not Connected"

when Button1.Click
  if BluetoothClient1.IsConnected
    call BluetoothClient1.SendText text "1"

when Button2.Click
  if BluetoothClient1.IsConnected
    call BluetoothClient1.SendText text "0"
    
Arduino Bluetooth Control App Interface

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

1- Pair Bluetooth Device:
Turn on Bluetooth on your smartphone.
Pair with the HC-05/HC-06 module (default password is 1234).

2- Connect via App:
Use the app to connect to the Bluetooth module.
Use the “Turn ON” and “Turn OFF” buttons to control the LED.

Part 2: Controlling a 230V Lamp in the Arduino Bluetooth Module Tutorial

In this part, we will extend our project to control a 230V lamp using a relay module. The relay will act as a switch to control the high voltage lamp.

Circuit Schematics

Here’s how to connect the HC-05/HC-06 Bluetooth module, relay module, and the lamp to the Arduino Board:

1- HC-05/HC-06 Bluetooth Module:
VCC to 5V on the Arduino.
GND to GND on the Arduino.
TXD to RX (Pin 0) on the Arduino (use a voltage divider for safety).
RXD to TX (Pin 1) on the Arduino.

2- Relay Module:
VCC to 5V on the Arduino.
GND to GND on the Arduino.
IN to Pin 7 on the Arduino.

3- Lamp (230V):
Connect one wire of the lamp to the normally open (NO) terminal of the relay.
Connect the common (COM) terminal of the relay to the live wire (L) from the power source.
Connect the neutral wire (N) from the power source directly to the lamp.

Note: Be extremely careful when working with 230V AC. Ensure all connections are secure and insulated. If you are not confident in handling high voltage, seek assistance from a qualified electrician.

Arduino Bluetooth Tutorial HC-05 Module Relay Lamp Control Circuit Diagram

Arduino Bluetooth Communication Example Source Code

We will write Arduino code to enable communication between the Arduino board and the smartphone to turn the lamp on and off via a relay.

/**
 * Author: Omar Draidrya
 * Date: 2024/06/07
 * This code controls the forward and backward movement of two DC motors using the Adafruit Motor Shield.
 */

#define relayPin 7
int state = 0;

void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  if (Serial.available() > 0) { // Check if data is coming from the serial port
    state = Serial.read(); // Read the data from the serial port
  }

  if (state == '0') {
    digitalWrite(relayPin, LOW); // Turn Relay OFF
    Serial.println("Lamp: OFF"); // Send back to the phone
    state = 0;
  } else if (state == '1') {
    digitalWrite(relayPin, HIGH); // Turn Relay ON
    Serial.println("Lamp: ON"); // Send back to the phone
    state = 0;
  }
}

    

Description: This code sets up the Arduino to communicate with a smartphone via Bluetooth to control a lamp through a relay. The relay is connected to pin 7. The Serial.begin(38400) starts the serial communication at 38400 baud rate. The loop function checks for incoming data and turns the relay on or off based on the received command (‘0’ or ‘1’).

Developing a Simple App with MIT App Inventor

Step-by-Step Guide to Creating the App

1- Register and Log In:
Go to MIT App Inventor.
Log in with your Google account.

2- Create a New Project:
Click on “Start New Project” and name it “BluetoothControl”.

3- Design the User Interface:
Add a Label and set the text to “Bluetooth Control”.
Add a ListPicker and set the text to “Select Bluetooth Device”. Name it “ListPicker_Bluetooth”.
Add two Buttons:
Name: “Button_ON”, Text: “Turn ON”.
Name: “Button_OFF”, Text: “Turn OFF”.
Add a BluetoothClient component from the Connectivity palette (non-visible component).

4- Program the Blocks:
Go to the Block Editor.
Add the following blocks:

when ListPicker1.BeforePicking
  set ListPicker1.Elements to BluetoothClient1.AddressesAndNames

when ListPicker1.AfterPicking
  if call BluetoothClient1.Connect address ListPicker1.Selection
    then set Label1.Text to "Connected"
    else set Label1.Text to "Not Connected"

when Button1.Click
  if BluetoothClient1.IsConnected
    call BluetoothClient1.SendText text "1"

when Button2.Click
  if BluetoothClient1.IsConnected
    call BluetoothClient1.SendText text "0"
    
Arduino Bluetooth Control App Interface

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

1- Pair Bluetooth Device:
Turn on Bluetooth on your smartphone.
Pair with the HC-05/HC-06 module (default password is 1234).

2- Connect via App:
Use the app to connect to the Bluetooth module.
Use the “Turn ON” and “Turn OFF” buttons to control the lamp via the relay.

Conclusion

Using the HC-05/HC-06 Bluetooth module with an Arduino opens up numerous possibilities for wireless control in your projects. Whether you’re controlling an LED or a 230V lamp, Bluetooth provides a simple and effective way to add wireless communication to your Arduino projects. This comprehensive tutorial has shown you how to set up both simple and more complex systems using Bluetooth, Arduino, and MIT App Inventor.

By following the steps outlined in this tutorial, you can expand your knowledge and skills in Arduino and Bluetooth communication. Feel free to ask any questions in the comments section below and check out my other Arduino projects for more inspiration.

omartronics

Welcome to OmArTronics, the hub for technology enthusiasts and creative minds! I'm Omar, the founder of this website and YouTube channel, and a passionate engineer with a background in electrical and mechanical engineering. I'm currently pursuing my master's in mechatronics in Germany.

Leave a Reply