Bluetooth

In this project, you'll learn how to connect and control a Bluetooth module.

Bluetooth is a wireless technology for two devices to communicate with each other over short distances. One device is designated as the master and the second device is the slave. In order for a master and a slave to communicate with each other, they must first be paired.

A Bluetooth device works like a serial modem. You can communicate with it either using the wireless Bluetooth signal or a wired serial connection. To configure the Bluetooth device, you must use the wired serial connection, and it must not be paired with another Bluetooth device.

Bluetooth icon
Parts needed:
  • Arduino
  • Bluetooth HC-06 slave only module (with 4 pins), or
  • Bluetooth HC-05 master/slave module (with 6 pins)
  • Wires
  • Breadboard
              Bluetooth Slave only module
HC-06
Slave Only
Bluetooth Master/Slave module
HC-05
Master/Slave

There is a slight difference between configuring the HC-06 and the HC-05 modules


Configuring the HC-06 (Slave Only) Bluetooth module

In this section you will learn how to configure the HC-06 (Slave Only) Bluetooth module.
1 Making the connections

Bluetooth pins Arduino pins Description
TX 2 (RX) Receive
RX 3 (TX) Transmit
VCC 5V Power
GND GND Ground
2 Create a new program in your Arduino IDE and copy the code on the right.
// This program allows you to communicate and configure
// the HC-06 (Slave Only) Bluetooth module using the 
// Arduino serial monitor
//
// Connections
// HC-06 (Slave Only)    Arduino
// TX                    2 (RX)
// RX                    3 (TX)
// VCC                   5V
// GND                   GND

// Serial Monitor settings should be No line ending and 9600 baud
// AT commands to type in the monitor      Response
//    for the HT-06 (Slave Only)
// AT                                      OK               it's working
// AT+VERSION                              linvorV1.5
// AT+BAUD4            for 9600            OK9600
// AT+BAUD6            for 38400           OK38400
// AT+BAUD8            for 115200          OK115200
// AT+NAMERobotsForFun-BT                  OKsetname        set the name
// AT+PIN1234                              OKsetpin         set the password
//
// commands are case sensitive

// Click on the Send button to send the command to the Bluetooth
// The response will print in the monitor

// c 2015 Enoch Hwang


#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX

void setup() {
  // If you do not get the initial OK, it is because the connection and/or the baud is wrong
  // If you see garbage, it is because the baud is wrong
  // The default baud rate for the HC-06 (Slave only) Bluetooth module is 9600
  Serial.begin(9600);     // baud rate for the Serial monitor
  BTSerial.begin(9600);   // // The default baud rate is 9600

  Serial.println("Enter AT commands:");  
  // if connection and baud are correct then should get OK response 
  BTSerial.write("AT");  
}

void loop() {
  if (BTSerial.available()){
    Serial.write(BTSerial.read());
  }
  if (Serial.available())
    BTSerial.write(Serial.read()); 
}
3 Upload and run the program.
4 Open the Serial Monitor. The monitor settings should be:
  • No line ending
  • 9600 baud
You should get an initial OK on the Serial Monitor.
  • If you do not get the initial OK, it is because the connection and/or the baud rate setting is wrong.
  • If you see garbage, it is because the baud rate is wrong.
  • The default baud rate for the HC-06 (Slave Only) module is 9600.
  • If you do not see the initial OK, or if you enter AT and it does not reply with OK then change the baud rate for the line BTSerial.begin(9600) to BTSerial.begin(38400). If it still does not work then change to another baud rate.
Serial monitor
5 The following is a summary of some of the available AT commands. The commands are CASE sensitive. After typing the command in the Serial Monitor, you need to click the Send button to send the command to the Bluetooth module. The response will be printed in the monitor.
AT commands Response Comment
AT OK
AT+VERSION linvorV1.8 Get version number
AT+BAUD4 OK9600 Set baud rate to 9600
AT+BAUD6 OK38400 Set baud rate to 38400
AT+BAUD8 OK115200 Set baud rate to 115200
AT+NAMERobotsForFun-BT OKsetname Change Bluetooth module name to RobotsForFun-BT. Max 20 characters.
AT+PIN1234 OKsetpin Change password to 1234


Configuring the HC-05 (Master/Slave) Bluetooth module

In this section you will learn how to configure the HC-05 (Master/Slave) Bluetooth module.

There are two versions of the HC-05:
+VERSION:4.0-20190815 with the gold check mark is the newer version
+VERSION:2.0-20100601 without the gold check mark is the older version.
The description below is for the newer version.
1 Making the connections

Bluetooth pins Arduino pins Description
TX 2 (RX) Receive
RX 3 (TX) Transmit
KEY/EN 5V HIGH = AT command mode
LOW (default) = data mode
VCC 5V Power
GND GND Ground

After powering up the module the status led should blink slowly (4 second intervals) telling you that it is now in AT command mode.

Note that there is an older version of the HC-05 (+VERSION:2.0-20100601). To bring this into AT command mode, you need to push down the button before you power up the module. The AT commands also work a bit different from what is listed below.
2 Create a new program in your Arduino IDE and copy the code on the right.
// This program allows you to communicate and configure
// the HC-05 (Master/Slave) Bluetooth module using the 
// Arduino serial monitor
//
// Connections
// HC-05 Master/Slave    Arduino
// TX                    2 (RX)
// RX                    3 (TX)
// KEY/EN                5V
// VCC                   5V
// GND                   GND

// Serial Monitor settings should be Both NL & CR and 9600 baud
// AT commands to type in the monitor      Response
//    for the HT-05 (Master/Slave)
// AT                                      OK
// AT+VERSION?                             +VERSION:4.0-20190815
// AT+UART?                                +UART:9600,0,0           default 9600, 1 stop bit, no parity
// AT+UART=9600,0,0                        OK                       set to 9600, 1 stop bit, no parity
// AT+NAME?                                +NAME:HC-05              get the name
// AT+NAME=RobotsForFun-BT                 +NAME:RobotsForFun-BT    set the name
// AT+PSWD?                                +PSWD:1234               get the password
// AT+PSWD=1234                            OK                       set the password
// invalid command                         ERROR:[0]
//
// commands are case sensitive

// Click on the Send button to send the command to the Bluetooth
// The response will print in the monitor
//
// c 2015 Enoch Hwang


#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX

void setup() {
  // If you do not get the initial OK, it is because the connection and/or the baud is wrong
  // If you see garbage, it is because the baud is wrong
  // The default baud rate for the HC-05 (Master/Slave) Bluetooth module is 38400
  Serial.begin(9600);     // baud rate for the Serial monitor
  BTSerial.begin(38400);  // The default baud rate is 38400

  Serial.println("Enter AT commands:");  
  // if connection and baud are correct then should get OK response
  BTSerial.write("AT");
}

void loop() {
  if (BTSerial.available()){
    Serial.write(BTSerial.read());
  }
  if (Serial.available())
    BTSerial.write(Serial.read()); 
}
3 Upload and run the program.
4 Open the Serial Monitor. The monitor settings should be:
  • Both NL & CR
  • 9600 baud
You should get an initial OK on the Serial Monitor.
  • If you do not get the initial OK, it is because the connection and/or the baud rate setting is wrong.
  • If you see garbage, it is because the baud rate is wrong.
  • The default baud rate for the HC-06 (Master/Slave) module is 38400 in command mode and 9600 in data mode.
  • The max baud rate you can use is 38400. Anything higher will get garbage.
  • If you do not see the initial OK, or if you enter AT and it does not reply with OK then change the baud rate for the line BTSerial.begin(38400) to BTSerial.begin(9600). If it still does not work then change to another baud rate.
Serial monitor
5 The following is a summary of some of the available AT commands. The commands are CASE sensitive. After typing the command in the Serial Monitor, you need to click the Send button to send the command to the Bluetooth module. The response will be printed in the monitor.
AT commands Response Comment
AT OK
AT+VERSION? +VERSION:4.0-20190815 Get version number (new version)
+VERSION:2.0-20100601 (old version)
AT+NAME? +NAME:HC-05 Get module name
AT+NAME=RobotsForFun-BT +NAME:RobotsForFun-BT Set module name to RobotsForFun-BT. Max 20 characters.
AT+UART? +UART:38400,0,0 Get baud
AT+UART=9600,0,0 OK Set baud to 9600, 1 stop bit, no parity
AT+PSWD? +PSWD:1234 Get password
AT+PSWD=1234 OK Set password to 1234
AT+ROLE? +ROLE:0 Get role. 0=slave (default); 1=master
AT+ROLE=1 OK Set role to master

Using Bluetooth to Control a LED

In this section you will learn how to control a LED wirelessly using the Bluetooth module.

We will connect the PC wirelessly using Bluetooth to the Bluetooth module that is connected to the Arduino. The PC Bluetooth is the master and the Arduino Bluetooth is the slave. On the PC side, we will run PuTTy, a terminal monitor program, to send commands wirelessly to the Arduino. The Arduino will execute the on/off commands received from the Bluetooth module to turn on or off the built-in LED.
6 Setup

If you are using the HC-06, just power it up.

If you are using the HC-05 (Master/Slave) then you need to first set its role to slave and then power it up into data mode (i.e. not AT command mode).
The PC or your mobile phone is always the master, so your HC-05 must be in the slave mode, otherwise it will not show up in the Bluetooth scan list.
7 Create a new program in your Arduino IDE and copy the code on the right.


Make sure that the baud rate specified in the BTSerial.begin(9600) line is correct.
// Control a LED wirelessly using Bluetooth

#include "SoftwareSerial.h"
SoftwareSerial BTSerial(2, 3); // RX, TX

int led = 13;
int val;
int onFlag;

void setup() {
  pinMode(led, OUTPUT);
  BTSerial.begin(9600);  // baud rate for the Bluetooth
  onFlag = -1;
  BTSerial.println("Press 1=on; 0=off");
}

void loop() {
  if(BTSerial.available()) {  // check if there's any incoming data on the Bluetooth
    val = BTSerial.read();  // if there is then read in the data

    if(onFlag == 0) {   // if LED is currently off and command is to turn it on
      if(val == '1') {
        digitalWrite(led, HIGH);
        BTSerial.println("LED on");
        onFlag = 1;
        BTSerial.println("Press 0 to turn off LED"); 
      }
    } else if(onFlag == 1) {  // if LED is currently on and command is to turn it off
      if(val == '0') {
        digitalWrite(led, LOW);
        BTSerial.println("LED off");
        onFlag = 0;
        BTSerial.println("Press 1 to turn on LED"); 
      }
    } else {
      onFlag = 0;
      BTSerial.println("Press 1=on; 0=off");
    }
  }
}
8 Upload the program to the Arduino.
9 You may want to power up your Bluetooth module and Arduino as a standalone, i.e. not using the USB cable connected to your PC but connected to a battery. This way, you will know for sure that the communication is through the wireless Bluetooth. The LED on the Bluetooth module should blink.
10 Connect the Bluetooth module to your Windows PC. The following instructions are for Windows 11. Click here for Windows 7.
10a Make sure that the Bluetooth on your Windows PC is turned on.
10b In the Search, type Bluetooth and select Bluetooth and other devices settings
10c If the Bluetooth module name is already listed in the Other Devices then you can go to step 10i.
10d Under Device settings | Bluetooth devices discovery, select Advanced in the dropdown. Bluetooth device setup
10e Click on Add Devices. Bluetooth device setup
10f In the Add a device window, click Bluetooth. Bluetooth device setup
10g Select the name of your Bluetooth module (RobotsForFun-BT) and type 1234 for the password. Bluetooth device setup
10h You should get the Connected message. Click Done. Bluetooth device setup
10i Back in the Devices window, click on More Bluetooth settings. Bluetooth device setup
10j In the Bluetooth Settings window, note down the COM port number for the Outgoing RobotsForFun-BT 'Dev B' line. This COM port number will be used in step 11. Bluetooth device setup
11 Download and run PuTTY.

In the configuration screen,
  • Select Serial
  • Type in the correct COM port number (obtained from step 10j)
  • Leave the speed at the default 9600. The speed must match the baud rate that you configured your Bluetooth module to (from steps 4 & 5), and also must match the speed in the BTSerial.begin(9600) line in your Sketch (from step 7).
  • Click Open to start the serial terminal.
When the connection is successfully made, the LED on the Bluetooth module will stop blinking and will stay on, and the PuTTy terminal will open.
Putty Configuration
12 In the PuTTY terminal, type in a 1 to turn on the LED on the Arduino, and type in a 0 to turn it off.

Observe the LED on the Arduino to see that it is responding correctly.
Putty