ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32

by Utsource in Circuits > Arduino

40699 Views, 11 Favorites, 0 Comments

ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32

20191001_091851.jpg
Hi guys Since the ESP32 Board comes with WiFi & Bluetooth both but for our mostly Projects we usually use only Wifi, we don't use Bluetooth.
So in this instructables i'll show how easy it is to use Bluetooth of ESP32 & For your basic Projects Bluetooth is more handy feature of ESP32 to use.

Things You Need

1568119828207.png

So for this project you only need :

ESP32 (ANY MODEL) :
And a cable to Program it.

Setting Up Arduino Ide for ESP 32

Screenshot_20190917-134004__01.jpg

Make sure you have Arduino IDE in your PC and you installed ESP32 Boards in your Arduino IDE, and if it is not the case please follow the following instructables of mine to install it. :


https://www.instructables.com/id/Getting-Started-W...

Get the Bluetooth App

Screenshot_20191001-092710.jpg
Before we go further make sure you have a Bluetooth serial application in your smartphone for BLUETOOTH communication with any BLUETOOTH device in our case ESP32.

Coding Part

images(66).jpg
Open you arduino ide.
go to File > Examples > BluetoothSerial > SerialtoSerialBT.

Or copy the following code :

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}




The code is very easy and very similar to BLUETOOTH code we generally use with arduino uno & hc05


Code explanation :

Below line includes the BluetoothSerial library.


#include "BluetoothSerial.h"

3 lines provided below enables the Bluetooth

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


Then, a instance of BLUETOOTH is created

BluetoothSerial SerialBT;


In the setup(), a serial communication is started at a baud rate of 115200.

Serial.begin(115200);

Initialize the Bluetooth serial device and pass as an argument the Bluetooth Device name. By default it’s called ESP32test but you can rename it and give it a unique name.

SerialBT.begin("ESP32test"); //Bluetooth device name


In the loop(), send and receive data via Bluetooth Serial.

In the below lines of code it will check if any data is available on serial monitor if yes then it will send the data to BLUETOOTH device (for eg : our smartphone ) using esp32's Bluetooth.

if (Serial.available()) {
SerialBT.write(Serial.read());
}
SerialBT.write() sends data using bluetooth serial.

Serial.read() returns the data received in the serial port.

The below part of code will check if any data from Bluetooth is available if it is then it will print it on serial monitor.

if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
So thats all the basic explanation of the code.

So now you can upload the code to your ESP32.

Testing the Bluetooth of Esp32

device_started_bluetooth-esp32.png
send-hello-bluetooth-esp32.png
hello-message-received-bluetooth.png
send-message-via-bluetooth-1.png
receive-message-bluetooth-application-esp32.png
After uploading of the code open serial monitor in your Arduino IDE & then connect the Bluetooth (esp32) from you smartphone.
And in devices section of app you can connect to ESP32 and then you will get message "connecting to ESP32".
And in few seconds it will connected and you will see message ESP32 connected.
Then if you type hello from app then in your serial monitor of Your Arduino IDE you can see Hello message & if you type How are you from your serial monitor you can see that message in Your App.
So this is how you can establish a Bluetooth connection with ESP32 and you can use a if condition to execute different actions for the different message you sent to esp32 from your phone.
So have fun using ESP32 Bluetooth in your projects.