m5paper_sd_card_logger-8.ino
Shows how to initialize the SD card reader integrated into the M5Paper and write CSV formatted data for autonomous data logging.
Uses the standard SD library. The CS pin is fixed to GPIO 4 on M5Paper. Ensure the SD card is formatted as FAT32. The display confirms write status.
m5paper_sd_card_logger-8.ino
#include <M5Paper.h>
#include <SD.h>
#include <FS.h>
#include <time.h>
#define SD_CS 4 // Chip Select pour le slot SD du M5Paper
void setup() {
M5.begin(true, false, false);
M5.Display.init();
M5.Display.setRotation(1);
if (!SD.begin(SD_CS)) {
M5.Display.drawString("Erreur SD Card", 10, 10);
return;
}
M5.Display.drawString("SD OK", 10, 10);
M5.Display.update();
}
void loop() {
// Simuler une donnée (ex: température interne ou compteur)
int sensorValue = random(20, 30);
String logMessage = String(millis()) + "," + String(sensorValue) + "\n";
// Ouverture en append
File myFile = SD.open("log.csv", FILE_APPEND);
if (myFile) {
myFile.print(logMessage);
myFile.close();
M5.Display.clear();
M5.Display.drawString("Log: " + String(millis()), 10, 10);
M5.Display.drawString("Val: " + String(sensorValue), 10, 40);
M5.Display.drawString("OK", 10, 70);
M5.Display.update();
} else {
M5.Display.drawString("Echec Ecriture", 10, 10);
M5.Display.update();
}
delay(5000); // Enregistrer toutes les 5 secondes
}