Skip to content

Bluetooth Documentation

Introduction

Bluetooth is a short-range wireless communication protocol commonly used to connect devices such as microcontrollers, sensors, smartphones, and computers. It operates in the 2.4 GHz ISM band and supports both Classic Bluetooth (higher data rate) and Bluetooth Low Energy (BLE) (low power, suitable for IoT).

How It Works

  • Wireless communication: No wires needed; devices communicate via radio waves.
  • Master-slave or central-peripheral model: One device initiates connection (master/central), others respond (slave/peripheral).
  • Pairing and bonding: Devices exchange keys for secure communication.
  • Data transmission: BLE uses GATT (Generic Attribute Profile) for structured data exchange.

Diagram – Basic Bluetooth Connection

[ESP32 / STM32] <--wireless--> [Phone / Laptop / Sensor]

Use Cases

  • Wireless control of robots or drones
  • Telemetry from sensors to a mobile app
  • Remote configuration and debugging of microcontrollers
  • Short-range wireless data logging

Common Pitfalls / Gotchas

  • Pairing issues: Make sure devices are discoverable and use compatible Bluetooth versions.
  • Range limitations: Classic Bluetooth up to ~100m; BLE typically ~10–30m indoors.
  • Interference: Other 2.4 GHz devices (Wi-Fi, microwaves) can cause packet loss.
  • Power consumption: BLE is much lower than Classic Bluetooth; consider for battery-powered devices.

Tools to Debug

  • Mobile apps like nRF Connect or LightBlue to scan and connect to devices
  • Logic analyzer for BLE sniffing (optional, advanced)
  • Serial monitor on ESP32/STM32 for printed debug messages

References

Onboarding Tutorial (ESP32 or STM32)

Goal: Connect a microcontroller to a phone via BLE and send data.

Materials

  • ESP32 or STM32 board with BLE support
  • Smartphone with BLE scanning app (e.g., nRF Connect)
  • Jumper wires (optional for sensors)

Steps

  1. Flash ESP32/STM32 with BLE peripheral example code.
  2. Open BLE scanning app on the phone and connect to the microcontroller.
  3. Send a simple value from the board and verify it on the app (e.g., temperature or counter).
// Example ESP32 BLE peripheral (Arduino style)
#include <BLEDevice.h>
#include <BLEServer.h>

BLEServer* pServer;
BLECharacteristic* pCharacteristic;

void setup() {
  Serial.begin(115200);
  BLEDevice::init("ESP32_BLE");
  pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService("1234");
  pCharacteristic = pService->createCharacteristic(
                      "5678",
                      BLECharacteristic::PROPERTY_READ |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pService->start();
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->start();
}

void loop() {
  static int counter = 0;
  pCharacteristic->setValue(counter++);
  pCharacteristic->notify();
  delay(1000);
}

Mini Challenges

  • Send sensor data (e.g., temperature) instead of a counter.
  • Add a second BLE characteristic and read/write from the phone app.
  • Connect multiple devices simultaneously and observe updates.