ESP32 Bluetooth & BLE
Bluetooth allows your ESP32 to communicate wirelessly with smartphones, computers, sensors, and other devices without using Wi-Fi.
The ESP32 family can support different Bluetooth technologies depending on the exact chip/board.
1. Bluetooth on ESP32
There are two major types:
ESP32 Bluetooth │ ┌───────────┴───────────┐ │ │ Bluetooth Classic BLE │ │ Serial / SPP Low Power Audio, etc. Sensors / IoT
Bluetooth Classic
Designed for continuous communication and relatively higher data throughput.
Example:
Phone │ │ Bluetooth ▼ ESP32 │ ▼ LED / Motor / Relay
A common ESP32 use is Bluetooth Serial (SPP).
BLE — Bluetooth Low Energy
BLE is optimized for:
- Low power
- Sensors
- IoT devices
- Wearables
- Small amounts of data
- Battery-powered devices
Example:
Temperature Sensor │ │ BLE ▼ Smartphone
2. Important: ESP32 Variants
Bluetooth capability depends on the ESP32 chip.
| ESP32 Chip | Bluetooth |
|---|---|
| Original ESP32 | Classic + BLE |
| ESP32-S2 | ❌ No Bluetooth |
| ESP32-S3 | BLE, no Classic |
| ESP32-C3 | BLE, no Classic |
| ESP32-C6 | BLE, no Classic |
So if you want to use Bluetooth Classic Serial, the original ESP32 is an important choice.
For BLE projects, newer chips such as ESP32-C3/S3/C6 can also be used.
3. Bluetooth Classic vs BLE
| Feature | Classic | BLE |
|---|---|---|
| Main purpose | Continuous communication | Low-power IoT |
| Power consumption | Higher | Lower |
| Typical use | Serial, audio | Sensors, smart devices |
| Communication model | Connection/stream | Services & characteristics |
| Phone support | Yes | Yes |
| Battery devices | Less suitable | Excellent |
4. Bluetooth Classic Serial
One of the easiest ESP32 Bluetooth projects is:
Phone │ │ Bluetooth ▼ ESP32 │ ├── GPIO │ └── LED
The phone sends:
1
ESP32 turns LED ON.
Phone sends:
0
ESP32 turns LED OFF.
5. Bluetooth Serial Example
For a classic ESP32 that supports Bluetooth Classic:
#include "BluetoothSerial.h" BluetoothSerial SerialBT; #define LED_PIN 2 void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(115200); SerialBT.begin("ESP32_Control"); Serial.println("Bluetooth Started"); } void loop() { if (SerialBT.available()) { char command = SerialBT.read(); if (command == '1') { digitalWrite(LED_PIN, HIGH); SerialBT.println("LED ON"); } else if (command == '0') { digitalWrite(LED_PIN, LOW); SerialBT.println("LED OFF"); } } }
The ESP32 will advertise itself as:
ESP32_Control
A compatible Bluetooth serial application can connect to it.
6. How the Code Works
Start Bluetooth
SerialBT.begin("ESP32_Control");
This creates the Bluetooth device name.
Check incoming data
if (SerialBT.available())
This checks whether the ESP32 received something.
Read the command
char command = SerialBT.read();
For example:
Phone → "1"
ESP32 receives:
command = '1'
Turn LED ON
if (command == '1') { digitalWrite(LED_PIN, HIGH); }
7. Bluetooth Control of a Relay
You can easily replace the LED with a relay.
Smartphone │ Bluetooth │ ▼ ESP32 │ GPIO 26 │ ▼ Relay │ ▼ Light / Fan
Example:
#define RELAY_PIN 26 if (command == '1') { digitalWrite(RELAY_PIN, HIGH); SerialBT.println("Relay ON"); } if (command == '0') { digitalWrite(RELAY_PIN, LOW); SerialBT.println("Relay OFF"); }
⚠️ For mains AC appliances, use an appropriately rated isolated relay module and proper electrical safety practices.
8. What is BLE?
BLE works differently from Bluetooth Serial.
Instead of simply thinking:
Phone ←→ ESP32
BLE commonly uses:
BLE │ ┌─────┴─────┐ │ │ Peripheral Central │ │ ESP32 Phone
Usually:
- ESP32 = Peripheral
- Phone = Central
But the roles can vary depending on the application.
9. BLE GATT
The most important BLE concept is GATT.
GATT organizes information into:
BLE Device │ └── Service │ ├── Characteristic │ ├── Characteristic │ └── Characteristic
For example, a temperature sensor could have:
ESP32 │ └── Temperature Service │ └── Temperature Characteristic │ └── 28.6 °C
10. BLE Service
A service groups related functionality.
Example:
Environmental Service
could contain:
Temperature Humidity Pressure
11. BLE Characteristic
A characteristic contains the actual data.
Example:
Temperature Characteristic │ └── 28.5 °C
Another:
LED Characteristic │ └── ON
12. BLE Operations
A characteristic can support operations such as:
Read
Phone asks:
"What is the temperature?"
ESP32:
28.5°C
Write
Phone sends:
ON
ESP32 changes the LED.
Notify
ESP32 automatically sends new data.
For example:
ESP32 │ ├── 25°C ├── 25.4°C ├── 26°C └── 26.3°C ↓ Phone
This is extremely useful for IoT sensors.
13. BLE UUID
BLE services and characteristics normally have identifiers called UUIDs.
Example:
Service UUID 12345678-1234-1234-1234-123456789abc
Characteristic:
87654321-4321-4321-4321-cba987654321
Think of UUID as a unique address/name for a BLE service or characteristic.
14. BLE Advertising
Before a BLE device connects, it can advertise its presence.
ESP32 │ │ Advertising ▼ Nearby Phones │ ├── ESP32 Sensor ├── ESP32 Door └── ESP32 Robot
The phone scans for BLE advertisements.
Then it can connect to the desired device.
15. BLE Sensor Example
Imagine your ESP32 has a DHT22.
DHT22 │ │ Temperature │ Humidity ▼ ESP32 │ │ BLE ▼ Smartphone
The ESP32 could expose:
Environmental Service │ ├── Temperature │ └── Humidity
Phone displays:
Temperature: 27.4 °C Humidity: 63 %
This is a very common IoT architecture.
16. Bluetooth vs Wi-Fi
This is very important when designing projects.
Bluetooth
Phone │ Bluetooth │ ESP32
Good for:
- Nearby control
- Remote control
- Sensors
- Low-power devices
- No router required
Wi-Fi
Phone │ Router │ ESP32
Good for:
- Internet access
- Web servers
- Cloud
- Remote monitoring
- MQTT
- Firebase
17. Bluetooth + Wi-Fi Together
The ESP32 can potentially use both wireless technologies.
For example:
Internet │ Wi-Fi │ ▼ ESP32 / \ / \ Bluetooth GPIO │ │ ▼ ▼ Phone Relay
This can be useful in smart-home projects.
18. Example: Bluetooth Smart Home
For your IoT project, you could create:
Smartphone / \ / \ Bluetooth Bluetooth │ │ ▼ ▼ ESP32 ESP32 │ ┌─────┼──────┐ │ │ │ Light Fan Door
Commands:
L → Light ON l → Light OFF F → Fan ON f → Fan OFF D → Door OPEN d → Door CLOSE
This can later be expanded to:
Bluetooth │ ▼ ESP32 ┌───┼───────────────┐ │ │ │ │ LED Fan Servo Relay
19. Bluetooth Security
Never assume Bluetooth automatically makes your project secure.
For important devices such as:
- Door locks
- Security systems
- Smart-home controls
consider:
- Pairing/authentication
- Encryption
- Authorization
- Strong commands
- Avoiding predictable control codes
For example, don't rely solely on:
OPEN
for a real door lock.
A real system should authenticate the controller and validate commands.
20. Common Mistakes
❌ Mistake 1: Using Bluetooth Classic code on unsupported ESP32
For example:
#include "BluetoothSerial.h"
will not be appropriate for ESP32-S2 and ESP32-C3/S3/C6 when you specifically need Classic Bluetooth.
❌ Mistake 2: Confusing BLE with Bluetooth Serial
BLE is not simply:
SerialBT.read()
BLE uses concepts such as:
Service ↓ Characteristic ↓ Read / Write / Notify
❌ Mistake 3: Expecting unlimited range
Bluetooth is primarily a short-range wireless technology. Actual range depends on the ESP32 variant, antenna, power level, environment, and phone.
❌ Mistake 4: Sending 5V logic directly to ESP32 GPIO
Bluetooth doesn't change the ESP32's GPIO electrical limits.
Remember:
ESP32 GPIO ↓ 3.3V logic
Don't directly feed a 5V signal into a GPIO unless the interface is properly level-shifted.
🧠 Quick Summary
Bluetooth │ ├── Classic Bluetooth │ └── Serial / SPP │ └── BLE ├── Low power ├── Services ├── Characteristics ├── Read ├── Write └── Notify
Remember:
Classic Bluetooth → continuous/serial communication
BLE → low-power IoT communication
And always check the exact ESP32 chip, because Bluetooth capabilities differ between ESP32 variants.