Pico 2: a 400 KB black-box recorder in the RP2350's SRAM
A Raspberry Pi Pico 2 samples a sensor 10 000 times a second on a timer interrupt and keeps the last 20 seconds, 400 KB of readings, in its 520 KB of SRAM; a…
Industrial · Intermediate
In a real product: Machine black-box recorder (Industrial): After a machine trips, the recorder still holds the seconds before it, so the fault can be seen and not guessed.
A black-box recorder writes a signal into a ring buffer without end, the newest reading over the oldest, so it always holds the last stretch of time. How long a stretch depends on the RAM. The Pico's RP2040 has 264 KB of SRAM; the Pico 2's RP2350 has 520 KB. This sketch keeps 20 seconds of a sensor on GP26 at 10 000 readings a second: 200 000 readings of 2 bytes, 400 000 bytes in one array, which a Pico cannot even link. A timer interrupt takes the readings, and a button on GP14 prints the minimum, maximum, mean and each second's average of what the buffer holds.
What you'll learn
- A ring buffer indexed by a running count:
ring[taken % N], with no separate head and tail to keep in step - Sampling on a timer interrupt (
add_repeating_timer_us), so the rate stays exact whateverloop()is doing - Reading the ADC with the Pico SDK's
adc_init,adc_gpio_init,adc_select_inputandadc_read - What the build's memory report says, and what the linker does when the RAM runs out
How it works
static uint16_t ring[200000] is in .bss, the zeroed RAM the start-up code clears, so it costs no flash. The build reports Global variables use 409864 bytes (78%) of dynamic memory: the ring and the core's own buffers, leaving about 112 KB for the heap and the stacks. For a Pico the same sketch stops at the linker: the RAM would end at 0x20064498, past the RP2040's 256 KB of main SRAM at 0x20040000 (cannot move location counter backwards).
add_repeating_timer_us(-100, sample, ...) calls sample() every 100 µs from the timer's interrupt. The minus sign means 100 µs from one call's start to the next, not from its end, so the rate does not drift. sample() stores one 12-bit reading and adds one to taken, which is volatile because loop() reads it while the interrupt changes it.
Each second loop() prints how many readings were taken; a press of the button prints the summary. With the knob at a third of its turn:
ring buffer: 400000 bytes (200000 samples, 20 s at 10000 Hz)
heap: 114272 of 114424 bytes free
t=1000 ms 9800 samples taken newest 1201
t=2000 ms 19801 samples taken newest 1201
--- the last 2.9 s (29403 samples)
min 1201 max 1201 mean 1201.0
s: 1201 1201 1201
The first second shows 9800 readings rather than 10 000, as the timer starts after the USB port opens. After 20 s the count goes on past 200 000 and the oldest readings are written over.
Try this
- Turn the knob while it runs, then press the button: the per-second averages show when you turned it.
- Freeze the recording on the press (stop the timer with
cancel_repeating_timer) so a slowreport()reads a buffer that is not changing. - Make
N260 000 and build again: the build fails, as there is no RAM left for the stacks and the heap.
sketch.ino
// Raspberry Pi Pico 2 (Arduino core): a black-box recorder that keeps the last 20 s of a sensor on GP26 at 10 000
// samples a second, 400 KB of samples in the RP2350's 520 KB of SRAM. A button on GP14 prints what it holds.
#include <hardware/adc.h>
const int BUTTON = 14;
const uint32_t RATE = 10000; // samples a second
const uint32_t SECONDS = 20;
const uint32_t N = RATE * SECONDS; // 200 000 samples
// 400 000 bytes: more than the whole SRAM of a Pico (264 KB), so the sketch does not even link for one.
static uint16_t ring[N];
volatile uint32_t taken = 0; // samples taken since the start: the newest is ring[(taken - 1) % N]
repeating_timer_t timer;
// Every 100 us, on the timer's interrupt: one 12-bit reading into the ring.
bool sample(repeating_timer_t *) {
ring[taken % N] = adc_read();
taken = taken + 1;
return true;
}
void report() {
uint32_t end = taken, count = end < N ? end : N;
Serial.printf("--- the last %.1f s (%lu samples)\n", count / (float)RATE, count);
uint32_t lo = 4095, hi = 0; uint64_t total = 0;
for (uint32_t i = end - count; i != end; i++) {
uint16_t v = ring[i % N];
lo = v < lo ? v : lo; hi = v > hi ? v : hi; total += v;
}
Serial.printf("min %lu max %lu mean %.1f\n", lo, hi, (double)total / count);
// Each second's average, oldest first.
for (uint32_t s = 0; s * RATE < count; s++) {
uint32_t sum = 0, from = end - count + s * RATE, n = min(RATE, count - s * RATE);
for (uint32_t i = from; i != from + n; i++) sum += ring[i % N];
Serial.printf("%s%lu", s ? " " : "s: ", sum / n);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 3000) delay(10); // the USB port open (up to 3 s), so the first lines are seen
pinMode(BUTTON, INPUT_PULLUP);
adc_init();
adc_gpio_init(26);
adc_select_input(0);
Serial.printf("ring buffer: %u bytes (%lu samples, %lu s at %lu Hz)\n", sizeof ring, N, SECONDS, RATE);
Serial.printf("heap: %d of %d bytes free\n", rp2040.getFreeHeap(), rp2040.getTotalHeap());
add_repeating_timer_us(-(int64_t)(1000000 / RATE), sample, nullptr, &timer);
}
uint32_t shown = 0;
bool wasPressed = false;
void loop() {
bool pressed = digitalRead(BUTTON) == LOW;
if (pressed && !wasPressed) report();
wasPressed = pressed;
if (millis() - shown >= 1000) {
shown = millis();
uint32_t t = taken;
Serial.printf("t=%lu ms %lu samples taken newest %u\n", shown, t, t ? ring[(t - 1) % N] : 0);
}
delay(20);
}