Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

ESP32 Bluetooth Classic

Introduction

The ESP32 is a powerful, low-cost microcontroller developed by Espressif Systems. It is widely used in IoT (Internet of Things) applications, smart devices, and wireless communication projects. One of its most significant features is built-in Bluetooth connectivity, which supports:

  1. Bluetooth Classic (BT) – Used for high-speed, continuous data transfer (like Bluetooth serial communication and audio streaming).
  2. Bluetooth Low Energy (BLE) – Used for low-power, intermittent communication (ideal for sensors and IoT applications).

In this Tutorial, we will focus on ESP32 Bluetooth Classic and how it works.

What is Bluetooth Classic?

Bluetooth Classic (BT) is a wireless communication technology designed for continuous and high-speed data exchange over short distances. It is widely used in:

  1. Wireless Audio Streaming (Bluetooth speakers, headphones)
  2. Wireless Serial Communication (like HC-05 Bluetooth module)
  3. Data Transfer Between Devices (phones, computers, and embedded systems)

ESP32 can act as:

  1. A Bluetooth Slave (accepts connections from other devices).
  2. A Bluetooth Master (connects to other Bluetooth devices).

Unlike Bluetooth Low Energy (BLE), which is optimized for low-power applications, Bluetooth Classic is ideal for applications that require higher data rates and continuous communication.

How Bluetooth Classic Works in ESP32?

ESP32 supports Serial Port Profile (SPP), allowing it to function as a wireless UART (Universal Asynchronous Receiver-Transmitter). This makes it a direct replacement for traditional serial communication methods.

Features

  • Wireless Serial Communication – Acts like a virtual COM port.
  • Bidirectional Data Transfer – Send & receive data over Bluetooth.
  • Compatible with Bluetooth 2.0 & 3.0 Devices – Can connect with phones, computers, and microcontrollers.
  • Supports Master & Slave Mode – Can initiate or accept Bluetooth connections.
  • Up to 3 Mbps Speed – Faster than traditional serial modules like HC-05/HC-06.

Comparison: Bluetooth Classic vs. Bluetooth Low Energy (BLE)

Feature Bluetooth Classic Bluetooth Low Energy (BLE)
Power Usage High Low
Data Rate High (Up to 3 Mbps) Low (Up to 1 Mbps)
Range 10-100 meters 50-100 meters
Latency Low High
Best For Audio, Serial Data IoT Sensors, Smart Devices
Example Uses Wireless Audio, Serial Communication Heart Rate Monitors, Beacons

Setting Up ESP32 for Bluetooth Classic

Install ESP32 Board in Arduino IDE

To use the ESP32, with the Arduino IDE, you must first install the ESP32 board package (also known as the ESP32 Arduino Core) through the Arduino Board Manager.

If you haven’t already, follow this tutorial to install the ESP32 board.

ESP32 Bluetooth Classic Code Example

This program will turn ESP32 into a Bluetooth Serial Device named ESP32_BT. You can send and receive data using a mobile app or PC.

#include "BluetoothSerial.h"   // Include Bluetooth Library

BluetoothSerial SerialBT;      // Create Bluetooth Serial Object

void setup() {
    Serial.begin(115200);     // Start Serial Monitor
    SerialBT.begin("ESP32_BT");  // Set Bluetooth Device Name
    Serial.println("Bluetooth Started! Pair with 'ESP32_BT'");
}

void loop() {
    if (SerialBT.available()) {   // Check if Bluetooth received data
        char receivedChar = SerialBT.read();
        Serial.write(receivedChar);  // Print data on Serial Monitor
    }

    if (Serial.available()) {   // Check if Serial Monitor sends data
        char sendChar = Serial.read();
        SerialBT.write(sendChar);   // Send data over Bluetooth
    }
}

Explanation of Code

  • SerialBT.begin("ESP32_BT"); → Sets the Bluetooth name as ESP32_BT
  • SerialBT.read(); → Reads data received from Bluetooth
  • SerialBT.write(data); → Sends data over Bluetooth
  • The program echoes received Bluetooth messages on the Serial Monitor.

Connect ESP32 with Phone

Let’s set up a wireless connection between the ESP32 and an phone. The process may vary depending on the device, but the general steps are quite similar.

1. Make sure the ESP32 is powered up and ready to establish a connection.

2. Now, swipe down from the top of your Android phone’s screen and make sure Bluetooth is turned on.

3. Touch and hold the Bluetooth icon, then tap “Pair new device” and wait a few seconds.

4. Tap the name of the Bluetooth device you want to pair with your device (in our case, ESP32_BT). Follow any on-screen instructions.

5. For the next steps in this tutorial, you need a Bluetooth Terminal application installed on your smartphone. We recommend using the Android app “Arduino Bluetooth Controller,” available in the Play Store.

6. After installing, launch the “Arduino Bluetooth Controller” app. Click on the icon of terminal

7. After going to the terminal, click on the Plugins option, which is shown in the red square below.

8. You should see a list of devices you’ve previously paired with. Select “ESP32_BT” from this list.

9. After clicking on ESP32_BT, you will see a green indication on the Plugins option. Now, your ESP32 is connected to your phone.

10. Now, type something in the input box located at the bottom of the app, for example, “ 1 ”

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

12. You can also exchange data between your Serial Monitor and your smartphone. Type something in the Serial Monitor’s top input box and press the “ENTER” button.

13. You should instantly receive that message in the Arduino Bluetooth Controller App.

Controlling LEDs via Bluetooth (Advanced)

Introduction

This project allows you to wirelessly control an LED connected to an ESP32 using Bluetooth. The ESP32 acts as a Bluetooth server, allowing a smartphone or computer to send commands to turn the LED ON or OFF.

Required Components

  • ESP32
  • LED
  • Resistor 220 OHM
  • Jumper Wire
  • Bread board (Optional)

Circuit Diagram / Wiring

  • Setup the Hardware: Connect the LED and resistor to GPIO 2 pins of the ESP32.

Arduino Code / Programming

We can modify the code to turn an LED ON/OFF using Bluetooth.

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
const int ledPin = 2; // LED connected to GPIO2

void setup() {
    Serial.begin(115200);
    SerialBT.begin("ESP32_BT_LED");
    pinMode(ledPin, OUTPUT);
    Serial.println("Bluetooth Ready!");
}

void loop() {
    if (SerialBT.available()) {  
        char command = SerialBT.read();  
        Serial.println(command);
        
        if (command == '1') {
            digitalWrite(ledPin, HIGH);  // Turn LED ON
            SerialBT.println("LED ON");
        }
        else if (command == '0') {
            digitalWrite(ledPin, LOW);  // Turn LED OFF
            SerialBT.println("LED OFF");
        }
    }
}

How It Works?

  • Sending “1” → Turns LED ON
  • Sending “0” → Turns LED OFF
  • The response LED ON or LED OFF is sent back via Bluetooth.

    Leave a Reply

    Your email address will not be published.

    Need Help?