Projects / Data logger
STM32 · about 10 hours · 4 phases
Build a data logger on an STM32
A data logger wakes up on time, takes a reading, and keeps it somewhere safe until someone asks for it. Weather stations, fridges in a vaccine store and the black box in a lorry all do this. On a microcontroller, a tiny computer on a single chip, there is no operating system to lean on. We write the code that runs when the power comes on, we decide where every byte lives, and we find out what happens when the power goes off in the middle of a write. In about 10 hours we will build a logger for the temperature sensor inside an STM32 chip, from the first instruction after reset to a log in flash that survives a power cut.
You can start without a board. QEMU, a free emulator, pretends to be an STM32F405, and every step runs there. When you have a Nucleo-F401RE, a cheap ST development board, the same code runs on it with one switch. You work in four phases of about 2.5 hours each, and every phase ends with something you can run and look at. By the end you will have:
- firmware you wrote from nothing, with no vendor library: a linker script, startup code and a serial port driver,
- a millisecond clock and a reading from the chip's own temperature sensor, turned into degrees with integer maths,
- readings taken on time inside an interrupt, passed to the main loop through a queue, and a serial console to ask for them,
- a log in flash that keeps every record it promised to keep, checked by a test that cuts the power a thousand times.
You need to be able to read and write C: pointers, structs, bit operations such as |= and &, and compiling with gcc. You do not need to have touched a microcontroller before.
The four phases
Boot a chip from nothing
You end with firmware you wrote from the first instruction, built with your own Makefile, printing logger: booted in QEMU.
- 1.1 What the chip does at power-on20 min
- 1.2 Tell the linker where memory is25 min
- 1.3 Write the startup code25 min
- 1.4 Turn on the parts you use20 min
- 1.5 Talk over the serial port35 min
- 1.6 Build it and run it25 min
Keep time and take a reading
You end with a reading from the chip's temperature sensor every second, with a timestamp, turned into degrees by code you tested on your laptop first.
- 2.1 Print numbers, tested on your laptop30 min
- 2.2 A millisecond clock30 min
- 2.3 Read the temperature sensor35 min
- 2.4 From counts to degrees30 min
- 2.5 One reading a second25 min
Sample on time, whatever else is going on
You end with readings taken inside an interrupt and queued for the main loop, a console that answers status, dump, rate and live, and a test that makes the logger drop readings on purpose and counts every one it lost.
- 3.1 A queue with no locks35 min
- 3.2 Take readings in the interrupt30 min
- 3.3 Receive by interrupt30 min
- 3.4 A console25 min
- 3.5 Make it drop readings on purpose30 min
Survive a power cut
You end with a log in flash that survives a reset, and a test on your laptop that cuts the power at a thousand random moments and shows the log never loses a reading it said it saved.
- 4.1 How flash behaves20 min
- 4.2 A flash you can test20 min
- 4.3 The log itself40 min
- 4.4 Pull the plug a thousand times30 min
- 4.5 Wire it in, and reset QEMU20 min
- 4.6 The real flash, on the board20 min