A Basic Introduction to ESP32 Bluetooth
This article will teach us about the ESP32’s Bluetooth functionality. Both Bluetooth Low Energy (BLE) and Classic Bluetooth v4.2 are supported by the ESP32. In this ESP32 Bluetooth Tutorial, let’s concentrate on Classic Bluetooth. We will gain a basic understanding of the Bluetooth architecture in the ESP32, as well as how to configure, set up, and initiate Bluetooth communication. We will also work on a few easy projects that include data transfer between the ESP32 and a smartphone via Bluetooth communication.Â

ESP32 Bluetooth
A fantastic wireless communication tool, Bluetooth has gained popularity recently. Bluetooth is a short-range wireless communication system with a range of up to 100 m that operates in the unlicensed 2.4 GHz ISM (Industrial, Scientific, and Medical) frequency band.
The Bluetooth Link Controller (or Link Manager) and Baseband are both included in the silicon of the ESP32 SoC. Physically, all that is required for proper Bluetooth communication is an external antenna.

The Wi-Fi Radio and the Bluetooth Radio in the ESP32 utilize the same antenna because they both use the same 2.4 GHz ISM frequency. There is just one pin (LNA IN) for connecting to an antenna, according to the ESP32 SoC pinout.
Both Bluetooth Classic (Classic BT) and Bluetooth Low Energy (BLE), which may be enabled with BLUEDROID Bluetooth Stack, are supported by ESP32. UART, SPI, and VHCI (Virtual HCI) interfaces are three types of host controller interfaces (HCI) that are supported by ESP32 Bluetooth (only one can be used at a time and UART is the default).
Getting Started with ESP32 Classic Bluetooth
The Classic Bluetooth is the original point-to-point network topology created for one-to-one wireless communication between a master and a slave. It is also known as Bluetooth Base Rate or Enhanced Data Rate. One master can support several slave devices, but only one slave can be in active communication with the master at any given time. Our Bluetooth mice and keyboards are compatible with Classic Bluetooth. Another straightforward example of Bluetooth capabilities is file transmission between two devices (such as two mobile phones or a laptop and a mobile phone).
On the other hand, BLE, or Bluetooth Low Energy, was created with Internet of Things (IoT) applications in mind and is optimized for low-power operation. BLE technology was added to Bluetooth Specification 4.0, which is mostly utilized in battery-powered devices including watches, music equipment, fitness trackers, and data beacons.

For now, let’s develop another tutorial on the ESP32 BLE and concentrate on the ESP32 Classic Bluetooth.
The BLUEDROID Bluetooth Stack offers APIs for user applications while interacting with Bluetooth Controller via VHCI (Virtual Host Controller Interface).
While Bluetooth Protocols describe message formats and procedures for data transport, link control, etc., Bluetooth Profiles determine the functions of each tier of the Bluetooth stack, from PHY to L2CAP.
The list of Classic Bluetooth Profiles and Protocols that the BLUEDROID Bluetooth Stack of ESP32 supports is provided below.
Classic Bluetooth Profiles
- GAP
- A2DP (SNK)
- AVRCP (CT)
Classic Bluetooth Protocols
- L2CAP
- SDP
- AVDTP
- AVCTP
The ESP32’s Bluetooth controller and processor communicate through a serial interface. Using the ‘BluetoothSerial’ library for Classic Bluetooth, let’s learn more about ESP32 Bluetooth.
ESP32 Classic Bluetooth Serial Communication
You may recall that Arduino UNO and HC-05 connect via serial communication if you have ever worked with Arduino and any Bluetooth device, such as HC-05. Similar connectivity between the primary Xtensa Processor and the Bluetooth Controller is also present in the Bluetooth Controller-equipped ESP32.
This indicates that the Bluetooth controller in the ESP32 delivers data to the processor using serial connection after receiving it wirelessly from a Bluetooth device. Similar to this, the ESP32 Processor sends data to the Bluetooth Controller via the serial interface in order to deliver data over Bluetooth.
To broadcast and receive data, we’ll use this knowledge combined with a special “BluetoothSerial” library.
Similar to the Serial library in operation, the BluetoothSerial library is only found on the ESP32. The following are some of the frequently used features provided by the BluetoothSerial library:
- begin()
- available()
- write()
- read()
Let’s create a straightforward piece of code that sends data between an ESP32 and a mobile device. We shall print the data on the serial port in order to observe the ESP32’s received data. When it comes to the mobile phone, we need to use an application in order to send and receive data over Bluetooth.
I explored a lot of Bluetooth Serial Applications for Android but settled on Kai Morich’s “Serial Bluetooth Terminal.” You may get it by clicking this link (or from Play Store).
Code
Let’s first examine the code so that we can comprehend how it works. The code is really easy to read. Create a BluetoothSerial object, and then use the function “begin()” to start a conversation.
The ‘begin()’ function accepts the name of the ESP32 Bluetooth Device as an input. If you leave it empty, ESP32, which is the default name, is utilized. Initialize the standard serial connection as well at a baud rate of 115200.
Then, read data from BluetoothSerial, print it on the Serial Monitor, and read data from the Serial Monitor, publish it to BluetoothSerial, all within the loop function.
The Bluetooth Terminal App on the phone receives the data that is written to BluetoothSerial and prints it when it is opened. The BluetoothSerial will read the data you enter in the app and transfer it over Bluetooth, which is then printed on the Serial Monitor.
| Â | #include “BluetoothSerial.h” |
| Â | Â |
| Â | /* Check if Bluetooth configurations are enabled in the SDK */ |
| Â | /* If not, then you have to recompile the SDK */ |
| Â | #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); |
| Â | /* If no name is given, default ‘ESP32’ is applied */ |
| Â | /* If you want to give your own name to ESP32 Bluetooth device, then */ |
| Â | /* specify the name as an argument SerialBT.begin(“myESP32Bluetooth”); */ |
| Â | SerialBT.begin(); |
| Â | Serial.println(“Bluetooth Started! Ready to pair…”); |
| Â | } |
| Â | Â |
| Â | void loop() { |
| Â | if (Serial.available()) |
| Â | { |
| Â | Â SerialBT.write(Serial.read()); |
| Â | } |
| Â | if (SerialBT.available()) |
| Â | { |
| Â | Â Serial.write(SerialBT.read()); |
| Â | } |
| Â | delay(20); |
| Â | } |
Uploading the Code and Testing
If you open the serial monitor in the Arduino IDE after uploading the code to the ESP32, you can see the ESP32 outputting some Bluetooth-related information. Additionally, it shows the message “ready.”
When your phone asks you if you wish to pair with “ESP32,” you choose yes (or ok). No password is necessary. Click on the three horizontal bars in the top left corner of the screen when the “Serial Bluetooth Terminal” app is active on your phone.
To connect to the ESP32 Bluetooth Device, click the “link” symbol at the top. While establishing a connection, the app will show the status as “Connecting to ESP32…,” and if the connection is successful, it will show “Connected.”
Select ‘Devices’ tab and select ESP32 from the list.
To connect to the ESP32 Bluetooth Device, click the “link” symbol at the top. While establishing a connection, the app will show the status as “Connecting to ESP32…,” and if the connection is successful, it will show “Connected.”
Data entry for Bluetooth data transfer is available below. Enter some text and press the “send” button. The app plays back the sent data. This information is transmitted through Bluetooth to the ESP32 and is obtained by the BluetoothSerial read() method.
You can see the data shown on the serial monitor because we are sending this information to a serial port.
The ESP32 can deliver data to mobile phones in a manner similar to this. Simply enter some data in the serial monitor and press the submit button. Through the BluetoothSerial write() function, this data is transmitted through Bluetooth to a mobile device.
This data will be read by the serial Bluetooth terminal app, which writes it out.
Bluetooth Controlled LED using ESP32
With a small amount of code modification and the aforementioned program, we can use the ESP32 to construct a Bluetooth Controlled LED. The purpose of this project is to determine how simple it is to send and understand Bluetooth data in order to operate ESP32’s GPIO Pins.
Let’s broadcast “1” and “0” from the mobile phone app utilizing the macro keys to keep things simple. I put a “1” for M1 and a “0” for M2. You can compare the data that was received with the ASCII characters “1” and “0,” or their decimal equivalents, 49 and 48.
The LED attached to GPIO 2 will turn ON when the value “1” is received, and OFF when the value “0” is received.
Clearly, the LED just serves as a visual indicator of the GPIO Pin’s ON and OFF states. Using ESP32, you can further enhance this application to create a Bluetooth Controlled Relay.
Code
| Â | #include <BluetoothSerial.h> |
| Â | Â |
| Â | #define ledPIN 2 |
| Â | BluetoothSerial SerialBT; |
| Â | byte BTData; |
| Â | Â |
| Â | /* Check if Bluetooth configurations are enabled in the SDK */ |
| Â | #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) |
| Â | #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it |
| Â | #endif |
| Â | Â |
| Â | void setup() |
| Â | { |
| Â | pinMode(ledPIN, OUTPUT); |
| Â | Serial.begin(115200); |
| Â | SerialBT.begin(); |
| Â | Serial.println(“Bluetooth Started! Ready to pair…”); |
| Â | } |
| Â | Â |
| Â | void loop() |
| Â | { |
| Â | if(SerialBT.available()) |
| Â | { |
| Â | Â BTData = SerialBT.read(); |
| Â | Â Serial.write(BTData); |
| Â | } |
| Â | Â |
| Â | /* If received Character is 1, then turn ON the LED */ |
| Â | /* You can also compare the received data with decimal equivalent */ |
| Â | /* 48 for 0 and 49 for 1 */ |
| Â | /* if(BTData == 48) or if(BTData == 49) */ |
| Â | if(BTData == ‘1’) |
| Â | { |
| Â | Â digitalWrite(ledPIN, HIGH); |
| Â | } |
| Â | Â |
| Â | /* If received Character is 0, then turn OFF the LED */ |
| Â | if(BTData == ‘0’) |
| Â | { |
| Â | Â digitalWrite(ledPIN, LOW); |
| Â | } |
| Â | } |
Conclusion
I hope all of you understand the basics of ESP32 Bluetooth. We MATHA ELECTRONICS will be back soon with more informative blogs soon.