🛠 Enregistreur de mouvement sur mémoire flash (LittleFS)
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
- 1× M5StickC Plus2 (v2)
- USB-C cable + CH9102 driver
- Arduino IDE / PlatformIO: M5Unified + LittleFS plugin (menu "ESP32 Sketch Data Upload" or
pio run -t uploadfs)
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
- Looking for an SD card slot: the Plus2 doesn't have one (unlike other ESP32 boards); everything goes through LittleFS on the internal flash.
- Forgetting to format the LittleFS partition on first boot (
LittleFS.begin(true)does this, but you also need to flash theuploadfsimage if the partition scheme has changed). - Opening/closing the file at 100 Hz: flash wear and latency explode; we only log when the threshold is exceeded.
- Writing without ever calling
f.close: lines are lost on the next restart. - Reading the log with a giant
Serial.printthat breaks the stream: send byte by byte withSerial.writeas done here.
Other tutorials for this board
Code examples for this board
hello-stick.inoArduinoboutons.inoArduinobuzzer.inoArduinoinclinometre.inoArduinohorloge.inoArduinoir-tv.inoArduino