Data Logger Crash

You should not use a passive shield on a Nano as the Nano is a 5V device so the signals to the card are 5V (MOSI and CS) which can damage the card.

What are the symptoms? Although I normally use Notepad++, I checked with notepad in Windows10 and it could open the 22MB file.

I used the below code (which is basically the one from post #119), speeding up the process by using delay(10) instead of delay(100) and adding a "done" when the process is complete.

// writing Test

#include <SD.h>

#include <SPI.h>  //for the SD card module

String POWER = "";
const int chipSelect = A0;

int N = 0;
int Nmax = 16000;

File myFile;

//_______________________________________________
void setup() {
  /* Debugging serial */
  Serial.begin(9600);
  SPI.begin();
  SPI.setClockDivider(4);
  Serial.println(F("Ini SD cardN.."));

  if (!SD.begin(chipSelect)) {
    Serial.println(F("ini failed!"));

    while (1)
      ;  // la ledOFF reste allumée en continu pas la ledRec verte
  }

  Serial.println(F("ini done."));

  myFile = SD.open("TESTX3.txt", FILE_WRITE);

  // lcd.setCursor(0, 0);
  Serial.print("Welcome");
  // lcd.setCursor(0, 1);
  Serial.print("TEST");
}
//______________________________________________________
void loop() {

  int heure = 22;
  int MIN = 33;
  int jour = 2;

  delay(10);

  float voltage = 200;
  float current = 12;
  float power = 11;
  float energy = 12;
  float frequency = 12;
  float pf = 55;


  char POWER[20];  //plenty of room for the output
  sprintf(POWER, "%02d;%02d;%02d;", jour, heure, MIN);

  N = N + 1;
  // lcd.setCursor(0, 0);
  Serial.print("Nb de lignes  ");
  Serial.println(N);

  Serial.print(" POWER ");
  Serial.print(POWER);
  Serial.print(voltage);
  Serial.print(",");
  Serial.print(current);
  Serial.print(",");
  Serial.print(power);
  Serial.print(",");
  Serial.print(energy, 1);
  Serial.print(",");
  Serial.print(frequency, 1);
  Serial.print(",");
  Serial.println(pf);

  delay(10);

  if (N < Nmax) {
    myFile.print(N);
    myFile.print("   ");
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(POWER);
    myFile.print(",");
    myFile.print(voltage);
    myFile.print(",");
    myFile.print(current);
    myFile.print(",");
    myFile.print(power);
    myFile.print(",");
    myFile.print(energy, 3);
    myFile.print(",");
    myFile.print(frequency, 1);
    myFile.print(",");
    myFile.println(pf);

    myFile.flush();
  }

  if (N > Nmax - 2) {
    Serial.println("Done");
    while (1)
      ;
  }
}

// loop finish

result in Notepad++

and very strange also the fact that 12000loops are OK but if I restart new 12000loops with another file name, both are destroiyed
Below a screen capture
initial GOOD file was TESTXXA.txt
2nd should have been TESTXX.txt
Very far from that and file size inconsitant
image

I would start looking at the hardware. We're going around on circles.

  1. Make sure that your wiring is solid. One intermittent contact can cause havoc. Breadboards can be a major cause of problems. If possible, solder.
  2. I don't know if you have mentioned which card reader you're using, but please provide a link. If needed, replace with one that is guaranteed to work with 5V; AdaFruit has one if I'm not mistaken.
  3. Use a brand spanking new SD card, formatted with the tool linked in post #90; your card(s) might be damaged.

I should have the same
I was just wondering what version of the SD library you use. May be give the exact reference so I can use it
Thanks in advance

-------- Message d'origine --------

The nano has a 3.3v exit
However I take note and make a new test with a 5v shield

-------- Message d'origine --------

This was done with 1.2.4. I can't remember how I installed it, possibly library manager.

I did rerun the test with 1.1.1 (the default installed with IDE 1.8.5) and 31999 records without a problem.

This is indeed not the total solution but the fact that I have added the instruction file.position() makes that I can reach 32000 loops
You will find the code below but no hardware has been changed: I am still using the same 2 cards (one 8GB the other 16GB) UNO R3 and SD Shield 3.3V . No change in the wiring, USB Supply, SD formated by SdFat
Does someone know the explanation of this improvement?

// writing Test

#include <SD.h>

#include <SPI.h>  //for the SD card module

String POWER = "";
const int chipSelect = 10;

int N = 0;
int Nmax = 32000;

File myFile;

//_______________________________________________
void setup() {
  /* Debugging serial */
  Serial.begin(9600);
  SPI.begin();
  //SPI.setClockDivider(4);
  Serial.println(F("Ini SD cardN.."));

  if (!SD.begin(chipSelect)) {
    Serial.println(F("ini failed!"));

    while (1)
      ;  // la ledOFF reste allumée en continu pas la ledRec verte
  }

  Serial.println(F("ini done."));

  myFile = SD.open("TESTXX.txt", FILE_WRITE);

  // lcd.setCursor(0, 0);
  Serial.print("Welcome");
  // lcd.setCursor(0, 1);
  Serial.print("TEST");
}
//______________________________________________________
void loop() {

  int heure = 22;
  int MIN = 33;
  int jour = 2;

  delay(100);

  float voltage = 200;
  float current = 12;
  float power = 11;
  float energy = 12;
  float frequency = 12;
  float pf = 55;


  char POWER[20];  //plenty of room for the output
  sprintf(POWER, "%02d;%02d;%02d;", jour, heure, MIN);

  if (myFile) {

    N = N + 1;
    // lcd.setCursor(0, 0);
    Serial.print("Nb de lignes  ");
    Serial.println(N);
    Serial.print("  File  Position  ");
    Serial.println(myFile.position());

    Serial.print(" POWER ");
    Serial.print(POWER);
    Serial.print(voltage);
    Serial.print(",");
    Serial.print(current);
    Serial.print(",");
    Serial.print(power);
    Serial.print(",");
    Serial.print(energy, 1);
    Serial.print(",");
    Serial.print(frequency, 1);
    Serial.print(",");
    Serial.println(pf);

    delay(100);

    if (N < Nmax) {
      myFile.print("ORDRE   ");
      myFile.print(N);
      myFile.print("   ");
      myFile.print(myFile.position());
      myFile.print("  ");
      for (int ii = 1; ii <= 20; ii++) {
        myFile.print(POWER);
      }
      myFile.print(",");
      myFile.print(voltage);
      myFile.print(",");
      myFile.print(current);
      myFile.print(",");
      myFile.print(power);
      myFile.print(",");
      myFile.print(energy, 3);
      myFile.print(",");
      myFile.print(frequency, 1);
      myFile.print(",");
      myFile.println(pf);

      myFile.flush();
    }
    Serial.println(myFile.position());
  }

  if (N > Nmax - 2) {
    while (1)
      ;
  }
}

// loop finish

Which improvement? You are still a lazy guy that loads work on the shoulders of your potential helpers

in which line of code did you add the functioncall

of couse I can search for it. But why don't you post a description of the location where you added this line?

Why don't you describe what your thoughts are that a call to file.position() should help solve the problem??

It seems you do not read my comments in my previous answers

The improvement is that I can run the line “file.position()” in the code and you will see in the code I have added

This is well described in the answer

The position has been tested at various locations in the code : beginning and end of SD writing (before flush)

Same effect

The idea to include that instruction was to trace through “Serial.print” the position of writing on the SD

After all the solutions proposed, none have been working for me (power, hardware, code, SD card…) all this is well described in the previous comments

SD card is class 10, different have been tested as well as 2 UNO and 1 NANO and 3 different SD Shield

My thought of the problem is (as I say from the beginning) the fact that for a reason that I do not know the data are written as the wrong location

My idea is that there is a mismatch in the sequence reading-writing….

The file name becomes the “position of writing” with values like 3550045

Why does not everyone who tested my code see the same as me is not clear

Reading the position force the system to read and memorize where it is

I hope this will help the community

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