🛠 Save Data to MicroSD and Display on e-paper
Objectifs
This tutorial guides you to create a simple logbook. You will learn to write text to a file on the MicroSD card and retrieve it to display on the 4.7-inch e-paper screen. This is a fundamental skill for creating autonomous devices that retain information even when powered off.
Matériel nécessaire
- M5Stack PaperDIY Assembly Kit (assembled)
- FAT32 formatted MicroSD card
- USB-C cable for power and code transfer
- Computer with Arduino IDE installed
M5UnifiedandSDlibraries (built-in or via library manager)
Étapes
- Environment Setup: Open Arduino IDE. Ensure the board is selected as "M5Stack-ATOMS3" or "ESP32-S3 Dev Module" depending on your configuration. Enable "USB CDC On Boot".
- System Initialization: Include necessary libraries. The code must initialize the M5PM1 power management, the e-paper display, and the SD reader.
#include <M5Unified.h>
#include <SD.h>
#define SD_CS 13 // Check your kit's schematic for the exact CS pin
void setup() {
M5.begin();
M5.Power.begin();
if(!SD.begin(SD_CS)) {
M5.Display.println("SD Error");
return;
}
M5.Display.setRotation(1);
}
- Data Writing: Create a function to save text. We will use a
journal.txtfile.
void saveData(String text) {
File file = SD.open("/journal.txt", FILE_APPEND);
if(file) {
file.println(text);
file.close();
}
}
- Reading and Displaying: Read the file content and display it. The ED047TC1 screen is slow; use
M5.Display.update()to refresh.
void displayData() {
M5.Display.clear();
File file = SD.open("/journal.txt", FILE_READ);
if(file) {
while(file.available()) {
M5.Display.println((char)file.read());
}
file.close();
}
M5.Display.update();
}
- Main Loop: In
loop(), call the functions to test. For example, save the current time every 10 seconds and display the journal.
void loop() {
saveData("Test : " + String(millis()));
displayData();
delay(10000);
}
Vérification
Insert the MicroSD card, upload the code, and power the kit. You should see text accumulating on the e-paper screen. Turn the kit off and on again: the data should persist, proving that writing to flash/MicroSD works correctly.
Pour aller plus loin
Explore WiFi 4 connectivity to send this data to a server. You can also use Bluetooth 5.0 LE to transfer the journal to a smartphone, turning your PaperDIY into a portable connected logbook.
Other tutorials for this board
Code examples for this board
epaper_hello_world.inoC++wifi_web_dashboard.inoC++sdcard_logger.inoC++deep_sleep_battery.inoC++ble_beacon.inoC++gpio-button-led-basic.inoC++ (Arduino)