🛠 Enregistreur de capteurs sur la flash embarquée
Objectives
Build a standalone datalogger without an SD card: the board features 2 MB of SPI flash (U7, MX25R1635F) connected to the ANNA-B112 module, separate from the 2 MB of QSPI flash (U2) reserved for the firmware and calibration data of the BHI260AP hub. The official "StandaloneFlashStorage" example from the Arduino_BHY2 library demonstrates how to log accelerometer, gyroscope, temperature, gas, and rotation data in CSV format directly to this flash, and then read the file back via the serial port. A typical use case for the Arduino Pro: a warehouse maturation monitoring node, offline for days.
Hardware
- Arduino Nicla Sense ME board (ABX00050) and micro-USB cable
- LiPo battery on J4 for autonomy
- Arduino_BHY2 library (file utilities are exposed by the BHY2 object)
- >
Complete Code
#include "Arduino_BHY2.h"
SensorXYZ accel(SENSOR_ID_ACC); // ID 4
Sensor temp(SENSOR_ID_TEMP); // ID 128
File logFile;
void setup {
Serial.begin(115200);
while (!Serial);
BHY2.begin;
accel.begin;
temp.begin;
// Ecriture sur la flash SPI 2 MB embarquee
logFile = BHY2.open("log.csv", O_WRONLY | O_CREAT | O_TRUNC);
if (!logFile) {
Serial.println("Ouverture flash impossible");
while (1);
}
logFile.println("millis,ax,ay,az,temp_C");
}
void loop {
static auto printTime = millis;
BHY2.update;
if (millis - printTime >= 1000) {
printTime = millis;
if (accel.dataAvailable && temp.dataAvailable) {
logFile.printf("%lu,%.3f,%.3f,%.3f,%.2f\n", millis,
accel.x, accel.y, accel.z, temp.value);
}
}
}
Expected Verification
After a few minutes of operation, the "log.csv" file contains one line per second with timestamp, three axes, and temperature. Read it back and copy it to the PC using the flash reading example (or exploit it directly as CSV in a spreadsheet). Also verify robustness: restarting the board clears the log due to O_TRUNC, which is intentional in this example.
Common Errors
- Confusing the two flashes: the 2 MB QSPI are dedicated to the BHI260AP (BHY2 firmware, BSX calibration) and are not user storage; the 2 MB SPI are the logging area.
- Writing on every loop iteration without delay: flash wear and bus saturation occur; limit the rate (1 Hz here) and prefer the hub's FIFOs for high-frequency data.
- Forgetting
BHY2.update: without the pump, no data arrives and the CSV contains only the header. - Planning for space: 2 MB seems huge, but at ~40 bytes per line, 1 Hz saturates the capacity in less than 12 hours; aggregate or compress for multi-day logging.
- >
Other tutorials for this board
Code examples for this board
led_rgb.inoArduinoimu_bhi260.inoArduinorotation_fusion.inoArduinopression_bmp390.inoArduinonose_electronique.inoArduinoboussole_bmm150.inoArduino