ESPIoT HardwaresSensorsTutorials/DIY

ESP32 BLE on Arduino IDE with UART Test

Introduction

The ESP32 is a powerful microcontroller that supports both Wi-Fi and Bluetooth functionalities. One of the key features of the ESP32 is Bluetooth Low Energy (BLE), which is widely used for short-range wireless communication in IoT applications. Using BLE in combination with UART (Universal Asynchronous Receiver-Transmitter) allows seamless wireless communication between devices such as smartphones, tablets, and computers.

In this comprehensive guide, we’ll walk through setting up ESP32 BLE with UART communication using the Arduino IDE. We’ll create a BLE server on the ESP32 that transmits and receives data over UART, demonstrating a practical BLE-to-UART test setup.

Prerequisites

Before proceeding, ensure you have the following:

  • ESP32 Development Board
  • Arduino IDE (latest version recommended)
  • ESP32 Board Package installed in Arduino IDE
  • Smartphone or Tablet with a BLE scanner app (e.g., nRF Connect, BLE Scanner)

Step 1: Install the ESP32 Board Package

If you haven’t installed the ESP32 board package in Arduino IDE yet:

  1. Open Arduino IDE.
  2. Go to File → Preferences.
  3. In the “Additional Board Manager URLs” field, add the following link:
https://dl.espressif.com/dl/package_esp32_index.json
  1. Go to Tools → Board → Board Manager.
  2. Search for ESP32 and click Install.

Step 2: ESP32 BLE Code for UART Communication

Create a new sketch in Arduino IDE and paste the following code:

C
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define SERVICE_UUID        "12345678-1234-5678-1234-56789abcdef0"
#define CHARACTERISTIC_UUID "abcdef01-1234-5678-1234-56789abcdef0"

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;

class MyServerCallbacks : public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
        deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
        deviceConnected = false;
    }
};

void setup() {
    Serial.begin(115200);
    Serial.println("Starting BLE UART Service...");

    // Initialize BLE
    BLEDevice::init("ESP32_BLE_UART");
    pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyServerCallbacks());

    // Create BLE Service
    BLEService *pService = pServer->createService(SERVICE_UUID);

    // Create BLE Characteristic for UART Communication
    pCharacteristic = pService->createCharacteristic(
                        CHARACTERISTIC_UUID,
                        BLECharacteristic::PROPERTY_READ |
                        BLECharacteristic::PROPERTY_WRITE |
                        BLECharacteristic::PROPERTY_NOTIFY
                    );

    pCharacteristic->addDescriptor(new BLE2902());

    // Start the service
    pService->start();

    // Start advertising
    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
    pAdvertising->addServiceUUID(SERVICE_UUID);
    pServer->getAdvertising()->start();

    Serial.println("BLE UART Service is now active");
}

void loop() {
    if (deviceConnected) {
        // Check for incoming data from BLE and send it to UART
        std::string rxValue = pCharacteristic->getValue();
        if (rxValue.length() > 0) {
            Serial.print("Received Value: ");
            Serial.println(rxValue.c_str());
            pCharacteristic->setValue("Data Received: " + rxValue);
            pCharacteristic->notify();
        }
    }
    delay(1000);
}

Step 3: Upload Code to ESP32

  1. Connect your ESP32 board to your computer via USB.
  2. Go to Tools → Board and select ESP32 Dev Module.
  3. In Tools → Port, select the correct COM port for your ESP32.
  4. Click the Upload button to flash the code.

Step 4: Testing BLE Communication

To test the BLE UART communication:

  1. Install a BLE scanner app like nRF Connect or BLE Scanner on your smartphone.
  2. Power up your ESP32 board. It will appear as ESP32_BLE_UART in the BLE scanner app.
  3. Connect to ESP32_BLE_UART and locate the characteristic with UUID abcdef01-1234-5678-1234-56789abcdef0.
  4. Send a string (e.g., “Hello ESP32”) from the BLE scanner app.
  5. The string should appear on the Serial Monitor in Arduino IDE.
  6. The ESP32 will respond back with “Data Received: [your message]” via BLE.

Step 5: UART Communication Test

To extend this BLE communication to UART:

  1. Connect the TX and RX pins of the ESP32 to another microcontroller or UART device.
  2. Use Serial2.begin(baud_rate, SERIAL_8N1, RX_PIN, TX_PIN); in the ESP32 code to initialize UART communication.
  3. Forward received BLE data to UART using:
Serial2.print(rxValue.c_str());
  1. Data sent via UART will now be broadcasted over BLE.

Step 6: Troubleshooting Common Issues

If you encounter issues, consider the following solutions:

  • ESP32 Not Visible on BLE Scanner: Ensure BLE is initialized correctly in setup() and verify BLE services are active.
  • Data Not Received: Verify the BLE characteristic UUID is correctly defined in the code and matches the scanner app.
  • Serial Monitor Not Displaying Data: Ensure your Serial Monitor baud rate is set to 115200.
  • Connection Drop Issues: Verify your smartphone is within range and Wi-Fi/BLE interference is minimized.

Step 7: Applications of ESP32 BLE UART

The ESP32 BLE UART functionality is useful in various scenarios such as:

  • IoT Device Control (e.g., smart home automation)
  • Wireless Sensor Data Transmission
  • BLE-Based Data Logging Systems
  • Communication Between Multiple ESP32 Devices

Conclusion

Using ESP32 BLE with UART on Arduino IDE provides a powerful wireless communication solution for IoT projects. This setup simplifies data transmission between the ESP32 and other devices without complex wiring. By combining BLE and UART, you gain flexibility, reliability, and scalability in your ESP32-based applications.


You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin