Go Down

Topic: Catalex Micro SD Card Module: unpredictable savings (Read 1 time) previous topic - next topic

Felipe2017

Apr 14, 2017, 11:45 pm Last Edit: Apr 14, 2017, 11:53 pm by Felipe2017
Dear Arduino friends,
I am currently working on my fifth Arduino project, the second with the Catalex Micro SD Card Adapter Module v1.0 11/01/2013 - which seems to save whenever it wants to, kind of heterogeneous in what it is doing. I had the same difficulties with the last project that includes this module. I have to admit that I am a beginner though.

In my sketch I added the variable "LOOPS_TO_SAVE_DATA" to define the loops when collected data from twi sensors should be saved. The SD card module always is initialized successfully when starting the Arduino and it does its job at the beginning, however it always stops working after a random number of loops. The number of loops it works fine differs without having a correlation, e.g. sometimes it works just for three data savings, another time (after resetting) for twenty - the maximum (after working with it the last three weeks) was 199 records. I shrank the code to another sketch to test it, even without the command "LOOPS_TO_SAVE_DATA". Same problem.

Code: [Select]

#define PROGRAM "1704X15H"
#define LOOPS_TO_SAVE_DATA 1  // Loops to save data to SD card [1-60 recommended]

#include <SD.h>
#include <SPI.h>
int chipSelect = 4;           // Set chipSelect = 4;
File mySensors;
int z = 0;                       // Variable to save data to SDcard
int loops = 0;                 // Counting loops for test purposes

void setup(void) {
  Serial.begin(9600);
  pinMode(10, OUTPUT);  // Reserve pin 10 as an OUTPUT (SDcard)
  SD.begin(chipSelect);   // Initialize SDcard with chipSelect on pin 4
}

void loop(void) {
  mySensors = SD.open("data.txt", FILE_WRITE);
  if ((mySensors && z == 0) || (mySensors && z >= LOOPS_TO_SAVE_DATA)) {
    Serial.print("Sensor data successfully saved! Loops: ");
    Serial.println(loops);
    mySensors.print(PROGRAM);
    mySensors.print(",");
    mySensors.println(loops);
    // [All data...]
    mySensors.close();
    z = 1;
  } else if ((mySensors && z >= 1) && (z < LOOPS_TO_SAVE_DATA)) {
    Serial.print("Sensor data will be saved each ");
    Serial.print(LOOPS_TO_SAVE_DATA);
    Serial.print(F(" loops. Current loop: "));
    Serial.println(z);
    z++;
  } else if (z > LOOPS_TO_SAVE_DATA) {
    Serial.print("Sensor data is NOT saved as it should!");
    z++;
  } else {
    Serial.print("Sensor data is NOT saved!");
    z++;
  }
  loops++;
  delay(1000);
}

I formated the card with SD-Formatter 4.0; I tried a 471 ceramic capacitor between VCC and GND, another 100uF to support my Arduino Nano in general as I read on a website that this helped someone else (https://forum.arduino.cc/index.php?topic=58643.0). Still the same. After spending lots of hours to figure out any correlation I am pretty close to through it in the trash can, wondering why so little trouble shooting for this specific module is on the web. Has somebody any idea why it saves data but always randomly stops doing so?

Any suggestion is very much appreciated.

Felipe2017

Update: The 471 ceramic capacitor I used was broken! As a result the SD card does save the data now (48h testing) as long as it saves every loop the desired data. If I change LOOPS_TO_SAVE_DATA to, e.g. 5, it works a few rounds as it should and then it still stops for a reason I can't explain.

cattledog

#2
Apr 21, 2017, 06:17 am Last Edit: Apr 21, 2017, 06:24 am by cattledog
Do not open the file each pass through loop without closing it. This uses some memory, and can cause the Arduino to run out of memory.  see http://forum.arduino.cc/index.php?topic=158304.0;wap2

Place this code in the conditional block where the file is written and then closed.
Code: [Select]
mySensors = SD.open("data.txt", FILE_WRITE);


Felipe2017

That's it, cattledog - sketch is up and running since 3 hours and still saving data successfully!
Thank you very much for you kind help, you made my day!  :)

Go Up