🛠 Enregistreur de capteurs sur carte microSD (slot interne)

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · avance · Schéma : IMG-m5stack-core-esp32-avance-2-m5stack-core-esp32-enregistreur-sd.svg — M5Stack Core de trois quarts avec trappe arrière, carte microSD insérée dans le slot interne (CS GPIO4, max 16 Go FAT32), écran affichant le nombre de lignes enregistrées et fichier CSV sur la carte

Objectives

Hardware

Complete code

#include <M5Unified.h>
#include <SD.h>

const int PIN_TF_CS = 4;      // CS du slot TF interne
const int PIN_CAPTEUR = 36;   // ADC1, PORT.B
File journal;
uint32_t nbLignes = 0;

void setup {
  Serial.begin(115200);
  auto cfg = M5.config;
  M5.begin(cfg);
  M5.Display.setTextSize(2);
  if (!SD.begin(PIN_TF_CS)) {
    M5.Display.println("Carte SD absente");
    M5.Display.println("ou pas en FAT32");
    while (true) vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
  journal = SD.open("mesures.csv", FILE_APPEND);
  if (!journal) { M5.Display.println("Ouverture fichier KO"); while (true); }
  if (journal.size == 0) journal.println("millis,adc36,batterie");
  Serial.println("Logger SD pret");
}

void loop {
  M5.update;
  if (M5.BtnA.wasPressed) {
    journal.printf("%lu,%d,%d\n", millis,
                   analogRead(PIN_CAPTEUR), M5.Power.getBatteryLevel);
    journal.flush;                       // ne depend pas de la cache
    nbLignes++;
    M5.Speaker.beep;
  }
  if (M5.BtnB.wasPressed) { journal.close; esp_restart; }
  M5.Display.fillRect(0, 30, 320, 60, TFT_BLACK);
  M5.Display.setCursor(10, 35);
  M5.Display.printf("Lignes : %lu", nbLignes);
  vTaskDelay(50 / portTICK_PERIOD_MS);
}

Expected verification

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials