Building a BLE Sensorhub with nRF52840 & Zephyr RTOS
Step-by-step guide to building a BLE sensorhub on the Seeed Studio XIAO nRF52840 Plus using Zephyr RTOS, from hardware overview to firmware and OTA updates.
The nRF52840 is one of the most capable Bluetooth Low Energy SoCs available today, 64 MHz Cortex-M4F, 1 MB Flash, 256 KB RAM, USB, and a hardware crypto accelerator. Paired with Zephyr RTOS and the nRF Connect SDK, it becomes a solid foundation for production IoT devices.
This tutorial walks through the full stack: hardware platform, Zephyr project setup, BLE service definition, and OTA firmware updates.
Hardware Platform
Why the nRF52840?
Compared to ESP32-based BLE solutions, the nRF52840 offers:
- Deterministic BLE timing: critical for sensor polling at fixed intervals
- Hardware crypto:AES-128/256, SHA-256, ECC in silicon
- USB 2.0 FS : no USB-UART bridge required
- Ultra-low sleep current : ~1.5 µA in System OFF
For a battery-powered sensorhub running 24/7, these tradeoffs matter.
Seeed Studio XIAO nRF52840 Plus
This project is built on the Seeed Studio XIAO nRF52840 Plus, a compact, production-ready module that handles the complexity of antenna design, power management, and USB charging out of the box.
The module provides:
- Integrated LiPo battery charger (200 mA charge current)
- 3.3V regulated supply for peripherals
- USB-C for power and DFU programming
- Onboard NFC antenna pads
- Battery voltage readout via onboard ADC divider
This lets you focus on the firmware and sensor integration rather than power rail design, a good tradeoff for rapid prototyping and short-run production.
Sensor Integration
Sensors connect via the XIAO’s I²C bus (SDA/SCL). For deep-sleep power savings, a GPIO-controlled load switch on the sensor rail cuts power to all peripherals when the MCU enters System OFF, critical for multi-week battery runtimes.
Zephyr Project Setup
west init
west init -m https://github.com/nrfconnect/sdk-nrf --mr v2.7.0 ncs
cd ncs && west update
Project Structure
app/
├── CMakeLists.txt
├── prj.conf
├── boards/
│ └── xiao_ble.conf
└── src/
├── main.c
├── ble/
│ ├── ble_service.c
│ └── ble_service.h
└── sensors/
├── bme280.c
└── bme280.h
prj.conf (minimal BLE config)
# BLE
CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="NeuLabs-Sensorhub"
CONFIG_BT_DEVICE_APPEARANCE=833
# BLE Security
CONFIG_BT_SMP=y
CONFIG_BT_PRIVACY=y
# Power Management
CONFIG_PM=y
CONFIG_PM_DEVICE=y
# Logging
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=3
Defining a Custom BLE Service
Zephyr uses a macro-based GATT service definition. Here’s a minimal sensorhub service:
// UUID: custom 128-bit base + characteristic offsets
#define SENSORHUB_UUID_BASE \
BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0xABCD, 0x1234567890AB)
#define SENSORHUB_UUID_SERVICE \
BT_UUID_DECLARE_128(SENSORHUB_UUID_BASE)
// Temperature characteristic (notify)
BT_GATT_SERVICE_DEFINE(sensorhub_svc,
BT_GATT_PRIMARY_SERVICE(SENSORHUB_UUID_SERVICE),
BT_GATT_CHARACTERISTIC(BT_UUID_TEMPERATURE,
BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_NONE,
NULL, NULL, NULL),
BT_GATT_CCC(temp_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
);
OTA Updates with MCUboot + SMP
The nRF Connect SDK integrates MCUboot for dual-bank OTA. Enable in prj.conf:
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_MCUBOOT_IMG_MANAGER=y
CONFIG_IMG_MANAGER=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_STREAM_FLASH=y
Then use mcumgr CLI or nRF Connect app to push firmware over BLE.
Questions? Reach out via the contact form or LinkedIn.
Michael Neumüller
Embedded & hardware engineer · Bern