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
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

Sketch

#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

Mobile APPs

Arduino Bluetooth Tutorial Example Android App

Arduino Bluetooth Tutorial Example App
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
By selecting one of the devices, you'll see the device is NOW connected.
Arduino Bluetooth App FrontPage
By clicking Turn ON and Turn OFF, you will see the led can be light on and off accordingly.
LED ON
LED OFF
And, some serial message will be printed on the Monitor Serial dialog as:
Bluetooth LED Serial

Longer Vision Robot LVControl

Longer Vision Robot provides its own APP LVContrl, which can be downloaded from ...
The first page of LVContrl looks like:
LVControl_V0.2
By selecting page BTSet, we can easily SEARCH FOR VISIBLE DEVICES:
LVControl Bluetooth Set
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

Assignment:

Two other very similar reference examples can be found at:
, which are left for students as their assignments.