m5paper_sd_logger.ino

M5Paper: IoT Development Kit with Tactile e-Ink Display · File : m5paper_sd_logger.ino · Language : Arduino · Use case : Enregistrer des données capteurs ou logs sur la carte SD via l'interface SPI.

Shows how to initialize the SD card and write data periodically for offline storage.

The M5Paper has an SD card slot. This code uses the SPI bus. Check the CS pin according to your board revision (often GPIO 33). Writing to SD is blocking, so it is spaced out to avoid freezing the interface.

m5paper_sd_logger.ino

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

// M5Paper SD Card Pins (Check specific revision, usually VSPI)
#define SD_CS  33
#define SD_MOSI 23
#define SD_CLK  18
#define SD_MISO 19

M5EPD m5;
File myFile;

void setup() {
  m5.begin();
  Serial.begin(115200);
  
  SPI.begin(SD_CLK, SD_MISO, SD_MOSI, SD_CS);
  
  if (!SD.begin(SD_CS)) {
    Serial.println("Card Mount Failed");
    m5.drawCentreString("SD Card Error", 480, 270, 4);
    m5.updateFrame();
    return;
  }
  Serial.println("SD Card mounted");
  
  // Write test
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("Logging started at: " + String(millis()));
    myFile.close();
    Serial.println("Done writing");
  }
}

void loop() {
  // Simulate sensor data logging
  static unsigned long lastLog = 0;
  if (millis() - lastLog > 5000) {
    lastLog = millis();
    myFile = SD.open("log.txt", FILE_WRITE);
    if (myFile) {
      myFile.println(millis() + "," + String(analogRead(36))); // Dummy analog read
      myFile.close();
    }
    
    m5.clear();
    m5.drawCentreString("Logged: " + String(millis()), 480, 270, 4);
    m5.updateFrame();
  }
  delay(100);
}

← Back to the board