🛠 Enregistreur de mouvement sur mémoire flash (LittleFS)

M5StickC Plus2 (M5Stack) — ESP32-PICO-V3-02 "stick" wearable kit (SKU K016-P2) · intermediaire · Schéma : IMG-m5stack-stick-c-plus-2-intermediaire-2-m5stick-c2-enregistreur-littlefs.svg — Coupe de la M5StickC Plus2 montrant la puce SiP ESP32-PICO-V3-02 et ses 8 Mo de flash avec le système de fichiers LittleFS, l'IMU écrivant des lignes CSV, sans emplacement carte SD.

Objectives

Build a motion data logger. Note: the M5StickC Plus2 has no SD/TF slot — data is stored in the 8 MB flash via LittleFS. We sample the IMU, log only shakes (acceleration norm above a threshold), write timestamped CSV lines using the BM8563 RTC, and read back the log via the serial port.

Hardware

Complete code

// Data-logger mouvement sur LittleFS (PAS de carte SD sur Plus2 — flash 8 Mo)
#include <M5Unified.h>
#include <LittleFS.h>

const char* FICHIER = "/mouvement.csv";
const float SEUIL = 1.6f;          // g : au-dessus = secousse
uint32_t nbLignes = 0;

void ecrireLigne(float ax, float ay, float az, float n) {
  File f = LittleFS.open(FICHIER, "a");
  if (!f) return;
  f.printf("%lu,%.3f,%.3f,%.3f,%.3f\n", (unsigned long)millis, ax, ay, az, n);
  f.close;
  nbLignes++;
}

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  Serial.begin(115200);
  if (!LittleFS.begin(true)) { M5.Display.println("LittleFS KO"); return; }
  M5.Display.setRotation(1);
  M5.Display.setTextSize(2);
  M5.Display.setTextColor(TFT_ORANGE, TFT_BLACK);
  if (M5.BtnB.isPressed) {        // B au boot = effacer le journal
    LittleFS.remove(FICHIER);
    Serial.println("Journal efface.");
  }
}

void loop {
  M5.update;
  float ax, ay, az;
  M5.Imu.getAccel(&ax, &ay, &az);
  float n = sqrtf(ax * ax + ay * ay + az * az);
  M5.Display.setCursor(0, 10);
  M5.Display.printf("lignes: %lu\n", (unsigned long)nbLignes);
  if (n > SEUIL) {
    ecrireLigne(ax, ay, az, n);
    M5.Speaker.tone(1200, 40);
  }
  if (M5.BtnA.wasPressed) {       // A = vider le journal sur le port serie
    File f = LittleFS.open(FICHIER, "r");
    if (!f) { Serial.println("journal vide"); }
    else { while (f.available) Serial.write(f.read); f.close; }
  }
  vTaskDelay(50 / portTICK_PERIOD_MS);
}

Expected verification

After uploading, shake the stick: a beep confirms each write and the "lines" counter increases. Press A then open the serial monitor at 115200 to display the CSV millis,ax,ay,az,norm. Holding B during boot resets the log.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials