Go Down

Topic: SD card data errors (Read 449 times) previous topic - next topic

deeceethree

I am writing an application which reads a few kilobytes of data from files on an SD card. I have an Arduino Mega 2560 and an Adafruit Data Logger shield and a SanDisk SD card purchased with it. I find that when the program reads a block of data from a file, using repeated read statements, very often part of the data read - typically 100 consecutive bytes - consists of zero bytes rather than what is actually in the file. These errors occur on more than half of all attempts. They are random, occurring at a different point in the data block each time. I have tried using a different SD card, but that did not fix the problem.

Here is the essential part of the code:

Code: [Select]
byte buffera[1000];
int bufcnt = 1000;
int filptr = 2000;
myfile.seek(filptr);
for (int i=0; i<bufcnt; i++) {
   buffera[i] = myfile.read();
}


David

pito

What sdcard library do you use?
What is your SPI speed?
Did you benchmark your setup (see examples in your sdcard lib)?

deeceethree

Thanks for your reply.

The library I am using is the current library available for download from the Adafruit web site (actually from Github). It is named SD-master.zip and the files in it are all dated 27 May 2016.

I can see in the library a function call with the parameter SPI_HALF_SPEED, so presumably that is the speed that is being used.

I am not sure what you mean by benchmarking my setup. I have successfully run all the examples in the library. I tested the example named DumpFile with the file that gave the errors with my program, and there were no errors.

I reformatted the SD card using the "official" formatting utility, downloaded from the Adafruit web site.

David

deeceethree

I have solved this problem. I have an Adafruit motor control shield plugged into the Arduino, and a servo was connected to one of the two servo headers on the shield. This header is connected to pin 10, the same pin used by the SD card reader. I have moved the servo to the other header, which uses pin 9, and the problem is fixed.

There is a lesson here: put all your pin assignments together in a sketch, and watch out for pins that are dedicated to some hardware function.

David

sterretje

#4
Jun 14, 2018, 08:21 pm Last Edit: Jun 14, 2018, 08:22 pm by sterretje
There is a lesson here: put all your pin assignments together in a sketch, and watch out for pins that are dedicated to some hardware function.
I use a dedicated file called io.h with all pin declarations sorted ascending by number. Simply include it in files that need it.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up