Basic/Gray/M5GO MicroSD Card related APIs and example program.
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>
#include <M5GFX.h>
#define SD_SPI_CS_PIN 4
#define SD_SPI_SCK_PIN 18
#define SD_SPI_MISO_PIN 19
#define SD_SPI_MOSI_PIN 23
void setup() {
M5.begin();
M5.Display.setTextFont(&fonts::Orbitron_Light_24);
M5.Display.setTextSize(1);
// SD Card Initialization
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)) {
// Print a message if SD card initialization failed or if the SD card does not exist.
M5.Display.print("\n SD card not detected\n");
while (1)
;
} else {
M5.Display.print("\n SD card detected\n");
}
delay(1000);
// Write TXT file
M5.Display.print("\n SD card write test...\n");
auto file = SD.open("/WriteTest.txt", FILE_WRITE, true);
if (file) {
file.print("Hello, world! \nSD card write success! \n");
file.close();
M5.Display.print(" SD card write success\n");
} else {
M5.Display.print(" Failed to create TXT file\n");
}
delay(1000);
M5.Display.print("\n SD card read test...\n");
if (SD.open("/TestPicture01.png", FILE_READ, false)) {
M5.Display.print(" PNG file 01 detected\n");
} else {
M5.Display.print(" PNG file 01 not detected\n");
}
if (SD.open("/TestPicture02.png", FILE_READ, false)) {
M5.Display.print(" PNG file 02 detected\n");
} else {
M5.Display.print(" PNG file 02 not detected\n");
}
}
void loop() {
// Read PNG file and draw picture
M5.Display.drawPngFile(SD, "/TestPicture01.png");
delay(1000);
M5.Display.drawPngFile(SD, "/TestPicture02.png");
delay(1000);
}
Prepare a MicroSD card, format it to FAT32, and place two 320*240
resolution PNG
images in the root directory, named TestPicture01.png
、TestPicture02.png
images in the root directory, named Sample Image 1 and Sample Image 2. (If the resolution of the images is not 320*240
, the program will decide the display method based on presets, which may cause display issues.)
Insert this SD card into the Basic/Gray/M5GO, ensuring the SD card's contacts face the same direction as the Fire's screen. Copy the code above into Arduino IDE, then compile and upload it to the Basic/Gray/M5GO。
This program will create a text file WriteTest.txt
on the SD card and write a string into it, then loop and display the two PNG images from the SD card.
Basic/Gray/M5GO MicroSD card section uses the built-in Arduino SD
library and the drawPngFile
function from the M5GFX
library. For more related APIs, refer to the documents below: