Go Down

Topic: [SOLVED] Dumpfile stop before to read completelly the file (Read 2 times) previous topic - next topic


Aug 26, 2011, 11:44 pm Last Edit: Sep 01, 2011, 11:47 pm by madepablo Reason: 1

I try to read a file stored in this microSD shield:

I used the Dumpfile example from the Sd library at the Arduino 22 IDE.

I modified a little bit the code to be running with this module, to be like this:
Code: [Select]

 SD card file dump

This example shows how to read a file from the SD card using the
SD library and send it over the serial port.

The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created  22 December 2010

This example code is in the public domain.


#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
#define MEM_PW 8                 // Define el pin que alimenta la tarjeta microSD
const int chipSelect = 10;

void setup()
 pinMode(MEM_PW, OUTPUT);
 digitalWrite(MEM_PW, HIGH);
 Serial.print("Initializing SD card...");
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(10, OUTPUT);
 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
 Serial.println("card initialized.");
 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile = SD.open("datos.log");

 // if the file is available, write to it:
 if (dataFile) {
   while (dataFile.available()) {
 // if the file isn't open, pop up an error:
 else {
   Serial.println("error opening datos.log");

void loop()

It works nearly perfect, except that it stop to read the file very soon, and it only return something more than 100 lines in the serial monitor, after to write "Completed" such as i modified in the code. So it seems that the card connection is closed.... ???

any of you had a similar problem? Any idea about what could be the problem or how could i solve it?



This is the default chip select pin #10.
Code: [Select]
const int chipSelect = 10;

If I understand your code, your chipselect pin is #8. If so change the above to:
Code: [Select]
const int chipSelect = 8;


Thanks cyclegadget,

No, no. The CS pin is 10. Pin 8 is declared to power the SD shield, but it does not disturb the other part of the code, because they are independant function. In other shields, this pin declaration is not necessary, but this is not the problem.

In fact, the code works... it start to return the content of the file... the problem is that it stop early, before to finish to return all the content of the file...



  There are more experience people here but, I am going to take a GUESS. Maybe you need to add a delay after serial.write. I don't know how much data you have but, maybe you have too much for the buffer. So, I would try to add a small delay, perhaps delay(5);  It may be too much but, it would be worth a try.



Aug 28, 2011, 01:17 am Last Edit: Aug 28, 2011, 01:23 am by madepablo Reason: 1

However, it does not work.  I tried with a delay from 1 to 100 and the file stop exactly at the same character of the file...

By the way, the file that i use now for testing if only about 250Kb... not s big for what i expect to have on the real use on my project...

Yes, i have the same idea. I think that there is a problem with the buffer... because it even stop at the same point! But i don´t  know what buffer it is or how to clean to allow to new data to be readed...

Thanks for the guess!


  Now that I am home I looked at your code some more and I don't see any problems. It looks cut and paste from the example so, it should work.

By the way, how do you know your file is 250Kb? Did you load it or read it to verify the contents?

I don't have a fix but, I can give you a link to a new library that has been made for SD cards. Maybe just trying something different will help you solve your problem.


This thread explains it all http://arduino.cc/forum/index.php/topic,64813.0.html


Aug 28, 2011, 11:11 pm Last Edit: Aug 28, 2011, 11:50 pm by madepablo Reason: 1
Thanks again,

Yes, the code is the example provided with the Arduino 22 IDE. I only added three lines to declare the pin to power the SD Shield (and one more for the delay that you proposed).

About the file, the file was filled by arduino with the data from different sensors. I know that it is about 250 Kb because i copied to my laptop meanwhile i formated the SD card to see if it was the problem... I also open it in my laptop to see where the arduino stopped to transmit the content to serial...

I will check the beta version of the library to see if it solve the problem. I will report here any news (i hope good ones).



I tested the Sdfat library, and the problem is bigger, because it reads one and a half lines of my file.

Meanwhile, the SD library reads 177 and a half lines, what is about 17Kb of the original file...


one more question related to this problem. Is it possible to overflow the serial monitor buffer? I don´t know if it exist and if it is possible.

But may be this could be the problem.... (????)
any idea? nobody had similar problems with dumpfile example??




I was near to go crazy because i thought i was doing something wrong.

Thanks for the links, but... is it possible to solve it? How?
I took a look to the links that you provided but i do not have clear what can i do to solve it...


Code: [Select]
    while (dataFile.available()) {
Code: [Select]
    while (dataFile.position() < dataFile.size()) {



IT WORKS! Thanks so much fat16lib!!! You solved my problem and my head pain at the same time!!

Thanks so much!

I tried with my short file and worked, but i also tried with a 7 Mb lng txt file and it worked as well as with an small one!

So SOLVED! Thank you!

Go Up