Pages: [1]   Go Down
Author Topic: Strange SD write behavior using sparkfun microSD shield and SD.h library  (Read 829 times)
0 Members and 1 Guest are viewing this topic.
Cupertino, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been having an unusual problem writing to SD cards using the sparkfun microSD shield and a 4gb card.  Bascially, the arduino reports that the file opens and closes ok, but the data does not actually appear in the resulting file.  The code below reproduces the error very reliably, however the exact amount of data that is written to the file varies.

This code should produce a file that contains integers that increment by 2 indefinitely, one number per line:
Code:
0
2
4
6
8
...


The reported serial output seems to behave as expected:
Code:
setup complete
delaying: 0
delay complete
file open
write complete
delaying: 2
delay complete
file open
write complete
delaying: 4
delay complete
file open
write complete
delaying: 6
delay complete
file open
write complete
delaying: 8
...

...but the file that is actually written is something like:
Code:
0
2
4
(End of File)

Setup:
* Arduino UNO R3
* Arduino Software 1.0.3 and included "SD.h" library
* Fat32-formatted 4GB (SanDisk)
* sparkfun miroSD shield

Code to reproduce:
Code:
#include <SD.h>

const int      chipSelect = 10;
File           dataFile;
int            t_delay    = 0;

void setup()
{
  Serial.begin(9600);

  // make sure that the default chip select pin is set to output
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

  // see if the card is present and can be initialized, otherwise halt
  if (!SD.begin(chipSelect)) halt();
  Serial.println("setup complete");
}


void loop()
{
  // make a test string to write
  String dataString = String(t_delay);
  Serial.print("delaying: ");
  Serial.println(dataString);

  // wait some amount of time between writes
  delay(t_delay);
  Serial.println("delay complete");

  // if the file is available, write to it, otherwise stop and wait
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    Serial.println("file open");
    dataFile.println(dataString);
    dataFile.flush();
    dataFile.close();
    Serial.println("write complete");
  }
  else halt();

  // increment counter
  t_delay = t_delay + 2;
}

// this function is an infinite loop that halts the arduino
void halt()
{
  while (1==1) {
    Serial.println("halted");
    delay(500);
  }
}

Any suggestions would be greatly appreciated!

Thanks,
-Jon
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6175
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you mean this shield, then you may be running out of power.
https://www.sparkfun.com/products/9802
This card uses the 3.3v output of the Uno. That is max 50ma supply. The SanDisk cards require around 100ma to read and write. Here is another thread covering this.
http://arduino.cc/forum/index.php/topic,132303.0.html
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Sparkfun microsSD shield uses pin 8 for chip select.  Sometimes it will sort of work using pin 10.

Quote
CS pin is broken out to Arduino's D8 pin. If you decide to use one of the many open source FAT libraries (like FAT16 or SDFat) make sure to change the code to reflect the location of the CS pin. Most libraries assume the CS pin is connected to D10; this will have to be changed to D8.

Try
Code:
const int      chipSelect = 8;
Logged

Cupertino, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My thanks to both of the posters who replied.

It turns out that this:
Code:
const int      chipSelect = 8;

fixed the problem completely.  Writes to the SD card now work reliably as expected.

Thanks again,
-Jon
Logged

Pages: [1]   Go Up
Jump to: