ESP32 with Bluetooth Serial Communication

What is Bluetooth Serial Communication?

Bluetooth Serial Communication is a wireless method of sending and receiving data between the ESP32 and another device, such as a smartphone or computer, over Bluetooth using a serial protocol. ESP32 supports Classic Bluetooth via the `BluetoothSerial` library.

Working Principle of Bluetooth Serial with ESP32

The ESP32 initializes a Bluetooth serial connection using the `BluetoothSerial` library. Once paired with a device (like a smartphone), it can send or receive data just like standard UART communication. This allows for controlling the ESP32 or receiving sensor data wirelessly over Bluetooth.

  • Include the BluetoothSerial library.
  • Initialize Bluetooth and set a device name.
  • Connect ESP32 to a phone/computer via Bluetooth.
  • Use Serial Monitor or phone app (like Serial Bluetooth Terminal) to send/receive data.
  • ESP32 reads/writes data using standard Serial functions.

Formula: N/A

Components Required

  • ESP32 development board
  • LED (for test output)
  • 220Ω resistor
  • Smartphone or Bluetooth-enabled PC
  • Micro USB cable

Pin Configuration

  • GPIO 2: Connected to LED (optional for control example)

Ensure that your mobile device supports Classic Bluetooth, not just BLE (Bluetooth Low Energy), as this example uses Classic Bluetooth.

Wiring Connections

  • Anode of LED -> GPIO 2 via 220Ω resistor
  • Cathode of LED -> GND

Arduino Code for ESP32 Bluetooth Serial Communication

1#include "BluetoothSerial.h"
2
3BluetoothSerial SerialBT;
4
5#define LED 2
6
7void setup() {
8  pinMode(LED, OUTPUT);
9  Serial.begin(115200);
10  SerialBT.begin("ESP32_BT"); // Name shown while pairing
11  Serial.println("Bluetooth Started! Pair your device.");
12}
13
14void loop() {
15  if (SerialBT.available()) {
16    char data = SerialBT.read();
17    Serial.write(data);
18
19    if (data == '1') {
20      digitalWrite(LED, HIGH);
21      SerialBT.println("LED ON");
22    } else if (data == '0') {
23      digitalWrite(LED, LOW);
24      SerialBT.println("LED OFF");
25    } else {
26      SerialBT.println("Send 1 to turn ON LED, 0 to turn OFF");
27    }
28  }
29  delay(20);
30}

Code Explanation (Line-by-Line)

  • #include "BluetoothSerial.h": Includes the Bluetooth Serial library for ESP32.
  • BluetoothSerial SerialBT;: Creates a Bluetooth Serial object named `SerialBT`.
  • SerialBT.begin("ESP32_BT");: Starts the Bluetooth service with the device name 'ESP32_BT'.
  • SerialBT.read();: Reads incoming data from Bluetooth.
  • digitalWrite(LED, HIGH/LOW);: Turns the LED ON or OFF based on received data.
  • SerialBT.println();: Sends response back to the Bluetooth device.

Applications

  • Wireless sensor monitoring
  • Home automation systems
  • Bluetooth remote control projects
  • Serial debugging wirelessly
  • Control robots or appliances via mobile phone

Conclusion

Bluetooth Serial Communication with ESP32 allows simple wireless communication between ESP32 and mobile devices. It is ideal for remote control, data monitoring, and debugging projects where Wi-Fi is not available or not required.