pdf-icon

Arduino 上手教程

SD Card

StamPLC SD 案例程序

本案例中,StamPLC使用ESP32 Arduino SD库作为屏幕驱动, 参考下方案例即可实现简单的显示, 获取更多API内容可以参考 arduino-esp32 源码。
/*
 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 *
 * SPDX-License-Identifier: MIT
 */
#include <Arduino.h>
#include <M5StamPLC.h>
#include <SD.h>

void setup()
{
    delay(3000);

    /* Enable SD card */
    auto config         = M5StamPLC.config();
    config.enableSdCard = true;
    M5StamPLC.config(config);
    M5StamPLC.begin();
}

void loop()
{
    static int count = 0;

    // Write file
    printf("\nSD card write test\n");
    auto file = SD.open("/test.txt", FILE_WRITE, true);
    if (file) {
        file.printf("Hello, World! Count: %d\n", count);
        file.close();
        printf("SD card write success\n");
    } else {
        printf("Failed to open file\n");
    }

    // Read file
    printf("\nSD card read test\n");
    file = SD.open("/test.txt");
    if (file) {
        printf("SD card read success:\n");
        while (file.available()) {
            printf("%c", file.read());
        }
    } else {
        printf("Failed to open file\n");
    }

    count++;
    delay(1000);
}
On This Page