flowchart LR
subgraph Sources["Event sources"]
BTN["Button interrupt"]
TMR["Timer"]
BLE["BLE callback"]
end
BTN -->|"event_post()"| Q[("Event queue<br/>(FreeRTOS Queue)")]
TMR -->|"event_post()"| Q
BLE -->|"event_post()"| Q
Q -->|"event_wait()"| LOOP["Main loop"]
LOOP --> TBL["State transition table<br/>handler_table[state][event]"]
TBL --> H["Handler<br/>processes and returns the next state"]
H -->|"next state"| LOOP
nRF52840 Event-Driven Firmware Hands-on
Organizing buttons, timers, BLE and sensors with an event queue and a state transition table
Introduction
English version — machine-assisted translation (by generative AI, Anthropic Claude) of the original Japanese hands-on. 日本語版はこちら.
A microcontroller program at first works fine if you just line up the processing inside loop(). However, as features grow, such as
- wanting to start a measurement when a button is pressed
- wanting to read a sensor every 100 ms
- wanting to receive commands from a smartphone over BLE
- wanting the same button to mean different things while measuring and while idle,
if statements and flags get tangled together, and you lose track of “in which state, when what happens, what to do”.
In this hands-on, using Nordic Semiconductor’s BLE microcontroller nRF52840 (Seeed Studio XIAO nRF52840), we build firmware with an event-driven + state transition table design in which
- all occurrences — interrupts, timers, BLE and so on — are pushed onto a queue as “events”,
- the main loop takes out events one at a time, and
- selects and runs a process (handler) from a “state × event” table (state transition table).
Sample firmware
This hands-on comes with sample firmware that you can build and flash as is.
firmware.zip — PlatformIO project (two environments: one for the XIAO nRF52840 Sense and one for the plain XIAO nRF52840 without Sense)
| Feature | Details |
|---|---|
| States | Idle (WAIT) / Measuring (MEAS) |
| Button | Short press while idle → start measurement; long press (1 s or more) while measuring → stop |
| Timers | Read acceleration every 100 ms; aggregate every 1 s and notify over BLE |
| BLE | Receives the commands “get device info”, “start/stop measurement” and “get latest value” from a smartphone or PC |
| LED | Changes color and blink pattern according to the state and the connection status |
| Browser | You can operate the device from this page via the BLE console in Chapter 9 (Chrome / Edge) |
The code in each chapter displays the source files of this sample firmware as they are.
Who this is for
- Those who have blinked an LED or read a sensor with Arduino
- Those who know the basics of C/C++ (functions, structs,
enum, a first taste of pointers) - Those who have struggled with a
loop()that kept getting longer
What you need
| Item | Notes |
|---|---|
| Seeed Studio XIAO nRF52840 Sense | The XIAO nRF52840 without Sense also works (acceleration becomes dummy data) |
| USB Type-C cable | One that supports data transfer |
| 1 tactile switch, jumper wires | Connect between D8 and GND |
| PC | VS Code + PlatformIO |
| Chrome or Edge (PC / Android) | For the BLE console in Chapter 9 (Safari on iPhone does not support Web Bluetooth) |
Chapters
| Chapter | Content | Approx. time |
|---|---|---|
| 1 | Development environment and the nRF52840 | 30 min |
| 2 | Why go event-driven? (a state machine simulator that runs in the browser) | 20 min |
| 3 | The event queue | 30 min |
| 4 | The state transition table | 30 min |
| 5 | Event source ①: button interrupt | 25 min |
| 6 | Event source ②: timer | 15 min |
| 7 | Event source ③: BLE | 30 min |
| 8 | Sensor and LED display | 20 min |
| 9 | Operating from the browser: Web Bluetooth console | 20 min |
| 10 | Design tips and common pitfalls | 30 min |
| 11 | Review questions and further exercises | 30 min |
Key-point slides for lectures are here.