Friday, March 29, 2024
ESPIoT HardwaresTutorials/DIY

ESP32 Bluetooth Low Energy (BLE) on Arduino IDE

The ESP32 comes not only with Wi-Fi but also with Bluetooth and Bluetooth Low Energy (BLE). This post is a quick introduction to BLE with the ESP32.

What is Bluetooth Low Energy?

Bluetooth Low Energy, BLE for short, is a power-conserving variant of Bluetooth. BLE’s primary application is short distance transmission of small amounts of data (low bandwidth). Unlike Bluetooth that is always on, BLE remains in sleep mode constantly except for when a connection is initiated.

This makes it consume very low power. BLE consumes approximately 100x less power than Bluetooth (depending on the use case).

Take a look at the table below that compares BLE and Bluetooth in more detail.

Bluetooth Low Energy (LE) Bluetooth Basic Rate/
Enhanced Data Rate (BR/EDR)
Optimized For… Short burst data transmission Continuous data streaming
Frequency Band 2.4GHz ISM Band (2.402 – 2.480 GHz Utilized) 2.4GHz ISM Band (2.402 – 2.480 GHz Utilized)
Channels 40 channels with 2 MHz spacing
(3 advertising channels/37 data channels)
79 channels with 1 MHz spacing
Channel Usage Frequency-Hopping Spread Spectrum (FHSS) Frequency-Hopping Spread Spectrum (FHSS)
Modulation GFSK GFSK, π/4 DQPSK, 8DPSK
Power Consumption ~0.01x to 0.5x of reference
(depending on use case)
1 (reference value)
Data Rate LE 2M PHY: 2 Mb/s
LE 1M PHY: 1 Mb/s
LE Coded PHY (S=2): 500 Kb/s
LE Coded PHY (S=8): 125 Kb/s
EDR PHY (8DPSK): 3 Mb/s
EDR PHY (π/4 DQPSK): 2 Mb/s
BR PHY (GFSK): 1 Mb/s
Max Tx Power* Class 1: 100 mW (+20 dBm)
Class 1.5: 10 mW (+10 dbm)
Class 2: 2.5 mW (+4 dBm)
Class 3: 1 mW (0 dBm)
Class 1: 100 mW (+20 dBm)
Class 2: 2.5 mW (+4 dBm)
Class 3: 1 mW (0 dBm)
Network Topologies Point-to-Point (including piconet)
Broadcast
Mesh
Point-to-Point (including piconet)

for more information please visit https://www.bluetooth.com/

BLE with ESP32

The ESP32 can act as a BLE server or as a BLE client. There are several BLE examples for the ESP32 in the ESP32 BLE library for Arduino IDE. This library comes installed by default when you install the ESP32 on the Arduino IDE.

Note: You need to have the ESP32 add-on installed on the Arduino IDE. Follow one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if you haven’t already.

Visit this Article http://iotbyhvm.ooo/arduino-esp32-support-on-windows-and-ubuntu/

In your Arduino IDE, you can go to File > Examples > ESP32 BLE Arduino and explore the examples that come with the BLE library.

Note: to see the ESP32 examples, you must have the ESP32 board selected on ToolsBoard. and Select correct port.

ESP32 BLE Server

To create an ESP32 BLE Server, open your Arduino IDE and go to File > Examples > ESP32 BLE Arduino and select the BLE_server example.

I explain Code how the Code Works

For creating a BLE server, the code should follow the next steps:
  1. Create a BLE Server. In this case, the ESP32 acts as a BLE server.
  2. Create a BLE Service.
  3.  Create a BLE Characteristic on the Service.
  4. Create a BLE Descriptor on the Characteristic.
  5. Start the Service.
  6.  Start advertising, so it can be found by other devices.

How the code works

Let’s take a quick look at how the BLE server example code works.

It starts by importing the necessary libraries for the BLE capabilities.

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

Then, you need to define a UUID for the Service and Characteristic.

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

You can leave the default UUIDs, or you can go to uuidgenerator.net to create random UUIDs for your services and characteristics.

In the setup(), it starts the serial communication at a baud rate of 115200.

Serial.begin(115200);

Then, you create a BLE device called “MyESP32”. You can change this name to whatever you like.

// Create the BLE Device
BLEDevice::init("MyESP32");

In the following line, you set the BLE device as a server.

BLEServer *pServer = BLEDevice::createServer();

After that, you create a service for the BLE server with the UUID defined earlier.

 BLEService *pService = pServer->createService(SERVICE_UUID);

Then, you set the characteristic for that service. As you can see, you also use the UUID defined earlier, and you need to pass as arguments the characteristic’s properties. In this case, it’s: READ and WRITE.

BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                     CHARACTERISTIC_UUID,
                                     BLECharacteristic::PROPERTY_READ |
                                     BLECharacteristic::PROPERTY_WRITE
                                     );

After creating the characteristic, you can set its value with the setValue() method.

pCharacteristic->setValue("Hello World says Neil");

In this case we’re setting the value to the text “Hello World says Neil”. You can change this text to whatever your like. In future projects, this text can be a sensor reading, or the state of a lamp, for example.

Finally, you can start the service, and the advertising, so other BLE devices can scan and find this BLE device.

BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();

This is just a simple example on how to create a BLE server. In this code nothing is done in the loop(), but you can add what happens when a new client connects (check the BLE_notify example for some guidance).

ESP32 BLE Scanner

Creating an ESP32 BLE scanner is simple. Grab another ESP32 (while the other is running the BLE server sketch). In your Arduino IDE, go to File > ExamplesESP32 BLE Arduino and select the BLE_scan example.

Once the code is uploaded and you should have the two ESP32 boards powered on:

  • One ESP32 with the “BLE_server” sketch;
  • Other with ESP32 “BLE_scan” sketch.

Go to the Serial Monitor with the ESP32 running the “BLE_scan” example, press the ESP32 (with the “BLE_scan” sketch) ENABLE button to restart and wait a few seconds while it scans.

Testing the ESP32 BLE Server with Your Smartphone

Download BLE Scanner App https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner&hl=en_IN

Or use nRF Connect for Mobile App

https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en_IN

 

 

 

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

10 thoughts on “ESP32 Bluetooth Low Energy (BLE) on Arduino IDE

Leave a Reply

Your email address will not be published. Required fields are marked *