How to Use BLE with Arduino: Simple Guide and Example
To use
BLE with Arduino, you need a BLE-capable board like the Arduino Nano 33 BLE or a BLE module like the HM-10. Use the ArduinoBLE library to create BLE services and characteristics, then write code to advertise and communicate wirelessly.Syntax
The basic syntax to use BLE with Arduino involves initializing the BLE device, creating services and characteristics, starting advertising, and handling connections.
- BLE.begin(): Starts the BLE hardware.
- BLEService: Defines a service with a unique UUID.
- BLECharacteristic: Defines data points within a service.
- BLE.advertise(): Starts advertising the BLE device.
- BLE.poll(): Checks for BLE events like connections.
cpp
void setup() { if (!BLE.begin()) { while (1); } BLEService myService("12345678-1234-5678-1234-56789abcdef0"); BLECharacteristic myCharacteristic("abcdefab-1234-5678-1234-56789abcdef0", BLERead | BLEWrite, 20); BLE.addService(myService); myService.addCharacteristic(myCharacteristic); BLE.advertise(); } void loop() { BLE.poll(); }
Example
This example shows how to create a BLE peripheral on an Arduino Nano 33 BLE that advertises a service with one characteristic. It allows a connected device to read and write a string.
cpp
#include <ArduinoBLE.h> BLEService myService("12345678-1234-5678-1234-56789abcdef0"); BLECharacteristic myCharacteristic("abcdefab-1234-5678-1234-56789abcdef0", BLERead | BLEWrite, 20); void setup() { Serial.begin(9600); while (!Serial); if (!BLE.begin()) { Serial.println("Starting BLE failed!"); while (1); } BLE.setLocalName("ArduinoBLEDevice"); BLE.setAdvertisedService(myService); myService.addCharacteristic(myCharacteristic); BLE.addService(myService); myCharacteristic.writeValue("Hello BLE"); BLE.advertise(); Serial.println("BLE device is now advertising"); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while (central.connected()) { if (myCharacteristic.written()) { String value = myCharacteristic.value(); Serial.print("Characteristic written: "); Serial.println(value); } } Serial.print("Disconnected from central: "); Serial.println(central.address()); } }
Output
BLE device is now advertising
Connected to central: XX:XX:XX:XX:XX:XX
Characteristic written: YourData
Disconnected from central: XX:XX:XX:XX:XX:XX
Common Pitfalls
Common mistakes when using BLE with Arduino include:
- Not calling
BLE.begin()before using BLE functions. - Forgetting to add services and characteristics before advertising.
- Using incorrect UUID formats (must be 128-bit or standard 16-bit UUIDs).
- Not handling BLE connections properly in the
loop()function. - Trying to use BLE on boards without BLE hardware support.
cpp
/* Wrong: Advertising before adding services */ void setup() { BLE.begin(); BLE.advertise(); // Advertising started too early BLEService myService("12345678-1234-5678-1234-56789abcdef0"); BLE.addService(myService); } /* Right: Add services before advertising */ void setup() { BLE.begin(); BLEService myService("12345678-1234-5678-1234-56789abcdef0"); BLE.addService(myService); BLE.advertise(); }
Quick Reference
Remember these key points when using BLE with Arduino:
- Use
ArduinoBLElibrary for easy BLE support. - Initialize BLE with
BLE.begin(). - Create services and characteristics with unique UUIDs.
- Start advertising after adding services.
- Handle connections and data in the
loop()function. - Use a BLE-capable board or module.
Key Takeaways
Use a BLE-capable Arduino board or BLE module to enable Bluetooth Low Energy.
Initialize BLE with BLE.begin() before creating services and advertising.
Define services and characteristics with proper UUIDs to organize data.
Start advertising only after adding all services and characteristics.
Handle BLE connections and data updates inside the loop function.