🛠 Diffuser ses capteurs en BLE avec ArduinoBLE
Objectives
Transform the Nicla Sense ME into a Bluetooth Low Energy peripheral that publishes its sensor temperature via a standard characteristic, using the ArduinoBLE library (v2.1.0, which explicitly lists this board). Hardware precision: BLE is not handled by an STM32WB55 as on other Arduino boards, but by the u-blox ANNA-B112 module, a SiP package that actually contains... an nRF52832 Nordic, which serves as the board's main microcontroller (the module runs on a 1.8 V rail; Bluetooth 5.0 according to the module datasheet, versus "4.2" on the store page, a mention considered obsolete). This same nRF52832 executes your sketch: BLE is therefore native, with no coprocessor.
Hardware
- Arduino Nicla Sense ME board (ABX00050) and micro-USB cable
- Arduino_BHY2 (temperature sensor) and ArduinoBLE (BLE service) libraries
- Smartphone with the nRF Connect app (or Arduino's WebBLE tool) to scan and connect
Complete Code
#include "Arduino_BHY2.h"
#include <ArduinoBLE.h>
Sensor temp(SENSOR_ID_TEMP);
BLEService serviceEnv("181A"); // service Environmental Sensing
BLEFloatCharacteristic tempChar("2A6E", BLERead | BLENotify); // Temperature
void setup {
Serial.begin(115200);
BHY2.begin;
temp.begin;
if (!BLE.begin) {
Serial.println("Demarrage BLE impossible");
while (1);
}
BLE.setLocalName("NiclaTemp");
BLE.setAdvertisedService(serviceEnv);
serviceEnv.addCharacteristic(tempChar);
BLE.addService(serviceEnv);
BLE.advertise;
Serial.println("Peripheral BLE en attente de connexion");
}
void loop {
static auto printTime = millis;
BHY2.update; // pompe les capteurs
BLE.poll; // sert la pile BLE
if (millis - printTime >= 2000) {
printTime = millis;
if (temp.dataAvailable) {
tempChar.writeValue(temp.value);
Serial.println(String("Temperature diffusee : ") + String(temp.value, 2));
}
}
}
Expected Verification
With nRF Connect, the "NiclaTemp" peripheral is visible during scanning; upon connecting, you will find service 0x181A and characteristic 0x2A6E, whose value tracks the board's temperature (enable notify, then blow on the sensor). The module's advertised range reaches 160 m in open conditions (+5 dBm EIRP).
Common Errors
- Forgetting
BLE.pollorBHY2.updatein loop: one freezes the BLE stack, the other cuts off sensor data, causing the central to read a static value. BLE.beginfails if another radio initialization is already in progress; cleanly restart the board after a sketch using BLE.- Relying solely on the advertising name: after connection, some apps do not display short UUIDs; verify by exploring the full service.
- BLE connectivity issues already reported on this board (Arduino forum): test with a second phone before suspecting the sketch.
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_bhi260.inoArduinorotation_fusion.inoArduinopression_bmp390.inoArduinonose_electronique.inoArduinoboussole_bmm150.inoArduino