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)
  • (optional) CP2102 USB-to-serial adapter
  • Wires
  • Breadboard
              Bluetooth Slave only module
HC-06
Slave Only
Bluetooth Master/Slave module
HC-05
Master/Slave
CP2102 USB-to-serial adapter
USB-to-serial adapter


Configuring the Bluetooth module

In this section you will learn how to configure either the HC-05 or HC-06 Bluetooth module.
1 Making the connections

Bluetooth pins Arduino pins Description
GND GND Ground
VCC 3.3V or 5V Power
TX 2 Transmit
RX 3 Receive
2 Create a new program in your Arduino IDE and copy the code on the right.
// This Sketch allows you to communicate and change the settings
// of the HC-05/06 Bluetooth module using the Arduino monitor
//
// Connect Bluetooth TX to Arduino pin 2
// Connect Bluetooth RX to Arduino pin 3
//
// Monitor settings should be No line ending and 9600 baud
// AT commands to type in the monitor  Response
// AT                                  OK
// AT+VERSION                          linvorV1.5
// AT+BAUD4            for 9600        OK9600
// AT+BAUD6            for 38400       OK38400
// AT+BAUD8            for 115200      OK115200
// AT+NAMEEH-BT2                       OKsetname
// AT+PIN1234                          OKsetpin
//
// Click on the Send button to send the command to the Bluetooth
// The response will print in the monitor
// 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
  // default baud is 9600
  // max baud is 38400. Anything higher will get garbage
  Serial.begin(9600);    // baud rate for the monitor
  BTSerial.begin(9600);  // baud rate for the Bluetooth

  Serial.println("Hello");  
  // 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 is 9600.
  • The max baud rate you can use is 38400. Anything higher will get garbage.
Serial monitor
The following is a summary of some of the available AT commands.
AT commands to type in the monitor Response Comment
AT OK
AT+VERSION linvorV1.5 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 pin to 1234
5 Issue the following AT commands. After typing in an AT 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 to type in the monitor Response Comment
AT OK
AT+VERSION linvorV1.5 Get version number
AT+NAMERobotsForFun-BT OKsetname Change Bluetooth module name to RobotsForFun-BT. Max 20 characters.

Do not change the baud rate.
Do not change the passcode pin.

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 Create a new program in your Arduino IDE and copy the code on the right.
// 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");
    }
  }
}
6 Upload the program to the Arduino.
7 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.
8 Make sure that the Bluetooth on your Windows PC is turned on.
9 Open up the Control Panel in Windows.
  • Select View Devices and Printers
  • If your Bluetooth module appears in the list of installed devices, then skip the following and go to step 12. Otherwise go to step 10.
Bluetooth device
10 In the Devices and Printers window
  • Select Add a Device
  • Select your Bluetooth module RobotsForFun-BT when it shows up
  • Click Next
  • Adding Bluetooth device
    11 When it asks for the passcode pin, enter 1234 for the default passcode if you have not changed it to something else in step 5. Bluetooth passcode
    12 Right-click on your Bluetooth device icon and select Properties. Under the Hardware tab, note the COM port number that the device is connected to. You'll need this COM port number in the next step. Bluetooth com port
    13 Download and run PuTTY.

    In the configuration screen,
    • Select Serial
    • Type in the correct COM port number (obtained from step 12)
    • Leave the speed at the default 9600. The speed must match the baud rate that you configured your Bluetooth module to (from step 5), and also must match the speed in the BTSerial.begin(9600) line in your Sketch (from step 6).
    • 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
    14 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