🛠 Enregistreur de capteurs sur la flash embarquée

Arduino Nicla Sense ME · avance · Schéma : IMG-nicla-sense-me-avance-2-niclasense-enregistreur-flash.svg — Nicla Sense ME avec puce de flash SPI 2 MB mise en évidence, flèches entrantes des capteurs et sortantes d'un fichier CSV vers l'ordinateur, carte SD barrée pour signifier l'absence de slot

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials