ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32

Introduction: ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32

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.

Step 1: Things You Need

So for this project you only need :

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

Step 2: Setting Up Arduino Ide for ESP 32

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

Step 3: Get the Bluetooth App

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.

Step 4: Coding Part

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.

Step 5: Testing the Bluetooth of Esp32

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.

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    Discussions

    0
    francois.tipold
    francois.tipold

    Question 8 months ago on Step 5

    OK, ESP32 to Android, I get it but how to connect 2 ESP32?


    The first one get a value from a magnetometer and the second ESP32 read it and display it on a TFT Screen.
    It works with cables but the sensor will be a 5 meters from the ESP32.

    Can you help me?