ESP32 Bluetooth
ESP32 Bluetooth Communication
ESP32 Bluetooth Communication demonstrates how the built-in Bluetooth functionality of the ESP32 can be used for wireless communication between the ESP32 board and a Bluetooth-enabled device such as a smartphone, tablet, or computer.
The ESP32 can send and receive data wirelessly using Bluetooth without requiring a physical serial connection.
Bluetooth Device → Wireless Communication → ESP32
Objectives
- To study the Bluetooth functionality available in the ESP32.
- To understand how to initialize Bluetooth communication.
- To pair the ESP32 with a Bluetooth-enabled device.
- To send data from the ESP32 to a Bluetooth device.
- To receive commands or data from a Bluetooth device.
- To control ESP32 peripherals using Bluetooth commands.
Software
- Arduino IDE
- ESP32 board package
- Bluetooth-enabled smartphone, tablet, or computer
Hardware Modules
- ESP32 Development Board
- PC / Laptop
- Smartphone / Bluetooth-enabled device
- External sensors or actuators as required
Theory
Bluetooth is a wireless communication technology used for exchanging data between electronic devices over short distances.
The ESP32 contains built-in Bluetooth hardware, allowing it to communicate with compatible Bluetooth devices without requiring an external Bluetooth module.
The ESP32 can be programmed to operate as a Bluetooth device with a user-defined device name.
ESP32 Bluetooth → Wireless Data Transmission → Smartphone / PC
Bluetooth communication can be used for:
- Sending sensor readings
- Receiving control commands
- Controlling LEDs
- Controlling buzzers
- Monitoring system status
- Sending data to a mobile application
- Wireless serial communication
ESP32 Bluetooth
The ESP32 provides built-in Bluetooth functionality.
For Bluetooth Classic communication in the Arduino environment, the BluetoothSerial library can be used.
The library provides a serial-like interface for sending and receiving data wirelessly.
The basic sequence is:
Initialize Bluetooth → Set Device Name → Pair Device → Send/Receive Data
BluetoothSerial Library
The BluetoothSerial library provides functions for Bluetooth Classic communication.
The library is included using:
#include "BluetoothSerial.h"
A Bluetooth serial object is then created:
BluetoothSerial SerialBT;
Bluetooth communication is started using:
SerialBT.begin("ESP32_Demo");
Here, ESP32_Demo is the Bluetooth device name that appears when searching for the ESP32 from another Bluetooth device.
Bluetooth Communication
Bluetooth communication works in two directions.
Sending Data
The ESP32 can send data to a connected Bluetooth device using:
SerialBT.print("Hello");
SerialBT.println("ESP32");
The print() function sends data without automatically adding a new line.
The println() function sends data followed by a new line.
ESP32 → Bluetooth → Smartphone / PC
Receiving Data
The ESP32 can check whether data has been received using:
SerialBT.available()
Received characters can be read using:
char c = SerialBT.read();
The received characters can then be processed as commands or sensor-control instructions.
Smartphone / PC → Bluetooth → ESP32
Bluetooth Device Name
The ESP32 Bluetooth device name can be specified while starting Bluetooth.
Example:
SerialBT.begin("ESP32_Demo");
After Bluetooth is started, the device appears as:
ESP32_Demo
on the Bluetooth device used for pairing.
The name can be changed as required.
For example:
SerialBT.begin("MyESP32");
Connections
Bluetooth communication itself does not require external communication wires between the ESP32 and the smartphone or computer.
| Device | Connection | Function |
|---|---|---|
| ESP32 | Built-in Bluetooth | Wireless communication |
| Smartphone / PC | Bluetooth | Wireless communication |
External sensors and actuators can be connected to the ESP32 separately depending on the application.
Basic Bluetooth Communication Flow
The Bluetooth communication process can be summarized as:
- Start the Arduino IDE.
- Include the BluetoothSerial library.
- Create a Bluetooth serial object.
- Start Bluetooth using SerialBT.begin().
- Assign a name to the ESP32 Bluetooth device.
- Search for the ESP32 using a Bluetooth-enabled device.
- Pair/connect the device with the ESP32.
- Send data from the ESP32 using SerialBT.print() or SerialBT.println().
- Check for incoming data using SerialBT.available().
- Read incoming data using SerialBT.read().
- Process the received data according to the application.
Algorithm
- Start the Arduino IDE.
- Include the BluetoothSerial library.
- Create a BluetoothSerial object.
- Start Bluetooth communication with a suitable device name.
- Display the Bluetooth status on the Serial Monitor.
- Wait for a Bluetooth device to connect.
- Check whether data is available from the Bluetooth device.
- Read the received data.
- Process the received command or data.
- Send a response using Bluetooth.
- Repeat the process continuously.
Start → Initialize Bluetooth → Connect Device → Send/Receive Data → Process Data → Repeat
Working
When the ESP32 program starts, the Bluetooth interface is initialized using the BluetoothSerial library.
The ESP32 becomes discoverable using the Bluetooth device name specified in the program.
After pairing or connecting a smartphone or computer, data can be exchanged wirelessly.
The ESP32 can send messages using:
SerialBT.println("Hello from ESP32");
It can also receive data from the connected Bluetooth device.
For example, a smartphone can send:
ON
The ESP32 can receive this command and use it to control an output device such as an LED.
Similarly, a command such as:
OFF
can be used to turn the LED OFF.
Bluetooth Command → ESP32 → Program Processing → Output Control
Download Cognifront Bluetooth App
Kindly install Cognifront Bluetooth App on your mobile phone (Android Support only) for doing communication with ESP32.
Example Program
The following program demonstrates basic Bluetooth communication with the ESP32.
/*
========================================
ESP32 Bluetooth + LM35 + Buzzer Demo
========================================
Bluetooth Commands:
ON -> Turn LED ON
OFF -> Turn LED OFF
TEMP -> Read LM35 Temperature
STATUS -> Check ESP32 Status
BLINK -> Blink LED 5 Times
BUZZON -> Turn Buzzer ON
BUZZOFF -> Turn Buzzer OFF
========================================
Connections:
LM35:
VCC -> 3.3V
OUT -> GPIO34
GND -> GND
Buzzer:
+ -> GPIO25
- -> GND
LED:
Built-in LED -> GPIO2
Bluetooth Device Name:
ESP32_Demo
========================================
*/
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
#define LED_PIN 2
#define LM35_PIN 34
#define BUZZER_PIN 25
String incomingData = "";
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
// Start Bluetooth
SerialBT.begin("ESP32_Demo");
Serial.println("Bluetooth Started");
SerialBT.println("ESP32 Bluetooth Ready");
SerialBT.println("Commands:");
SerialBT.println("ON, OFF, TEMP, STATUS");
SerialBT.println("BLINK, BUZZON, BUZZOFF");
}
void loop() {
while (SerialBT.available()) {
char c = SerialBT.read();
if (c == '\n') {
incomingData.trim();
incomingData.toUpperCase();
Serial.print("Received: ");
Serial.println(incomingData);
// ======================
// LED ON
// ======================
if (incomingData == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED ON");
}
// ======================
// LED OFF
// ======================
else if (incomingData == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED OFF");
}
// ======================
// TEMP READ
// ======================
else if (incomingData == "TEMP") {
float temperatureC = readTemperature();
SerialBT.print("Temperature: ");
SerialBT.print(temperatureC);
SerialBT.println(" °C");
Serial.print("Temperature: ");
Serial.println(temperatureC);
// Auto buzzer alert
if (temperatureC > 35) {
digitalWrite(BUZZER_PIN, HIGH);
SerialBT.println("WARNING: HIGH TEMPERATURE!");
}
else {
digitalWrite(BUZZER_PIN, LOW);
}
}
// ======================
// STATUS
// ======================
else if (incomingData == "STATUS") {
SerialBT.println("ESP32 Running OK");
}
// ======================
// BLINK
// ======================
else if (incomingData == "BLINK") {
SerialBT.println("Blinking LED");
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(300);
digitalWrite(LED_PIN, LOW);
delay(300);
}
SerialBT.println("Blink Complete");
}
// ======================
// BUZZER ON
// ======================
else if (incomingData == "BUZZON") {
digitalWrite(BUZZER_PIN, HIGH);
SerialBT.println("Buzzer ON");
}
// ======================
// BUZZER OFF
// ======================
else if (incomingData == "BUZZOFF") {
digitalWrite(BUZZER_PIN, LOW);
SerialBT.println("Buzzer OFF");
}
// ======================
// UNKNOWN COMMAND
// ======================
else {
SerialBT.println("Unknown Command");
}
incomingData = "";
}
else {
incomingData += c;
}
}
}
// =====================================
// FUNCTION: Read LM35 Temperature
// =====================================
float readTemperature() {
int adcValue = analogRead(LM35_PIN);
// Convert ADC to Voltage
float voltage = adcValue * (3.3 / 4095.0);
// LM35 = 10mV per °C
float temperatureC = voltage * 100.0;
return temperatureC;
}
Example of Command-Based Communication
Bluetooth can also be used to send commands to the ESP32.
For example:
| Command | Function |
|---|---|
| ON | Turn an output ON |
| OFF | Turn an output OFF |
| STATUS | Send ESP32 status |
| SENSOR | Send sensor reading |
A command received through Bluetooth can be compared in the program and the required operation can be performed.
Example:
if (command == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED ON");
}
else if (command == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED OFF");
}
Applications
ESP32 Bluetooth communication can be used in many applications, including:
- Wireless LED control
- Home automation
- Sensor monitoring
- Robot control
- Wireless data logging
- Industrial control systems
- Smart appliances
- Mobile-controlled projects
- Wireless serial communication
Safety Precautions
- Check the ESP32 power supply before starting the experiment.
- Use the correct GPIO pins for external sensors and actuators.
- Do not exceed the GPIO current limits of the ESP32.
- Ensure that the Bluetooth device is properly paired before testing communication.
- Keep the Bluetooth-enabled devices within the appropriate communication range.
- Use suitable driver circuits when controlling high-current loads.
Observation
After starting the program, the ESP32 appears as the configured Bluetooth device name.
After pairing the ESP32 with a smartphone or computer, messages can be transmitted wirelessly.
Observe the transmitted and received data on the Serial Monitor and the connected Bluetooth device.
Result
The ESP32 successfully communicates with a Bluetooth-enabled device using its built-in Bluetooth interface.
Data can be transmitted from the ESP32 to the connected device, and commands or data can be received by the ESP32 through Bluetooth.
The Bluetooth interface can therefore be used for wireless monitoring, control, and data communication in ESP32-based applications.