Go Down

Topic: Files on SD Card are being deleted after time (Read 1 time) previous topic - next topic


I used to log data on my SD card. This time however i logged for a few days, expecting everything to go fine (heck it never will :smiley-cry: ). After a few thousand logs (based on the test i ran afterwards; not a consistent number i think), i am starting to occasionally get error messages because my SD card couldn't be opened properly. 
I am asking why that might happen. That alone not being the problem. The main problem was, that the file i create gets deleted after that comes up and weird signs ASCII characters.

It also changes my card name sometimes.

Things that connot be the issue:
card size. 30gb
delay between writing, because i originally had a 8 second delay where i took measurements and had the same problem occur without delays at all.

I wrote a little test code to see if it repeats (before it was every 8 seconds this time i wanted to see how fast the error comes up)
This is the Code, i doubt that you will track the problem down on this one though.
Code: [Select]
#include <SD.h>
#include <SPI.h>
int CS_PIN = 10;
void setup() {
  // put your setup code here, to run once:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(CS_PIN)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
File file;
char b[7];
int test = 0;
void loop() {
  // put your main code here, to run repeatedly:
  String(test).toCharArray(b, 7);
  // if the file is available, write to it:

  file = SD.open("datalog.txt", FILE_WRITE);
  if (file) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  Serial.flush(); //make sure the data is sent completly before sleeping again

Since i am pretty much clueless now what it could be, i am turning to you guys willing to know if you had similar problems/experience or some ideas on what might be causing this issue


Hi, Grasbock
Maybe it has something to do with String in the loop.
String(test).toCharArray(b, 7);
String object consumes a lot of memory and after a while it behaves erratically.


I could think of that possibility that in case of me storing a string resulting in a memory leak or something, but now i am just using a string function, without actually creating one. I will try running some test without though to see if changes come up.

I have run some other tests in the meantime:
My Code now only tells me when it starts failing.
the test numbers/runs it fails at:
12381 (files ok)
4344 (all deleted)
then sometimes it doesn't fail at all going beyond 39k
i reset the arduino making it run again beyond 30k
But in between these last two tests the files got deleted again so i assume resetting arduino can kill the old file?
After that i ran another test. It reaching up to 120502 (12k+ came up twice?!);

I have a suspicion that it has to do with Serial; i am thinking of the phenomenon that usb used to have issues when pulling it out of a computers with destroyed data. I assume because i tend to stop the process by pulling out the the power of the arduino to see the card, things get messed up. Hope that this will give someone an idea what this issue might be


Anyway you should close all your files before removing the sd-card


Anyway you should close all your files before removing the sd-card
Isn't that what file.flush() already does?


file.flush() empty's the buffer, but I think you still have to close the file for removal of the sd card.
Sometimes all is right if you remove without closing the file, but when Murhpys law comes in the file get corrupted.


Aug 25, 2017, 07:24 pm Last Edit: Aug 25, 2017, 07:33 pm by GRASBOCK
Murphys Law isn't some random factor that comes around and destroys everything. It states, that everything that can go wrong will certainly go wrong one day when run long enough.  This would mean that something can go wrong in my code and the question is exactly what. If you say the close command might fix it, you imply, that the flush is not sufficient. Then what i am interested in is: what does the file.close() do, that file.flush() does not?
EDIT: Is there something that has an impact on the cards behaviour when using close()?
Anyway, i also found out that whenever i try to press reset while the arduino is writing, the card gets corrupted. So it might have to do something with not closing/finishing properly. But what exactly?


So apparently the problem lies within the pro mini i am using. I tried the same with a nano and it worked fine.  Unfortunately i can't explain the issue, because it works completly fine. Only the continuos writing into that file does not.

Go Up