pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Tab5 microSD Card

Tab5 microSD card related API and example program.

Example Program

Compilation Requirements

  • M5Stack board management version >= 3.2.2
  • Board option = M5Tab5
  • M5Unified library version >= 0.2.8
  • M5GFX library version >= 0.2.11
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 <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>
#include <M5GFX.h>

#define SD_SPI_CS_PIN   42
#define SD_SPI_SCK_PIN  43
#define SD_SPI_MOSI_PIN 44
#define SD_SPI_MISO_PIN 39

void setup()
{
    M5.begin();

    M5.Display.setRotation(1);
    M5.Display.setFont(&fonts::FreeMonoBold18pt7b);

    // 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, "/tab5_test_picture_1280_x_720_01.png");
    delay(1000);
    M5.Display.drawPngFile(SD, "/tab5_test_picture_1280_x_720_02.png");
    delay(1000);
}

Prepare a microSD card, format it to FAT32 format, and place two PNG images with 720*1280 resolution in its root directory named TestPicture01.png and TestPicture02.png. (You can also directly download Sample Image 1, Sample Image 2. If the image resolution is not 720*1280, the program will decide the display mode based on preset settings, which may result in display anomalies.)

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

This program will create a text file WriteTest.txt in the SD card and write a segment of text, then loop through displaying two PNG images from the SD card.

API

Tab5 microSD card functionality uses the Arduino built-in SD library and the drawPngFile function from the M5GFX library. For more related API information, refer to the following documentation:

On This Page