Table of Contents
๐ฆ Hardware Requirements
Required Hardware
- nRF52840 DK (or compatible board)
- USB Cable (for programming)
- Computer (Windows/Mac/Linux)
Optional Hardware
- Thread Border Router (HomePod mini, Apple TV)
- External antenna (for better range)
- Battery (for portable testing)
๐ ๏ธ nRF Connect SDK Setup
Note: This project requires nRF Connect SDK v2.5.0 or later for full Matter support.
Option 1: Install via nRF Connect for Desktop (Recommended)
- Download nRF Connect for Desktop
- Install the application
- Open nRF Connect and install "Toolchain Manager"
- In Toolchain Manager, install nRF Connect SDK v2.5.0+
- Install VS Code extension for development
Option 2: Manual Installation
# Create workspace
mkdir ~/ncs
cd ~/ncs
# Initialize west
west init -m https://github.com/nrfconnect/sdk-nrf --mr v2.5.0
# Update dependencies
west update
# Export Zephyr environment
west zephyr-export
# Install Python dependencies
pip3 install -r zephyr/scripts/requirements.txt
pip3 install -r nrf/scripts/requirements.txt
pip3 install -r bootloader/mcuboot/scripts/requirements.txt
Verify Installation
# Check west version
west --version
# Check toolchain
arm-none-eabi-gcc --version
# Test build system
west build --help
๐ Project Structure
fitness-sensor-matter/
โโโ CMakeLists.txt # Build configuration
โโโ prj.conf # Project configuration
โโโ fitness-sensor.zap # Matter cluster definitions
โโโ src/
โ โโโ main.cpp # Main application code
โ โโโ include/
โ โโโ CHIPProjectConfig.h # CHIP configuration
โโโ boards/
โ โโโ nrf52840dk_nrf52840.overlay # Board overlay
โโโ README.md
๐จ Building the Firmware
Step 1: Generate Matter Code
# Navigate to project directory
cd fitness-sensor-matter
# Generate ZAP files from Matter cluster definitions
$NCS_PATH/modules/lib/matter/scripts/tools/zap/generate.py fitness-sensor.zap
Step 2: Configure Build
The prj.conf file contains all configuration options:
# Key configurations in prj.conf:
CONFIG_CHIP=y # Enable Matter
CONFIG_BT=y # Enable Bluetooth
CONFIG_NET_L2_OPENTHREAD=y # Enable Thread
CONFIG_OPENTHREAD_MTD=y # Minimal Thread Device
CONFIG_CHIP_DEVICE_VENDOR_ID=0xFFF1 # Vendor ID
CONFIG_CHIP_DEVICE_PRODUCT_ID=0x8005 # Product ID
Step 3: Build Firmware
# Using west (recommended)
west build -b nrf52840dk_nrf52840
# Clean build
west build -b nrf52840dk_nrf52840 -p
# With CMake directly
mkdir build && cd buildcmake -GNinja -DBOARD=nrf52840dk_nrf52840 ..
ninja
# Verbose build output
west build -b nrf52840dk_nrf52840 -v
Build Output
Successful build creates:
build/zephyr/zephyr.hex- Firmware in HEX formatbuild/zephyr/zephyr.bin- Raw binarybuild/zephyr/zephyr.elf- Executable with debug info
โก Flashing the Device
Method 1: Using West (Easiest)
# Flash the built firmware
west flash
# Flash and open serial monitor
west flash && screen /dev/ttyACM0 115200
Method 2: Using nrfjprog
# Erase chip
nrfjprog --eraseall -f nrf52
# Flash firmware
nrfjprog --program build/zephyr/zephyr.hex --chiperase -f nrf52
# Reset device
nrfjprog --reset -f nrf52
Method 3: Using J-Link
# Using JLinkExe
JLinkExe -device NRF52840_XXAA -if SWD -speed 4000
# J-Link commands:
erase
loadfile build/zephyr/zephyr.hex
r
g
q
Success! If flashing succeeds, the device will reset and start running the firmware.
๐ Matter Commissioning
Prerequisites
- Thread Border Router active (HomePod mini, Apple TV, or other)
- iPhone with iOS 16.1+ OR chip-tool installed
- Device flashed and powered on
Option 1: Commission via iOS Home App
- Open Home app on iPhone
- Tap "+" โ "Add Accessory"
- Scan QR code or enter manual pairing code
- Default setup code:
20202021-3840 - Follow on-screen instructions
- Name the device (e.g., "Bike Sensor")
- Assign to room (e.g., "Home Gym")
Option 2: Commission via chip-tool (Developer)
# Commission via Thread
./chip-tool pairing ble-thread <node-id> hex:<thread-dataset> 20202021 3840
# Example with node-id 1:
./chip-tool pairing ble-thread 1 hex:0e080000000000010000... 20202021 3840
# Verify commissioning
./chip-tool onoff read on-off 1 1
Get Thread Dataset
From your Thread Border Router or using Thread tools:
# From OpenThread CLI
dataset active -x
# From Apple Home (requires developer tools)
# Or use Thread Border Router web interface
โ Verification
1. Check Serial Output
# Connect to serial console
screen /dev/ttyACM0 115200
# Expected output:
nRF52840 BLE & Matter Fitness Sensor Emulator
============================================
Bluetooth initialized
BLE advertising started
Matter initialized
Ready for commissioning!
2. Verify BLE Advertising
Using nRF Connect app or command line:
# List BLE devices (Linux)
sudo hcitool lescan
# Look for "Bike Sensor Matter"
3. Test BLE Services
Use nRF Connect mobile app to verify:
- Heart Rate Service (0x180D)
- Cycling Power Service (0x1818)
- Battery Service (0x180F)
4. Test Matter Control
# Turn sensor on
chip-tool onoff on 1 1
# Set intensity
chip-tool levelcontrol move-to-level 150 0 0 0 1 1
# Read current state
chip-tool onoff read on-off 1 1
chip-tool levelcontrol read current-level 1 1
๐ Troubleshooting
Build Errors
| Error | Solution |
|---|---|
| "West not found" | Install west: pip3 install west |
| "Board not found" | Check board name: nrf52840dk_nrf52840 |
| "Toolchain error" | Install ARM toolchain via Toolchain Manager |
| "ZAP generation failed" | Install Node.js and run generate.py again |
Flashing Issues
Device Not Found
- Check USB connection
- Verify J-Link firmware updated
- Try different USB port/cable
- Check device manager (Windows)
Flash Verification Failed
- Erase chip first:
west flash --erase - Try lower SWD speed
- Update J-Link firmware
- Check power supply
Matter Commissioning Issues
| Issue | Solution |
|---|---|
| Can't find device | Check BLE is advertising, ensure Bluetooth enabled |
| Pairing fails | Use correct code (20202021-3840), reset device |
| Thread join fails | Verify border router active, check Thread credentials |
| Device offline after pairing | Check Thread network, power cycle device |
Runtime Issues
# View detailed logs
screen /dev/ttyACM0 115200
# Enable debug logging in prj.conf:
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_MATTER_LOG_LEVEL_DBG=y
# Rebuild and reflash
west build -b nrf52840dk_nrf52840 -p
west flash
๐ Performance Optimization
Power Consumption
| Mode | Current Draw | Battery Life (1000mAh) |
|---|---|---|
| BLE Active | ~15mA | ~66 hours |
| Thread Active | ~8mA | ~125 hours |
| Both Active | ~23mA | ~43 hours |
| Sleep Mode | ~5ยตA | ~22 years |
Optimization Options
# In prj.conf for lower power:
CONFIG_BT_CONN_TX_POWER_LEVEL_0=y # Reduce TX power
CONFIG_OPENTHREAD_SED=y # Sleepy End Device
CONFIG_PM=y # Enable power management
# For better performance:
CONFIG_BT_CTLR_TX_PWR_PLUS_4=y # Max BLE power
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 # Larger stack
๐ Security Considerations
Production Deployment
Before production use:
- Change default pairing code
- Implement device attestation
- Use production certificates
- Enable secure boot
- Lock debug interfaces