ESP32 Bluetooth Classic with Arduino IDE – Getting Started

The ESP32 comes with Wi-Fi, Bluetooth Low Energy and Bluetooth Classic. In this tutorial, you’ll learn how to use ESP32 Bluetooth Classic with Arduino IDE to exchange data between an ESP32 and an Android smartphone.

ESP32 Bluetooth Classic and Android Smartphone BT

We’ll control an ESP32 output, and send sensor readings to an Android smartphone using Bluetooth Classic.

Note: this project is only compatible with Android smartphones.

Watch the Video Tutorial

You can watch the video tutorial or keep reading this page for the written instructions.

Bluetooth Classic with ESP32

At the moment, using Bluetooth Classic is much more simpler than Bluetooth Low Energy. If you’ve already programmed an Arduino with a Bluetooth module like the HC-06, this is very similar. It uses the standard serial protocol and functions.

hc-05 bluetooth module arduino ESP32

In this tutorial, we’ll start by using an example that comes with the Arduino IDE. Then, we’ll build a simple project to exchange data between the ESP32 and your Android smartphone.

Parts Required

To follow this tutorial, you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Bluetooth Terminal Application

To proceed with this tutorial, you need a Bluetooth Terminal application installed in your smartphone.

We recommend using the Android app “Serial Bluetooth Terminal” available in the Play Store.

bluetooth serial application esp32

Serial to Serial Bluetooth

We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:

Open your Arduino IDE, and go to File > Examples > BluetoothSerial > SerialtoSerialBT.

The following code should load.

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#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);
}

View raw code

How the Code Works

This code establishes a two-way serial Bluetooth communication between two devices.

The code starts by including the BluetoothSerial library.

#include "BluetoothSerial.h"

The next three lines check if Bluetooth is properly enabled.

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

Then, create an instance of BluetoothSerial called SerialBT:

BluetoothSerial SerialBT;

setup()

In the setup() initialize a serial communication 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

loop()

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

In the first if statement, we check if there are bytes being received in the serial port. If there are, send that information via Bluetooth to the connected device.

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 next if statement, checks if there are bytes available to read in the Bluetooth Serial port. If there are, we’ll write those bytes in the Serial Monitor.

if (SerialBT.available()) {
  Serial.write(SerialBT.read());
}

It will be easier to understand exactly how this sketch works in the demonstration.

Testing the Code

Upload the previous code to the ESP32. Make sure you have the right board and COM port selected.

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 Enable button.

After a few seconds, you should get a message saying: “The device started, now you can pair it with bluetooth!”.

ESP32 Bluetooth Classic and Android Smartphone Demonstration

Go to your smartphone and open the “Serial Bluetooth Terminal” app. Make sure you’ve enable your smartphone’s Bluetooth.

To connect to the ESP32 for the first time, you need to pair a new device.
Go to Devices.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal

Click the settings icon, and select Pair new device. You should get a list with the available Bluetooth devices, including the ESP32test. Pair with the ESP32test.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Pair new device

Then, go back to the Serial Bluetooth Terminal. Click the icon at the top to connect to the ESP32. You should get a “Connected” message.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal connected

After that, type something in the Serial Bluetooth Terminal app. For example, “Hello”.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration

You should instantly receive that message in the Arduino IDE Serial Monitor.

ESP32 Bluetooth Classic demonstration serial monitor Arduino IDE

You can also exchange data between your Serial Monitor and your smartphone. Type something in the Serial Monitor top bar and press the “Send” button.

ESP32 Bluetooth Classic demonstration serial monitor Arduino IDE

You should instantly receive that message in the Serial Bluetooth Terminal App.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration

Exchange Data using Bluetooth Serial

Now that you know how to exchange data using Bluetooth Serial, you can modify the previous sketch to make something useful. For example, control the ESP32 outputs when you receive a certain message, or send data to your smartphone like sensor readings.

The project we’ll build sends temperature readings every 10 seconds to your smartphone. We’ll be using the DS18B20 temperature sensor.

ds18b20 temperature sensor

Through the Android app, we’ll send messages to control an ESP32 output. When the ESP32 receives the led_on message, we’ll turn the GPIO on, when it receives the led_off message, we’ll turn the GPIO off.

Schematic

Before proceeding with this project, assemble the circuit by following the next schematic diagram.

Connect an LED to GPIO25, and connect the DS18B20 data pin to GPIO32.

Bluetooth Classic ESP32 communication

Recommended reading: ESP32 Pinout Reference: Which GPIO pins should you use?

Code

To work with the DS18B20 temperature sensor, you need to install the One Wire library by Paul Stoffregen and the Dallas Temperature library. Follow the next instructions to install these libraries, if you haven’t already.

One Wire library

  1. Click here to download the One Wire library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get OneWire-master folder
  3. Rename your folder from OneWire-master to OneWire
  4. Move the OneWire folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Dallas Temperature library

  1. Click here to download the Dallas Temperature library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get Arduino-Temperature-Control-Library-master folder
  3. Rename your folder from Arduino-Temperature-Control-Library-master to DallasTemperature
  4. Move the DallasTemperaturefolder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

After assembling the circuit and installing the necessary libraries, copy the following sketch to your Arduino IDE.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com
*********/

// Load libraries
#include "BluetoothSerial.h"
#include <OneWire.h>
#include <DallasTemperature.h>

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

// Bluetooth Serial object
BluetoothSerial SerialBT;

// GPIO where LED is connected to
const int ledPin =  25;

// GPIO where the DS18B20 is connected to
const int oneWireBus = 32;          
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

// Handle received and sent messages
String message = "";
char incomingChar;
String temperatureString = "";

// Timer: auxiliar variables
unsigned long previousMillis = 0;    // Stores last time temperature was published
const long interval = 10000;         // interval at which to publish sensor readings

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

void loop() {
  unsigned long currentMillis = millis();
  // Send temperature readings
  if (currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;
    sensors.requestTemperatures(); 
    temperatureString = String(sensors.getTempCByIndex(0)) + "C  " +  String(sensors.getTempFByIndex(0)) + "F";
    SerialBT.println(temperatureString); 
  }
  // Read received messages (LED control command)
  if (SerialBT.available()){
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    Serial.write(incomingChar);  
  }
  // Check received message and control output accordingly
  if (message =="led_on"){
    digitalWrite(ledPin, HIGH);
  }
  else if (message =="led_off"){
    digitalWrite(ledPin, LOW);
  }
  delay(20);
}

View raw code

How the Code Works

Let’s take a quick look at the code and see how it works.

Start by including the necessary libraries. The BluetoothSerial library for Bluetooth, and the OneWire and DallasTemperature for the DS18B20 temperature sensor.

#include "BluetoothSerial.h"
#include <OneWire.h>
#include <DallasTemperature.h>

Create a BluetoothSerial instance called SerialBT.

BluetoothSerial SerialBT;

Create a variable called ledPin to hold the GPIO you want to control. In this case, GPIO25 has an LED connected.

const int ledPin =  25;

Define the DS18B20 sensor pin and create objects to make it work. The temperature sensor is connected to GPIO32.

// GPIO where the DS18B20 is connected to
const int oneWireBus = 32;          
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

Create an empty string called message to store the received messages.

String message = "";

Create a char variable called incomingChar to save the characters coming via Bluetooth Serial.

char incomingChar;

The temperatureString variable holds the temperature readings to be sent via Bluetooth.

String temperatureString = "";

Create auxiliar timer variables to send readings every 10 seconds.

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 10000;        // interval at which to publish sensor readings

setup()

In the setup(), set the ledPin as an output.

pinMode(ledPin, OUTPUT);

Initialize the ESP32 as a bluetooth device with the “ESP32” name.

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

loop()

In the loop(), send the temperature readings, read the received messages and execute actions accordingly.

The following snippet of code, checks if 10 seconds have passed since the last reading. If it’s time to send a new reading, we get the latest temperature and save it in Celsius and Fahrenheit in the temperatureString variable.

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    sensors.requestTemperatures(); 
    temperatureString = " " + String(sensors.getTempCByIndex(0)) + "C  " +  String(sensors.getTempFByIndex(0)) + "F";

Then, to send the temperatureString via bluetooth, use SerialBT.println().

SerialBT.println(temperatureString);  

The next if statement reads incoming messages. When you receive messages via serial, you receive a character at a time. You know that the message ended, when you receive \n.

So, we check if there’s data available in the Bluetooth serial port.

if (SerialBT.available()) {

If there is, we’ll save the characters in the incomingChar variable.

char incomingChar = SerialBT.read();

If the incomingChar is different than \n, we’ll concatenate that char character to our message.

if (incomingChar!= '\n'){
  message += String(incomingChar);
}

When we’re finished reading the characters, we clear the message variable. Otherwise all received messages would be appended to each other.

message = "";

After that, we have two if statements to check the content of the message. If the message is led_on, the LED turns on.

if (message =="led_on"){
   digitalWrite(ledPin, HIGH);
}

If the message is led_off, the LED turns off.

else if (message =="led_off"){
  digitalWrite(ledPin, LOW);
}

Testing the Project

Upload the previous sketch to your ESP32 board. Then, open the Serial Monitor, and press the ESP32 Enable button. When you receive the following message, you can go to your smartphone and connect with the ESP32.

Testing ESP32 Bluetooth Classic Project Code Example

Then, you can write the“led_on” and “led_off” messages to control the LED.

Testing ESP32 Bluetooth Classic Project Code Example

The application has several buttons in which you can save default messages. For example, you can associate M1 with the “led_on” message, and M2 with the “led_off” message.

Now, you are able to control the ESP32 GPIOs.

At the same time, you should be receiving the temperature readings every 10 seconds.

Wrapping Up

In summary, the ESP32 supports BLE and Bluetooth Classic. Using Bluetooth Classic is as simple as using serial communication and its functions.

If you want to learn how to use BLE with the ESP32, you can read our guide:

We hope you’ve found this tutorial useful. For more projects with the ESP32 you can check our project’s compilation: 20+ ESP32 Projects and Tutorials.

This tutorial is a preview of the “Learn ESP32 with Arduino IDE” course. If you like this project, make sure you take a look at the ESP32 course page where we cover this and a lot more topics with the ESP32.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

73 thoughts on “ESP32 Bluetooth Classic with Arduino IDE – Getting Started”

  1. Wonderful tutorial! Also, I recently bought your book about MicroPython programming that was great. Hopefully, we will be able to program with BLE in MicroPython soon.

    Reply
    • Hi David.
      Yes, we’ll create a tutorial with MicroPython and BLE as soon as it is fully implemented in the ESP32 micropython firmware.
      Thanks 🙂

      Reply
  2. Hi! very interesting project, however I cannot find the ESP32test Bluetooth device. The sketch runs on my ESP32 without problems, but the Bluetooth scanner does not find the ESP. What could be the reason for that?

    Reply
  3. Interesting work
    This is very helpful when we want to implement IoT devices or Even WSN using ESP8266 or better ESP32 modules.
    thanks for the tutorials and examples.

    Reply
  4. Hi, Thanks for the tutorial. I want to add Bluetooth to an existing sketch that uses 56% of memory on my ESP32. When I add the Bluetooth code the size goes to 102% !
    Is this normal?

    Reply
  5. hi, I tried just like you said and it almost works. I can send commands from my phone and see them in the serial monitor but when I send a line in the serial monitor it doesn’t appear in my pone
    also on my cell appears some lines with numbers occasionally (-127.00C -196.60F)
    what can be the problem?

    Reply
  6. Sir? about the code, i want to do the temperature sensor only without LED.
    if (message ==”led_on”){
    digitalWrite(ledPin, HIGH);
    }
    else if (message ==”led_off”){
    digitalWrite(ledPin, LOW);

    }
    if I erased this code for LED?, temperature sensor will still work? thank you sir

    Reply
  7. Hey!

    Thank you for this great tutorial. As a hobbyst it was very difficult for me to pair a simple arduino with HC bluetooth modules. After this article i quickly switched to the ESP32 and finally i could make my BLE security door lock with SPI lcd at my office.

    But i have a question: How could i setup a pairing password for the ESP32? 🙂

    Anyway, this tutorial is a lifesaver! 🙂

    Reply
  8. Instead of a smart phone I want to be able to use another esp32 with a touch screen so they converse with each other both ways.
    Is that possible?

    Reply
  9. Whoaaw ! thanks for the tutorial, Ms. Sara Santos. I am very interested in that ESP32 program. but, i have something to ask. If you change how it works, how do I get the ESP32 to scan classic bluetooth? thank you

    Reply
  10. Hi,

    Thanks a lot for the tutorial, it’s very easy to understand.
    But I have a question.
    If I send wroing messages, for example, ‘bread’ or ‘milk’, not ‘led_on’ nor ‘led_off’, I hope ESP32 can respond ‘Wrong code’, I don’t know how to edit the code. Can you help me?

    Best regards

    Reply
    • Hi.
      You can add an else statement after this:
      if (message ==”led_on”){
      digitalWrite(ledPin, HIGH);
      }
      else if (message ==”led_off”){
      digitalWrite(ledPin, LOW);
      }

      for example:

      else{
      SerialBT.print(“Invalid message”);
      }

      If the message is not “led_on” or “led_off”, it means it is an invalid message.

      I hope this helps.

      Regards,
      Sara

      Reply
  11. Hi Sara, Rui, Your tutorials are great thanks. I just purchased ESP 32 book
    How can I use esp32 to audio stream to another esp32 through BLE using my own audio signal?
    Regards
    Kazem

    Reply
  12. In this line ” SerialBT.write(Serial.read()); ”
    I want to put LoRa module data which is obtained using following command
    ” String LoRaData = LoRa.readString(); ”
    In case i write it this it gives compilation error SerialBT.write(LoRaData);

    Reply
  13. I am looking for an example program that shows how to pair 2 devices. I can connect an ESP32 device to the “Serial Bluetpoth Terminal” application on my phone. But I can’t connect 2 ESP32s together!

    Reply
  14. I am trying to pair 2 devices. I can connect an ESP32 device to the “Serial Bluetooth Terminal” application on my phone. But I can’t connect ESP32 with HC-05 module. I follow the example codes but do not work. ESP32 always find a device to pair, although the remote device is turn off. is there any suggestion??

    Reply
  15. Hi,

    Managed to get this project working. However, when I disconnect or when the bluetooth signal is too weak and disconnects, I cannot seem to reconnect the ESP32 again using bluetooth. Can you please advise what might be the problem?

    I can do it with a HC06 no problem. Many thanks.

    Reply
  16. I notice you list an Android phone in the hardware requirements for this tutorial, and I have successfully managed to transfer text between my phone and an ESP32.
    Is there an app for an iPhone that will talk serial Bluetooth classic in a similar fashion to Serial Bluetooth Terminal? I tried Blue Term, but no joy with that.

    Reply
  17. The sketch work fine but i was surprised to see it uses 71% of recourses, i tried to use it but program went over the limit.

    71% of flash memory wow i was using ESP32 WROOM.

    something is rong somewhere…

    Reply
  18. Hey can i pair my hc05 and esp32 such that hc05 acts as a master and esp32 acts as a slave.If yes could u help me out with the code

    Reply
  19. Hi, i am able to connect mobile to BT module and able to send & receive data.if i want to send more than 250 bytes(i need buffer size 1K) unable to receive the data.can you please help me how to increase the buffer size in libraries for working.

    Rajesh.M

    Reply
  20. Now I want toI have 2 bluetooth devices .
    Device 1: Arduino with HC05 bluetooth module along with a lm35 temp sensor.
    Device 2: ESP32
    Now I want to receive data in ESP 32 which is sended by the hc05 via Bluetooth
    I need help that how to pair esp32 bt and hc05

    Reply
  21. Hi, in all the esp32 Bluetooth applications I’ve seen on the net, you always connect to the device from inside an app, and not the main Bluetooth functionality availible in the phone bluetooth settings. I tried to connect directly to the phone via the bluetooth settings section but even though the pairing was successfully established, connection was not. Would you happen to have any idea why this is? Is a third party app mandatory? I’ve been stuck for days now and would love some support. Thank you so much for your time.

    Reply
  22. hello sara, i have tried your tutorial on esp32 and AMR_voice application, when i send voice message ‘ hello ‘ my serial monitor will show ‘*hello#’, can you give me reference to remove * and #

    sorry if my english is bad and my question is stupid 🙁
    regards,

    Reply
  23. Yes the tutorial works well Thanks. Nice way to control projects using a nearby phone.

    I was hoping to connect a Bluetooth keyboard to an ESP32 using BT classic. I dont see any sample projects and it seems way more difficult than it should.

    Reply
  24. HI,
    I got compiling error of:
    #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
    how to fix this please?
    Thanks
    Summer

    Reply
  25. I have been looking all afternoon for library with the BluetoothSerial.h file. It won’t compile in Arduino or VSCode.

    Do you know where I can find it?

    Reply
    • Hi.
      It should be automatically installed by default.
      Make sure you have an ESP32 selected in Tools > Board.
      Regards,
      Sara

      Reply
  26. Hi, I enjoyed this tutorial and wondered if it is possible to see continuous Arduino analog data from, for example, a potentiometer on Android using bluetooth with ESP32. In other words, can the Android be used as an Arduino serial plotter?

    Reply
  27. I enjoyed your tutorial on ESP32 with BT. However, I would like to see a tutorial extending these concepts to continuous analog recording with the ESP32 and BT. My experience is that with continuous analog recording, the BT is overwhelmed, resulting in hanging of the recording every few seconds. I don’t know if that reflects a problem with my programming or a limitation of the hardware or software.

    Reply
    • There is indeed a hardware limitation of how frequently you can send data using bluetooth classic. I’ve found that the maximum frequency is about once every 1.25-1.5 seconds without overwhelming the bluetooth. I don’t think anything can be done about that-in the arduino/esp32 realm at least. What you can do is read 5 or 10 values, and send them all together in one packet. Then you’ll have to send data 5 or 10 times less frequently, giving the bluetooth plenty of time to do its job.

      Reply
      • Thanks so much for your reply. It makes me feel a bit better about my failure to obtain a continuous recording–without hanging–via bluetooth using the ESP32 and the AD8232 breakout board. It seems unlikely that any other Arduino board is free of this problem.

        Reply
  28. I enjoyed your tutorial on ESP32 with BT. BUT Is there a way to use bluetooth and wifi at the same time on the ESP32

    Reply
  29. Hello, Im able to use the bluetooth serial app to turn the on board led on and off. Unfortunantly I cant read any data on my PCs serial monitor. I have tried several bluetooth serial examples. They all load and connect to my android app but the serial monitor does not work. When I first blug it in the serial monitor diplays some information EXCEPT for the Bluetooth ready to pair message that seems to be in most of the examples. I have a esp 32 dev kitv1. Any help would be appreciated. Lastly, can you recommend a good esp32 board?
    Thanks for all the great lessons

    Reply
  30. I uploaded the code to the ESP32 and it all was successful. I then tried to connect using the recommended Serial Bluetooth Terminal but I don’t see the ESP32test. It doesn’t show in the list. I am using Pixel 8 pro with Android 14. Any idea why it doesn’t show up?

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.