m5paper_sd_log.ino

M5Paper IoT Development Kit with 4.7-inch Touchscreen e-Ink Display · File : m5paper_sd_log.ino · Language : C++ · Use case : Écrire des données de capteurs fictifs dans un fichier CSV sur la carte MicroSD.

Periodically logs random values to a CSV file on the SD card, demonstrating data persistence.

Uses the SD library. CS pin is typically GPIO4 on M5Paper. Ensure SD card is FAT32 formatted. File is opened for writing each cycle to prevent data loss on power failure.

m5paper_sd_log.ino

#include <M5EPD.h>
#include <SD.h>
#include <FS.h>

const int chipSelect = 4; // Vérifier le schéma M5Paper
File myFile;

void setup() {
  M5.begin();
  Serial.begin(115200);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Card Mount Failed");
    return;
  }
  
  myFile = SD.open("log.csv", FILE_WRITE);
  if (myFile) {
    myFile.print("Time,Value\n");
    myFile.close();
  }
}

void loop() {
  int sensorValue = random(100, 200);
  myFile = SD.open("log.csv", FILE_WRITE);
  if (myFile) {
    myFile.print(millis());
    myFile.print(",");
    myFile.println(sensorValue);
    myFile.close();
  }
  delay(5000);
}

← Back to the board