Pages: [1]   Go Down
Author Topic: 2GB SD Card FAT16 not working with arduino exaample  (Read 2713 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I've got a 2GB SD card formated FAT16 with a SD card module. When running the arduino example datalogger, the serial monitor shows:

Initializing SD card...card initialized.
534,442,392
error opening datalog.txt
error opening datalog.txt

When running the arduino example card info, the serial monitor shows:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 1980432384
Volume size (Kbytes): 1934016
Volume size (Mbytes): 1888

Files found on the card (name, date and size in bytes):
DATALOG.TXT   2000-01-01 01:00:00 0



Anyone knows what am I missing?

Thank you,

Javi
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could it be case sensitive?  You have a file called "DATALOG.TXT" and you are trying to open a file called "datalog.txt".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It could be, I changed in the code the filename to DATALOG in capitals and serial monitor shows:



.txt
error open
Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 1980399616
Volume size (Kbytes): 1933984
Volume size (Mbytes): 1888

Files found on the card (name, date and size in bytes):
DATALOG.TXT   2000-01-01 01:00:00 0


Why the serial monitor shows at the beginning the following?   

.txt
error open
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why the serial monitor shows at the beginning the following?
Because you told it to. How or where we can not tell you, until the psychic we hired comes on board. When that will be, we can't tell you, because we're no psychic.

POST YOUR CODE!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my code:

Code:
/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors
 to an SD card using the SD library.
 
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 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.
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  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:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // 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("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is my code:
Which could be greatly improved. Loose the String class completely. Open the file. Loop to read a sensor and write a value, with some data before and after, if desired, to the file. Close the file.

Some people have seen the need to delay for a short while after opening the serial port, before writing to it. That, generally, shouldn't be necessary, but the stuff printed to the serial port doesn't seem to come from this program, although the text is certainly present in the program.

What kind of Arduino are you using?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using arduino uno
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I formated the SD card and runnned the following code:

Code:
//Program by Jeremy Blum
//www.jeremyblum.com
//SD Card Demonstration
//Based on Example by Tom Igoe

#include <SD.h>

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
//int pow_pin = 8;

void setup()
{
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
/* 
  //Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT); 
  digitalWrite(pow_pin, HIGH);
 */
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
 
}

void loop()
{
  String dataString = "Hello";
 
  //Open a file to write to
  //Only one file can be open at a time
 
  File dataFile = SD.open("log.txt", FILE_WRITE);
  if (dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  delay(5000);
}

The serial monitor shows:

Initializing Card
Card Failure
Couldn't open log file
Couldn't open log file


Then I runned the arduino example cardinfo and the serial monitor showed:

Initializing SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 1980399616
Volume size (Kbytes): 1933984
Volume size (Mbytes): 1888

Files found on the card (name, date and size in bytes):
LOG.TXT       1985-00-17 04:02:00 1082130433
€À`0 .  1986-00-24 06:03:00 1623228417
€À`0


There are a total of 8 files in the SD card. The file sizes range from 387MB to 3GB. The file LOG (in capitals despite in the code is lower case) is there and has 1GB size. At this stage I upload again the code and the serial monitor shows:

Initializing Card
Card Ready
Hello
Hello
Hello
Hello
Hello



Look like is writing in the card. Logically when I try to access any of the files in the SD card, windows tells me that the file or directory is corrupted and unreadable. Do you think is a hardware problem?
 



Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 471
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have about the same problem but not with the same case... datalogger example works perfect for me. It creates the file and store data inside.

However, when i use my own code, it creates the file, but does not write data on it ("error opening data file").

This is the code of the procedure containg the file management:
Code:
void startSD(){
    pinMode(10, OUTPUT);
    if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      return;
    }
    Serial.println("card initialized.");
    // Check and/or create the logdata file on SD Card
    if (!SD.exists(filename1)) {
      Serial.println("Data file does not exist on SD card");
      datafile = SD.open(filename1, FILE_WRITE);
      if (datafile){
        datafile.println("Header1");
        datafile.println("Header2");
        datafile.println();
        datafile.println("Header3");
        datafile.close();
        Serial.println("'Data.csv' header is ready");
      }
      else {
        Serial.println("error opening data file");
      }
    Serial.println("'Data.csv' file has been created");     
    }
    if (! SD.exists(filename1)) {
      error("couldn't create the file");
    }
}

I call it from SETUP section, and, of course, datafile and filename1 are defined at the header.
All the pther part of my program works great, except this part...
This is the result on serial port:
Quote
card initialized.
Data file does not exist on SD card
error opening data file
'Data.csv' file has been created

Could you see what i am doing wrong with this code?
Thanks!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could you see what i am doing wrong with this code?
Some things, yes.
Code:
    if (!SD.exists(filename1))
    {
      Serial.println("Data file does not exist on SD card");
      datafile = SD.open(filename1, FILE_WRITE);
      if (datafile)
      {
      }
      else
      {
        Serial.println("error opening data file");
      }
      Serial.println("'Data.csv' file has been created");     
    }
Putting each { on a new line, where it belongs, it becomes obvious that "'Data.csv' file has been created" is printed when the file does not exist, regardless of whether the file is created by the open() call, or not. This, of course, is nonsense.

Quote
However, when i use my own code, it creates the file, but does not write data on it ("error opening data file").
This statement, too, is nonsense. If the "error opening data file" message is printed, there is no basis to conclude that the file is created. Clearly, the conclusion ought to be that the file was NOT created.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 471
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay PaulS,

The problem is my bad english...

I made some changes along the day. First of all, i moved the code to the setup better to an independant procedure.

Okay, this is not all the code, just only what imply the SD card:
Code:
// Initialize SD memory card
  if (!SD.begin(chipSelect)) {
    e = 2;
    error("2");
    return;
  }
  else {
    Serial.println("Card initialized.");
  }
 
  // Check and/or create the logdata file on SD Card
  if (! SD.exists(filename1)) {
    Serial.println("Data file does not exist on SD card");
    datafile = SD.open(filename1, FILE_WRITE);
    datafile.close();
    if (! SD.exists(filename1)) {
      e = 3;
      error("3");
    }
    else{
      Serial.println("'Data.csv' file has been created");
    }
    delay(100);
    datafile = SD.open(filename1, FILE_WRITE);
    if (datafile){
      datafile.println("Header");
      datafile.close();
      Serial.println("'Data.csv' header is ready");
    }
    else {
      e = 4;
      error("4");
    }     
  }

Now, how it works? Not completely bad. The problem is when trying to write the header of the file.

SD card initialization... OK
File creation................... OK
Header writing............. error

I added some error control to see where is the problem.
So, the first time it runs using a completely empty sd card, it initilizate it, and write the file, but it fail when trying to write the header.
If i reset the board, then it works, because, if the file already exist, it does not try to write the header. Then, the problem seems to start where i call for the second time in the code above to SD.open

I copied from the running example of SD library, so, i really do not know what could be wrong. I tryed to solve adding a delay (different values from 5 to 1000) before to try to call the SD.open to write the header, but nothing happens.
Moreover, i have the same structure in the loop to save data from sensors, and they already work! So, the unique problem is when trying to write the file header...
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 471
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, i solved my problem. I don´t  know if it is THE solution, but at least it works for me.. Now, the program is able to write the header in the file.

What i made was to add "delay(30)" after the header writing and the data close procedure. Something like this:
Code:
datafile = SD.open(filename1, FILE_WRITE);
    if (datafile){
      datafile.println("Header");
      delay(30);
      datafile.close();
      Serial.println("'Data.csv' header is ready");
    }

Probably this is the quick&dirty solution. I don´t know.

Edited:
Quote
Okay, it is not the solution... sometimes works sometimes not....
« Last Edit: November 29, 2013, 06:10:22 pm by madepablo » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Okay, it is not the solution... sometimes works sometimes not....
Anytime you want to post all of your code...
Logged

Pages: [1]   Go Up
Jump to: