Go Down

Topic: Weird behavior of SD library Datalogger example (Read 183 times) previous topic - next topic


Hi everyone,

I work on a project to build an arduino UNO R3 based data logger. It will (hopefully) receive data from two NMEA 0183 devices (GPS and Anemometer) and store it on a micro SD card. So far I've written a code to parse NMEA sentence from the GPS module (AT6588 based, works fine as far as I can see).

I've modified the data logger example of the SD library to put the parsed NMEA data (latitude and longitude only in my case) on the SD card. I use this SD card module, it is connected to the arduino as shown in the picture attached. I've triple checked the wiring, its correct. My full code, if of any interest, is attached as ino file.

When I run the program it looks fine. Most of the time the SD card is successfully initialized. The data I want is saved on the SD card in a text file, just as required. Unfortunately the problem occurs after about 830 samples of data on the text file. The serial monitor puts out the error message "error opening log.txt" and no further data is recorded. This happens repeatedly every time I run the program. I've never recorded beyond 900 samples.

I thought it might be my code so I tried the original SD library data logger example (unchanged but chip select port 10 instead of 4). The exact same error occurs, the program fails to open the txt file after about 800 something samples. When I try to continue to write on the SD with the log file already filled with 800 + x samples the card is initialized but the file cant be opened. Seems like something keeps my program and even the example from saving more than these number of lines in one txt file. When I change the name of the file in the program (so that the code creates a brand new and empty file) it still doesent work.

Is there something obvious I forgot? Does anybody has experienced this problem before?

What could I try next to debug the code and build a data logger that runs for at least 10h?


My initial thought was that you maybe forgot to close the file but I see that's not the case.

I've not used the String class myself, but from what i've read in the forums, it seems that it is best avoided. I did find this article about Arduino string manipulation. Here's the bit that leapt out for me:

Furthermore, if you start manipulating strings of text using the String class, the ram disappears rapidly. Even worse, if you use too many String class routines, the memory starts getting fragmented, and in extreme cases, runs perfectly for a week and then crashes.
I wonder if that may have something to do with what you are seeing? 


Is there something obvious I forgot? Does anybody has experienced this problem before?
Yes, you forgot to read the instructions on how to use this forum, and post your code in the proper manner. Generally speaking, if you can use
and see what you expect to see on the serial monitor
should send what you expect to send to the card. So, can you send the data to the screen OK? I recognise that this exercise may not be as simple as it sounds, but doing it as a dummy routine can be worthwhile.

While mark is dead right to caution about Strings, I don't think it is likely to be an immediate problem.


Thanks for the reply. I've attached my modified code as file (to long for a post) but thats not the point. I struggle to run the unchanged "Datalogger" example of the SD library (native to IDE so no point in posting it here). It reproduces the same error I see in my modified code. 

What I see in the serial monitor when I run the Datalogger example is this:
Code: [Select]
10:29:56.212 -> Initializing SD card...card initialized.
10:29:56.635 -> 438,395,373
10:29:56.635 -> 398,393,383
10:29:56.635 -> 384,381,378
10:29:56.682 -> 377,374,372
10:29:56.682 -> 373,370,368
10:29:56.728 -> 368,365,363
10:29:56.728 -> 361,357,356
10:29:56.728 -> 353,348,348
10:29:56.775 -> 346,342,341
10:29:56.775 -> 343,339,337
10:29:56.775 -> 339,335,334
10:29:56.822 -> 333,329,328
10:29:56.822 -> 325,321,320

It goes on like this for a while. After 830 samples written to the card this happens:
Code: [Select]
10:30:10.909 -> 178,176,174
10:30:10.956 -> 179,177,175
10:30:10.956 -> 180,178,176
10:30:10.956 -> 180,178,176
10:30:11.003 -> 180,178,176
10:30:11.003 -> 179,177,175
10:30:11.611 -> 178,176,174
10:30:12.219 -> error opening datalog.txt
10:30:12.826 -> error opening datalog.txt
10:30:13.435 -> error opening datalog.txt
10:30:14.042 -> error opening datalog.txt
10:30:14.651 -> error opening datalog.txt
10:30:15.260 -> error opening datalog.txt
10:30:15.866 -> error opening datalog.txt
10:30:16.474 -> error opening datalog.txt
10:30:17.082 -> error opening datalog.txt
10:30:17.643 -> error opening datalog.txt

The error message is printed for every incoming sample and goes on forever.

When I now run CardInfo from the SD Library I see this:

Code: [Select]
10:33:41.736 ->
10:33:41.736 -> Initializing SD card...initialization failed. Things to check:
10:33:43.886 -> * is a card inserted?
10:33:43.933 -> * is your wiring correct?
10:33:43.933 -> * did you change the chipSelect pin to match your shield or module?
10:34:04.395 ->
10:34:04.395 -> Initializing SD card...Wiring is correct and a card is present.
10:34:05.287 ->
10:34:05.287 -> Card type:         SDHC
10:34:05.333 -> Clusters:          490466
10:34:05.333 -> Blocks x Cluster:  16
10:34:05.380 -> Total Blocks:      7847456
10:34:05.427 ->
10:34:05.427 -> Volume type is:    FAT32
10:34:05.427 -> Volume size (Kb):  3923728
10:34:05.474 -> Volume size (Mb):  3831
10:34:05.474 -> Volume size (Gb):  3.74
10:34:05.521 ->
10:34:05.521 -> Files found on the card (name, date and size in bytes):
10:34:05.567 -> SYSTEM~1/     2021-02-18 17:53:14
10:34:05.614 ->   WPSETT~1.DAT  2021-02-18 17:53:14 12
10:34:05.660 ->   INDEXE~1      2021-02-18 17:53:14 76
10:34:05.708 -> DATALOG.TXT   2000-01-01 01:00:00 11089

The Card is not initialized right away (has always been like this, no idea if thats normal), I have to remove it, plug it in and reset the arduino to run CardInfo (thats what you see above). 

Can you see anything wrong? 


It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up