pdf-icon

Arduino Quick Start

2. Devices & Examples

StamPLC SD Card

StamPLC SD example program

In this example, StamPLC uses the ESP32 Arduino SD library as the SD card driver. Refer to the example below to achieve a simple SD card test. For more API details, please refer to the arduino-esp32 source code.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 * 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