pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

CoreS3 microSD Card

CoreS3 microSD card–related APIs and example programs.

Example Program

Build Requirements

  • M5Stack Board Manager version >= 3.2.5
  • Board option = M5CoreS3
  • M5Unified library version >= 0.2.11
  • M5GFX library version >= 0.2.18
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 51 52 53 54 55 56 57 58 59 60 61 62
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>

// ========== Correct CoreS3 SD card pin definitions ==========
#define SD_SPI_CS_PIN    4
#define SD_SPI_SCK_PIN  36
#define SD_SPI_MISO_PIN 35
#define SD_SPI_MOSI_PIN 37

void setup() {
  M5.begin();

  M5.Display.setFont(&fonts::FreeMono12pt7b);
  M5.Display.clear();

  // ========== SD card initialization (using correct pins) ==========
  SPI.begin(SD_SPI_SCK_PIN, SD_SPI_MISO_PIN, SD_SPI_MOSI_PIN, SD_SPI_CS_PIN);

  if (!SD.begin(SD_SPI_CS_PIN, SPI, 25000000)) {
    M5.Display.println("SD card not detected");
    while (1);
  } else {
    M5.Display.println("SD card detected");
  }
  delay(1000);

  // ========== Write test ==========
  M5.Display.println("SD card write test...");
  auto file = SD.open("/WriteTest.txt", FILE_WRITE, true);
  if (file) {
    file.print("Hello, world! \nSD card write success! \n");
    file.close();
    M5.Display.println("SD card write success");
  } else {
    M5.Display.println("Failed to create TXT file");
  }
  delay(1000);

  // ========== Read test ==========
  M5.Display.println("SD card read test...");

  if (SD.exists("/cores3_test_picture01.png")) {
    M5.Display.println("PNG file 01 detected");
  } else {
    M5.Display.println("PNG file 01 not detected");
  }

  if (SD.exists("/cores3_test_picture02.png")) {
    M5.Display.println("PNG file 02 detected");
  } else {
    M5.Display.println("PNG file 02 not detected");
  }
  delay(1000);
}

void loop() {
  M5.Display.drawPngFile(SD, "/cores3_test_picture01.png");
  delay(500);
  M5.Display.drawPngFile(SD, "/cores3_test_picture02.png");
  delay(500);
}

Prepare a microSD card formatted as FAT32. Place two PNG images with a resolution of 320 * 240 in the root directory, named cores3_test_picture01.png and cores3_test_picture02.png. You can also directly download cores3_test_picture01.png and cores3_test_picture02.png. If the image resolution is not 320 * 240, the program will determine the display method based on presets, which may result in abnormal display.

Insert the SD card into the CoreS3, making sure that the SD card contacts face the same direction as the CoreS3 screen. Copy the above code into the Arduino IDE, compile it, and upload it to the CoreS3.

This program will create a text file WriteTest.txt on the SD card and write a block of text to it, then continuously play the two PNG images stored on the SD card.

API

The CoreS3 microSD card functionality uses the Arduino built-in SD library and the drawPngFile function from the M5GFX library. For more related APIs, please refer to the documents below:

On This Page