🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    SD mount fail on M5Stack basic

    Arduino
    1
    2
    3.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      cepics
      last edited by

      Hi all, I have problems with lovyan launcher... now the app doesn,t open the sd of the stack basic..

      when I burn lovyan launcher with M5burner in a M5stack basic, the sd fail... same proces on a M5Stack gray, works...

      on a basic unit, with this sketch, sd fail!!

      /*
      *******************************************************************************
      * Copyright (c) 2022 by M5Stack
      *                  Equipped with M5Core sample source code
      *                          配套  M5Core 示例源代码
      * Visit for more information: https://docs.m5stack.com/en/core/gray
      * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
      *
      * Describe: TF Card. TF卡
      * Date: 2022/3/25
      *******************************************************************************
          In this example, we will detect the existence of a file and perform read and
          write operations on it
          在这个示例中,我们将会检测某文件是否存在,并进行读写文件操作
      */
      
      #include <M5Stack.h>
      
      void setup() {
          M5.begin();
          if (!SD.begin()) {  // Initialize the SD card. 初始化SD卡
              M5.Lcd.println(
                  "Card failed, or not present");  // Print a message if the SD card
                                                   // initialization fails or if the
                                                   // SD card does not exist
                                                   // 如果SD卡初始化失败或者SD卡不存在,则打印消息
              while (1)
                  ;
          }
          M5.Lcd.println("TF card initialized.");
          if (SD.exists("/hello.txt")) {  // Check if the "/hello.txt" file
                                          // exists.查看是否存在"/hello.txt"文件
              M5.Lcd.println("hello.txt exists.");
          } else {
              M5.Lcd.println("hello.txt doesn't exist.");
          }
          M5.Lcd.println("Creating hello.txt");
          File myFile = SD.open("/hello.txt",
                                FILE_WRITE);  // Create a new file "/hello.txt".
                                              // 创建一个新文件"/hello.txt"
          if (myFile) {  // If the file is open, then write to it.
                         // 如果文件打开,则进行写入操作
              M5.Lcd.println("Writing to test.txt...");
              myFile.println("SD test.");
              myFile.close();  // Close the file. 关闭文件
              M5.Lcd.println("done.");
          } else {
              M5.Lcd.println("error opening test.txt");
          }
          delay(500);
          myFile = SD.open("/hello.txt",
                           FILE_READ);  // Open the file "/hello.txt" in read mode.
                                        // 以读取模式打开文件"/hello.txt"
          if (myFile) {
              M5.Lcd.println("/hello.txt Content:");
              // Read the data from the file and print it until the reading is
              // complete. 从文件里读取数据并打印到串口,直到读取完成.
              while (myFile.available()) {
                  M5.Lcd.write(myFile.read());
              }
              myFile.close();
          } else {
              M5.Lcd.println("error opening /hello.txt");  // If the file is not open.
                                                           // 如果文件没有打开
          }
      }
      
      void loop() {
      }
      

      .... on basic unit with this sketch, sd works

      // Libraries for SD card
      #include "FS.h"
      #include <SD.h>
      //#include "mySD.h"
      #include <SPI.h>
      
      
      // Define CS pin for the SD card module
      #define SD_MISO     19
      #define SD_MOSI     23
      #define SD_SCLK     18
      #define SD_CS       4
      SPIClass sdSPI(VSPI);
      
      String dataMessage;
      
      void setup() {
        // Start serial communication for debugging purposes
        Serial.begin(115200);
       
        // Initialize SD card
        //SD.begin(SD_CS);  
        sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
        if(!SD.begin(SD_CS, sdSPI)) {
          Serial.println("Card Mount Failed");
          return;
        }
        Serial.println("1");
        uint8_t cardType = SD.cardType();
        if(cardType == CARD_NONE) {
          Serial.println("No SD card attached");
          return;
        }
        Serial.println("Initializing SD card...");
        if (!SD.begin(SD_CS)) {
          Serial.println("ERROR - SD card initialization failed!");
          return;    // init failed
        }
        Serial.println("2");
        // If the data.txt file doesn't exist
        // Create a file on the SD card and write the data labels
        File file = SD.open("/data1.txt");
        if(!file) {
          Serial.println("File doens't exist");
          Serial.println("Creating file...");
          writeFile(SD, "/data1.txt", "Reading ID, Date, Hour, Temperature \r\n");
        }
        else {
          Serial.println("File already exists");  
        }
        file.close();
        logSDCard();
        
      }
      
      void loop() {
        // The ESP32 will be in deep sleep
        // it never reaches the loop()
      }
      
      // Write the sensor readings on the SD card
      void logSDCard() {
        //dataMessage = String(readingID) + "," + String(dayStamp) + "," + String(timeStamp) + "," + 
        //              String(temperature) + "\r\n";
        dataMessage = "Hello World \n";
        Serial.print("Save data: ");
        Serial.println(dataMessage);
        appendFile(SD, "/data1.txt", dataMessage.c_str());
      }
      
      // Write to the SD card (DON'T MODIFY THIS FUNCTION)
      void writeFile(fs::FS &fs, const char * path, const char * message) {
        Serial.printf("Writing file: %s\n", path);
      
        File file = fs.open(path, FILE_WRITE);
        if(!file) {
          Serial.println("Failed to open file for writing");
          return;
        }
        if(file.print(message)) {
          Serial.println("File written");
        } else {
          Serial.println("Write failed");
        }
        file.close();
      }
      
      // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
      void appendFile(fs::FS &fs, const char * path, const char * message) {
        Serial.printf("Appending to file: %s\n", path);
      
        File file = fs.open(path, FILE_APPEND);
        if(!file) {
          Serial.println("Failed to open file for appending");
          return;
        }
        if(file.print(message)) {
          Serial.println("Message appended");
        } else {
          Serial.println("Append failed");
        }
        file.close();
      }
      

      maybe I have to change some pins assignement some where??

      tnks a lot!!

      1 Reply Last reply Reply Quote 0
      • C
        cepics
        last edited by

        the first sketch works, with M5Stack basic, if I modify the M5Stack.cpp library from:

        // TF Card
        if (SDEnable == true) {
            SD.begin(TFCARD_CS_PIN, SPI, 40000000);
        }
        

        to

        // TF Card
        if (SDEnable == true) {
            SD.begin(TFCARD_CS_PIN, SPI, 10000000);
        }
        
        1 Reply Last reply Reply Quote 1
        • First post
          Last post