Micro SD lose format during simple read/write test

Hi all, I'm doing a 'simple' test to check my SD setup stability. I'm just reading and writing repeatedly forever and printing the results to the Serial port. At the beginning it works, but at random times the read/write test fail. Resetting the Arduino does not fix the problem, only formatting the SD in the computer.

Sample code:

// Global libs
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

// Arduino Mega
#define SD_CS_PIN     53
#define SD_SCK_PIN    52
#define SD_MOSI_PIN   51
#define SD_MISO_PIN   50
#define BAUD_RATE     115200

// globals
String TEST_FILE = "TEST.TXT";
String NOT_TEST_FILE = "NOT_TEST.TXT";

void setup() {

  // Serial
  Serial.begin(BAUD_RATE);
  while (!Serial) {
      Serial.println("Waiting for Serial!");
      delay(1000);
  }

  // Init SD
  while (true) {
    Serial.print("Initializing SD card...");
    if (!SD.begin(SPI_QUARTER_SPEED, SD_CS_PIN)) {
        Serial.println("Card failed, or not present");
        delay(1000);
    } else {
        Serial.println("card initialized.");
        break;
    }
  }

}

void loop() {

  // SD.exists tests
  if (SD.exists(TEST_FILE)){
      Serial.println("TEST.TXT File exist.");
  } else {
      Serial.println("TEXT.TXT File do not exist.");
  }
  if (SD.exists(NOT_TEST_FILE)){
      Serial.println("NOT_TEST.TXT File exist.");
  } else {
      Serial.println("NOT_TEST.TXT File do not exist.");
  }

  // SD.read test
  File RIO = SD.open(TEST_FILE, FILE_READ);
  char c = '0';
  if (RIO) {
      Serial.print("Read: ");
      while(RIO.available()){
          c = RIO.read(); 
          Serial.print(c);
      }
      Serial.println();
  } else {
    Serial.println("Reading failed!!!");
  }
  RIO.close();

  // SD.print test
  File WIO = SD.open(TEST_FILE, FILE_WRITE);
  if (WIO) {
      WIO.print("GATITOS"); 
      // delay(5);
      Serial.println("Writing succeed!!!");
  } else {
      Serial.println("Writing failed!!!");
  }
  WIO.close();

  delay(1000);
}

A typical output is like this:

---- Opened the serial port /dev/tty.usbmodem14101 ----
Initializing SD card...
card initialized.
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
//
// SOME TIME LATER ...
//
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Read: GATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOSGATITOS
Writing succeed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Reading failed!!!
Writing failed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Reading failed!!!
Writing failed!!!
TEST.TXT File exist.
NOT_TEST.TXT File do not exist.
Reading failed!!!
Writing failed!!!
TEST.TXT File exist.

Hardware:

  • Arduino mega (elego)
  • 4GB micro SD (Cloudisk)
  • Micro SD Card Adapter (HW-125)

I have several SDs and Adapters and the problem persist with different of them.

What am I doing wrong?

Thank you in advance...

Opening an SD file, writing one line, and closing it again is a common beginner mistake, and a very slow operation that requires a huge amount of work for the card.

As you have discovered, doing so with the Arduino SD library vastly increases the error rate, as well as the current consumption.

Members of this forum tend to agree that the Arduino SD library is not very reliable, but no one seems to know what the problem is. Other platforms, like Raspberry Pi, read and write SD cards with orders of magnitude more reliability.

1 Like

Thanks, for the replay. What is a possible solution?

It should help a lot to avoid opening the file, writing a tiny bit of data, and closing it again.

Do you know of any production ready open source example where I can see how they do this?

Open the file once, say in setup(), collect and write data until done, then close the file.

By the way, do not use Strings with AVR-base Arduinos. String operations cause memory problems and program crashes.

1 Like

You are powering the HW-125 with 5V at the module's VCC pin?

Try:-

Yes, and Im powering the Arduino from the PC.

Sorry, I didn't found a link to any open software there...

Please describe what you actually want to do.

Sure, I am building a device, controlled by the Arduino, which has a set of sensors (~ 5 sensors) . The device is simple and its main role is to record the sensors readings at a rate between 1 and 60 seconds. Each sensor reading is a single number. Each continuous run is expected to last about 2 weeks.

The problem is that, once deployed, there won't be supervision, so... I want to test first the robustness of the system.

Your test clearly showed that Arduino and SD cards are not robust, and that is a common observation, although the naive open/write/close approach does bring out the very worst of that behavior.

For robust data storage on SD cards, I recommend to use Raspberry Pi (Pi Zero W and W2 are excellent choices). Otherwise, send the data elsewhere.

1 Like

You might try the SdFat.h library. It has two very interesting examples that come with it. The first is SDFormatter, which is the best way to completely erase and format an SD card. It is better than your computer can do.

The second is called the LowLatencyLogger. At the beginning it creates 128MB files on the card. Then as you log data, you are just writing the data directly to the SD card's sectors. The file system is ignored, so you don't have to open and close the file, and the file system's directory entries and FAT tables don't need to be updated. Also, if you had a crash, anything already written to the card would be saved, and would be readable on your computer. This method is about as foolproof as you can get on an Arduino. You are just writing to consecutive sectors on the card.

But the experience of many would suggest that you really do need to do away with the String stuff. Apparently the problem is a kind of memory leak, so eventually you run out of ram, and crash.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.