IoT/ESP32 B.Tech/Diploma Beginner

ESP32 Bluetooth Classic with Arduino IDE – Getting Started

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. We’ll control an ESP32 output, and send sensor readings.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

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.
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.
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.
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:
ESP32 DOIT DEVKIT V1 Board
(read
Best ESP32 development boards
)
Android Smartphone with Bluetooth
5mm LED
330 Ohm resistor
DS18B20 temperature sensor
4.7k Ohm resistor
Jumper wires
Breadboard
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.
Serial to Serial Bluetooth
We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:
Windows:
instructions – ESP32 Board in Arduino IDE
Mac and Linux:
instructions – ESP32 Board in Arduino IDE
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);
}
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!
”.
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.
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
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.
After that, type something in the Serial Bluetooth Terminal app. For example, “
Hello
”.
You should instantly receive that message in the Arduino IDE Serial Monitor.
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.
You should instantly receive that message in the Serial Bluetooth Terminal App.
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
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
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
Click here to download the One Wire library
. You should have a .zip folder in your Downloads
Unzip the
.zip
folder and you should get
OneWire-master
folder
Rename your folder from
OneWire-master
to
OneWire
Move the
OneWire
folder to your Arduino IDE installation
libraries
folder
Finally, re-open your Arduino IDE
Dallas Temperature library
Click here to download the Dallas Temperature library
. You should have a .zip folder in your Downloads
Unzip the
.zip
folder and you should get
Arduino-Temperature-Control-Library-master
folder
Rename your folder from
Arduino-Temperature-Control-Library-master
to
DallasTemperature
Move the
DallasTemperature
folder to your Arduino IDE installation
libraries
folder
Finally, re-open your Arduino IDE
After assembling the circuit and installing the necessary libraries, copy the following sketch to your Arduino IDE.

// 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 an

Technologies used

ESP32ArduinoBluetoothBLEDS18B20AndroidArduino IDE

Hardware components

  • ESP32
  • Arduino
  • DS18B20

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.