
Arduino Quick Start
API and example programs for StackChan microSD card functionality.
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>
#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();
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);
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);
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, and 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 test_picture01.png and test_picture02.png. If the image resolution is not 320*240, the program will determine the display method based on preset settings, which may result in display abnormalities.
Insert the SD card into StackChan, making sure the SD card's contacts face the same direction as the StackChan screen. Copy the code above into the Arduino IDE, then compile and upload it to StackChan.
The program will create a text file WriteTest.txt on the SD card and write a text string to it, then cycle through the two PNG images stored on the SD card.
The StackChan microSD card section utilizes Arduino's built-in SD library and the drawPngFile function from the M5GFX library. For more related APIs, refer to the documentation below: