Esp32

ESP32 Serial Communication over Bluetooth Using Arduino IDE

ESP32 is a microcontroller board that has dual support for WiFi and Bluetooth. It comes with dual Bluetooth classic and Bluetooth Low Energy. Using Bluetooth, we can transfer data from different sensors to ESP32 or can create a mesh network of ESP32 Bluetooth inside our project.

This tutorial covers how we can communicate serially using the Bluetooth of ESP32 with a PC. Here we will transmit a string to the Arduino IDE serial monitor while connecting the ESP32 to the PC not serially but using the Bluetooth connection.

Introduction to ESP32 Bluetooth Serial Communication Using Arduino IDE

Serial communication is important when it comes to microcontrollers as it allows devices to exchange data between them. ESP32 comes with UART and multiple other communication protocols that allows it to exchange data with a PC and if required print it on a serial monitor.

ESP32 features Bluetooth support which means we can configure its Bluetooth in such a way that ESP32 communicates serially, and we can print the received data over Bluetooth on the serial monitor.

For this we will be calling the SerialBT() function from the Arduino BluetoothSerial.h library.

Initializing ESP32 Bluetooth

Before we can communicate over Bluetooth, we have to initialize it first for that the steps will be helpful.

Step 1: Connect ESP32 board with PC using the Micro USB cable. After connecting check, the COM port at which the ESP32 board is connected. Open device manager and here we can see ESP32 is connected at COM3:

Step 2: Now open Arduino IDE and select the ESP32 board along with the same COM port:

Step 3: Now upload the given code to the ESP32 board:

This code started by initializing the Bluetooth Serial library by including the “BluetoothSerial.h”.

After that Bluetooth SSID/Name is initialized using the command SerialBT.begin().

In the end inside the loop part a string Hello World will constantly print over the serial monitor using the ESP32 Bluetooth connection.

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
  SerialBT.begin("ESP32 Bluetooth");
}
void loop() {
  SerialBT.println("Hello World");
  delay(1000);
}

 

Now we have successfully configured the ESP32 Bluetooth.

Note:  The BluetoothSerial.h is by default installed in Arduino IDE once we add the ESP32 board core. We don’t need to separately install this library. You can also try different examples related to BluetoothSerial.h library by going to:  to File>Examples>Bluetooth Serial

Before you can use this Library make sure the ESP32 board is added in Arduino IDE. Read the article Installing the ESP32 Board in Arduino IDE for more information on installation.

Visit the following Espressif Systems ESP32 GitHub repository to learn more about BluetoothSerial.h library: [GitHub Arduino-ESP32/BluetoothSerial].

Connecting ESP32 Bluetooth with PC

As ESP32 Bluetooth is configured follow the steps to connect ESP32 Bluetooth with PC.

Step 1: Open Bluetooth settings in Windows:

Step 2: Click Add a Bluetooth Device:

Step 3: Select Bluetooth device:

Step 4: Click ESP32 Bluetooth. Here you will see any name of ESP32 which you defined inside the Arduino code:

Step 5: Once the ESP32 Bluetooth is successfully connected below a message will appear:

Step 6: In device manager we can see COM port for ESP32 Bluetooth. Knowing this COM port is important as it will help us to receive the data over the serial monitor in the Arduino IDE:

Now we have successfully connected ESP32 with PC over Bluetooth communication.

Reading Serial Data over Bluetooth Communication

To read serial data over Bluetooth first we have to disconnect the ESP32 from the PC so it will not establish any UART serial communication. After disconnecting we can verify it from the Device Manager.

The image shows ESP32 is disconnected from the PC.

Note: Remember to power ESP32 from any other PC or using the power adapter. This will help to remain ESP32 Bluetooth turned on.

After disconnecting the ESP32 from the PC, open the Arduino IDE and select the COM port at which ESP32 Bluetooth is connected.

After selecting the right port, we can see the ESP32 is continuously transmitting the Hello World string over the serial monitor using Bluetooth communication.

We have successfully received data using the Serial Bluetooth of ESP32.

Conclusion

ESP32 is a versatile programming microcontroller board. It gives users multiple methods for communication like Bluetooth WiFi or using the UART, SPI or any other communication protocol. Here we have covered how ESP32 Bluetooth can be configured for serial communication. For a complete step by step process read the tutorial.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.