0
0
Arduinoprogramming~10 mins

Bluetooth with HC-05/HC-06 module in Arduino

Choose your learning style9 modes available
Introduction

Bluetooth modules like HC-05 and HC-06 let your Arduino talk wirelessly to other devices. This helps you send and receive data without wires.

You want to control an Arduino robot from your phone without cables.
You need to send sensor data from Arduino to a computer wirelessly.
You want to make a wireless remote control for a device.
You want to connect two Arduinos to share information without wires.
Syntax
Arduino
SoftwareSerial bluetooth(10, 11); // RX, TX pins

void setup() {
  Serial.begin(9600);          // Start serial monitor
  bluetooth.begin(9600);       // Start Bluetooth serial
}

void loop() {
  if (bluetooth.available()) {
    char data = bluetooth.read();
    Serial.print(data);        // Show data on serial monitor
  }
  if (Serial.available()) {
    char data = Serial.read();
    bluetooth.print(data);     // Send data to Bluetooth
  }
}

Use SoftwareSerial to create a serial connection on other pins because Arduino Uno has only one hardware serial port.

Make sure baud rates match between Arduino and Bluetooth module (usually 9600).

Examples
Set up Bluetooth on pins 2 and 3 with 9600 baud rate.
Arduino
SoftwareSerial bluetooth(2, 3); // RX=2, TX=3

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
}
Read one character from Bluetooth and print it to serial monitor.
Arduino
if (bluetooth.available()) {
  char c = bluetooth.read();
  Serial.print(c);
}
Send characters typed in serial monitor to Bluetooth device.
Arduino
if (Serial.available()) {
  char c = Serial.read();
  bluetooth.print(c);
}
Sample Program

This program reads data from the Bluetooth module and prints it on the serial monitor. It also sends anything typed in the serial monitor to the Bluetooth device.

Arduino
/*
  Simple Bluetooth communication with HC-05/HC-06
  Connect HC-05 RX to Arduino pin 10 (RX), TX to pin 11 (TX)
  Use SoftwareSerial to communicate
*/
#include <SoftwareSerial.h>

SoftwareSerial bluetooth(10, 11); // RX, TX

void setup() {
  Serial.begin(9600);          // Start serial monitor
  bluetooth.begin(9600);       // Start Bluetooth serial
  Serial.println("Bluetooth ready");
}

void loop() {
  if (bluetooth.available()) {
    char data = bluetooth.read();
    Serial.print("Received: ");
    Serial.println(data);
  }
  if (Serial.available()) {
    char data = Serial.read();
    bluetooth.print(data);
  }
}
OutputSuccess
Important Notes

HC-05 can be master or slave; HC-06 is slave only.

Make sure to connect HC-05 TX to Arduino RX pin and RX to Arduino TX pin (cross connection).

Use a voltage divider on HC-05 RX pin if Arduino uses 5V to avoid damage (HC-05 RX is 3.3V tolerant).

Summary

HC-05/HC-06 modules let Arduino communicate wirelessly using Bluetooth serial.

Use SoftwareSerial to connect Bluetooth module on different pins.

Match baud rates and connect pins correctly for communication.