### Project 16: Bluetooth Remote Control

#### **(1)Description:**
In the last several decades, Bluetooth has become the most popular wireless communication module for it is easy to use and has found wide applications in most devices powered by batteries.
In order to adjust with the time and reality and need the needs of customers, Bluetooth has been upgraded several times. In recent years, it embraces lots of transformations in terms of data transfer rate, power consumption of wearable devices and IoT devices, and security systems and others. Here, we plan to learn about DX-BT24 with Arduino board.
#### **(2)Parameter:**
- Bluetooth Protocol: Bluetooth Specification V5.1 BLE
- Serial port sending and receiving without byte limit
- Communication distance: 40m (open environment)
- Operating frequency: 2.4GHz ISM band
- Modulation method: GFSK (Gaussian Frequency Shift Keying)
- Security Features: Authentication and Encryption
- Support Services: Central and Peripheral UUIDs FFE0, FFE1, FFE2
- Power consumption: automatic sleep mode, standby current 400uA\~800uA, 8.5mA during transmission.
- Power supply: 5V
- Operating temperature: –10 to +65 degrees Celsius
#### **(3)Connection Diagram:**
1.STATE is the status test pin connected to the internal light-emitting diode and usually remains unconnected.
2.RXD is the serial port interface for receiving terminal.
3.TXD is the serial port interface for sending terminal.
4.GND is for ground.
5.VCC is the positive pole.
6.EN/BRK: the disconnection of it represents the disconnection of the Bluetooth and it usually remains unconnected.
(Note: here the Bluetooth is directly linked with the V2 shield and **please pay attention to the direction**)

#### **(4)Download and install APP:**
##### **For IOS system**
1\. Open App Store.
2\. Search KeyesRobot in the Apple Store and click download.

3\. After the app is installed, you will see the following icon on your phone desktop.

**How to connect iOS Phone to Bluetooth module:**
1\. Turn on the Bluetooth and location services on phone through settings.

2\. Allow KeyesRobot APP to access Bluetooth through settings.

3\. Click to open KeyesRobot App.

4\. KeyesRobot App is a universal APP, which is applied to multiple keyestudio robots. If the interface does not display "TANK ROBOT", you can click the left and right buttons to find "TANK ROBOT".
5\. Click the Bluetooth button  in the upper right corner to scan the bluetooth

6\. You will see a Bluetooth named **BT24**, click the Connect button.

7\. If the onboard LED on the Bluetooth module stops flashing and stays on, it means your phone is successfully connected to the Bluetooth module.

##### **For Android System**
1\. Search **KeyesRobot** in Google Play, or open the following link to download and install the app.
[https://play.google.com/store/apps/details?id=com.keyestudio.keyestudio](https://play.google.com/store/apps/details?id=com.keyestudio.keyestudio)

2\. Turn on the Bluetooth and the location services of the mobile phone

3\. Find the KeyesRobot Bluetooth app from settings, click on the permission options of the app, and
enable Location and nearby device permissions.(Note: Some mobile phones do not have nearby device permissions function.)

4\. Click to open KeyesRobot App.

5\. KeyesRobot App is a universal APP, which is applied to multiple keyestudio robots. If the interface does not display "TANK ROBOT", you can click the left and right buttons to find "TANK ROBOT".
6\. Click the Bluetooth button  in the upper right corner to scan the bluetooth

7\. You will see a Bluetooth named **BT24**, click the Connect button.

8\. When your phone is successfully connected to the Bluetooth module, the onboard LED on the Bluetooth module will stop flashing and stay on.


#### **(5)Test the Bluetooth APP:**
(**Note:** Do not connect the Bluetooth module before uploading the code, because uploading the code also uses serial communication, and there may be conflicts with the Bluetooth serial communication, which can cause the upload to fail.)
```C
/*
Keyestudio Mini Tank Robot V3 (Popular Edition)
lesson 16.1
Bluetooth
http://www.keyestudio.com
*/
char ble_val; //Character variable(used to store the value received by Bluetooth)
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) //Determine whether there is data in the serial port buffer
{
ble_val = Serial.read(); //Read the data in the serial port buffer
Serial.println(ble_val); //Print out
}
}
```
Upload the code to the development board, then plug in the Bluetooth module, and then connected the mobile phone to the Bluetooth module.
After the mobile phone is successfully connected to the Bluetooth module, click to open the Bluetooth APP and click the Select button on the homepage.

The main interface of the Bluetooth app is shown in the figure below.

After the code above is successfully uploaded, open the serial monitor of the arduino IDE and set the baud rate to 9600. Click the icon on the APP interface and the serial monitor will display command sent by button.

**Note: The APP connection method is the same as below.**
#### **(6)Code Explanation:**
**Serial.available()** represents the number of characters currently remaining in the serial port buffer.
This function is generally used to determine whether there is data in this area. When Serial.available()\>0, it means that the serial port has received data and can be read.
**Serial.read()** refers to taking out and reading a Byte of data from the serial port buffer. For example, if a device sends data to the Arduino through the serial port, we can use Serial.read() to read the sent data.
#### **(7)Expansion Project:**
Here we use the command sent by the mobile phone to turn on or off an LED light. Looking at the wiring diagram, an LED is connected to the D9 pin.

**Test Code**
(Note: Do not connect the Bluetooth module before uploading the code, because the uploading of the code also uses serial communication, and there may be conflicts with the serial communication of the Bluetooth, which can cause the uploading of the code to fail.)
```C
/*
Keyestudio Mini Tank Robot V3 (Popular Edition)
lesson 16.2
Bluetooth
http://www.keyestudio.com
*/
int LED = 9;
char ble_val; //Integer variable used to store the value received by Bluetooth
void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop()
{
if (Serial.available() > 0) //Determine whether there is data in the serial port buffer
{
ble_val = Serial.read(); //Read data from serial port buffer
Serial.print("DATA RECEIVED:");
Serial.println(ble_val);
if (ble_val == 'a')
{
digitalWrite(LED, HIGH);
Serial.println("led on");
}
if (ble_val == 'b')
{
digitalWrite(LED, LOW);
Serial.println("led off");
}
}
}
```

After the code above is successfully uploaded, open the serial monitor of the arduino IDE and set the baud rate to 9600. Click  to control the LED. When clicking it, a character a will be sent, then LED will be on. If this button is pressed again, the LED will be off.


You need to remove the BT module if you finish projects.