> For the complete documentation index, see [llms.txt](https://longer-vision-robot.gitbook.io/arduino-full-stack/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://longer-vision-robot.gitbook.io/arduino-full-stack/14_communication/03_bluetooth.md).

# 14.3 Bluetooth

A **ZS-040** (detected as **HC-0X**) bluetooth module is provided in our Arduino course on-site, which looks like:

![HC-05 Bluetooth Module](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJYVPEe-9lDZfB1N%2FHC-05-Bluetooth-Module.jpg?generation=1526059587744397\&alt=media)

The **ONLY** difference is that the provided bluetooth module has 4 pins but **NOT** 6 pins, without pin **STATE** and pin **EN**.

## Hardware Wiring

![Bluetooth](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJZn4_JZZia646M9%2F003_bluetooth.jpg?generation=1526059604588850\&alt=media)

## Sketch

The code can be found at [Examples\_Arduino - arduinocc - \_003\_Bluetooth - \_003\_Bluetooth.ino](https://github.com/LongerVisionRobot/Examples_Arduino/blob/master/howtomechatronics/_003_Bluetooth/_003_Bluetooth.ino) You can also refer to Adafruit's official website at <https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-05-bluetooth-module-tutorial/>.

```
#include <SoftwareSerial.h>
#define ledPin 7

SoftwareSerial mySerial(10, 11); // RX, TX
int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  //mySerial.println("Hello, world?");
}

void loop() {   
  if(mySerial.available() > 0){ // Checks whether data is coming from the serial port
    state = mySerial.read(); // Reads the data from the serial port
    Serial.print("mySerial available!\n");
 }

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

**Note: Please remove your bluetooth module before flashing your code onto Arduino board. Otherwise, you'll obtain the following ERROR messages:**

![AVR Bluetooth Error](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJgVqnKOrO-iFnn5%2Favr_bluetooth_error.jpg?generation=1526059588330858\&alt=media)

## Mobile APPs

### Arduino Bluetooth Tutorial Example Android App

This APP can be downloaded from <https://howtomechatronics.com/download/arduino-bluetooth-tutorial-example-android-app/?wpdmdl=2833>

![Arduino Bluetooth Tutorial Example App](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LDJKrwCgJEh9d6V-Nae%2F-LDJKzu2ZmDLFSyUVvbw%2FHowtoMechatronics_ArduinoBluetoothTutorialExampleApp.jpg?generation=1527202187632105\&alt=media)

After the installation, by click on **Connect** on the **FIRST** page of this APP, you'll see all detected bluetooth devices as:

![Arduino Bluetooth App Devices Found](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LDgmpIyC3HXY793Ljwq%2F-LCFEJlczWZoEb75PeB3%2FHowtoMechatronics_Arduino_Bluetooth_App_DevicesFound.jpg?generation=1527612402643848\&alt=media)

By selecting one of the devices, you'll see the device is NOW **connected**.

![Arduino Bluetooth App FrontPage](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LDgmpIyC3HXY793Ljwq%2F-LCFEJmq4jwCoFnzju6m%2FHowtoMechatronics_Arduino_Bluetooth_App_FrontPage.jpg?generation=1527612402715864\&alt=media)

By clicking **Turn ON** and **Turn OFF**, you will see the led can be light on and off accordingly.

![LED ON](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJoELGJjrM8mThtB%2Fled_on.jpg?generation=1526059587944276\&alt=media)

![LED OFF](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJpxQpzknfw38Ja4%2Fled_off.jpg?generation=1526059604909971\&alt=media)

And, some serial message will be printed on the **Monitor Serial** dialog as:

![Bluetooth LED Serial](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJsxcYwkDHFWGE7B%2Fbluetooth_led_serial.jpg?generation=1526059606312319\&alt=media)

### Longer Vision Robot LVControl

[Longer Vision Robot](http://www.longervisionrobot.com) provides its own APP **LVContrl**, which can be downloaded from ...

The first page of **LVContrl** looks like:

![LVControl\_V0.2](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LCFE4dCRQBMq1ajywfh%2F-LCFEJwRIzgi2IOpthXV%2FLVControl_V0.2.jpg?generation=1526059606176617\&alt=media)

By selecting page **BTSet**, we can easily **SEARCH FOR VISIBLE DEVICES**:

![LVControl Bluetooth Set](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LDgmpIyC3HXY793Ljwq%2F-LCFEJzN0SBwxEj0phc9%2FLVControl_Bluetooth_Set.jpg?generation=1527612402642975\&alt=media)

After simply pair the device by just clicking on the listed device name, you will be able to see that our bluetooth device is now listed in **LIST PAIRED DEVICES**.

![LVControl Bluetooth Connected](https://2341898987-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LCFDbcteyTR3tQSs-5x%2F-LDgmpIyC3HXY793Ljwq%2F-LCFEK0RtpvruJZ-Tc1b%2FLVControl_Bluetooth_Connected.jpg?generation=1527612402643028\&alt=media)

## Assignment:

Two other very similar reference examples can be found at:

* <https://create.arduino.cc/projecthub/user206876468/arduino-bluetooth-basic-tutorial-d8b737>
* <http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/>

， which are left for students as their assignments.
