๐Ÿ”ง nRF52840 Hardware Setup

Complete guide to firmware setup with BLE and Matter support

๐Ÿ“ฆ 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)

  1. Download nRF Connect for Desktop
  2. Install the application
  3. Open nRF Connect and install "Toolchain Manager"
  4. In Toolchain Manager, install nRF Connect SDK v2.5.0+
  5. 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 build
cmake -GNinja -DBOARD=nrf52840dk_nrf52840 ..
ninja
# Verbose build output
west build -b nrf52840dk_nrf52840 -v

Build Output

Successful build creates:

โšก 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

Option 1: Commission via iOS Home App

  1. Open Home app on iPhone
  2. Tap "+" โ†’ "Add Accessory"
  3. Scan QR code or enter manual pairing code
  4. Default setup code: 20202021-3840
  5. Follow on-screen instructions
  6. Name the device (e.g., "Bike Sensor")
  7. 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:

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

๐ŸŽ“ Next Steps

1. Build iOS App

Create control app with HomeKit integration

iOS Guide โ†’

2. Test Workouts

Run automated test scenarios

Testing Guide โ†’

3. Zwift Integration

Connect to Zwift and start riding

System Guide โ†’