Go Down

Topic: SD card,Mega 2560: no text written to file, file seems corrupted on pc. (Read 3227 times) previous topic - next topic

Ward123

I'm using a sd module and the mega 2560. Using the example programs, I can list the files on my sd-card.
One step further, I'd like to write something like "123" or millis(); to a file on the sd card.
Suspecting the wiring being correct, because the files can be read (with the size etc.) after formatting the card.
But when I try to write something to the card, the card goes corrupted, or nothing is written...
(I editted the cardInfo example)
Code: [Select]
// include the SD library:
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;   

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("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(53, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }
 
//write something to the card 
  File dataFile = SD.open("test.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println("testing");
    dataFile.close();
    // print to the serial port too:
    Serial.println("testing");
  } 
  else Serial.println("failed");
}
void loop(void) {}


Output:  Initializing SD card...Wiring is correct and a card is present.
Card type: SD1
failed

So for some reason, It always fails to write to the card. What am I overlooking? :/

PaulS

Quote
What am I overlooking?

The position of the write-protect switch? The amount of space still available on the card?

What shield are you using? With the Ethernet shield on the Mega, pin 53 needs to be set as output, but it is not the chip select pin for the SD card. It is the chip select pin for the Ethernet chip. Pin 4 is still the chip select pin for the SD card.
The art of getting good answers lies in asking good questions.

fat16lib

You can't mix code from the CardInfo example with use of SD.  SD has it's own version of these:
Code: [Select]

Sd2Card card;
SdVolume volume;
SdFile root;


Notice that SD is not used in CardInfo.

Ward123

The write-lock switch is in the right position. I'm using 2 old sd cards (64 and 256MB's), FAT16 formatted. to write a few strings, this would do?
I'm using a "YL-30" SD card module from dx.com, (chinese stuff) http://dx.com/p/sd-card-reading-writing-module-for-arduino-148784#.UwDKO_ldVZg
It worked a few months ago, but I haven't used it for a while...
I used that example sketch because I knew that using this, the connection to the SD card was ok.

I don't know what pin must be set to high, I'm not using the ethernet shield, But I thought it had to be 53?


Using the "datalogger" program, but changed the dataString:
Code: [Select]
/*
  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 = 53;

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 = "Test123";


  // 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");
  }
  delay(5000);
}


I get the error opening datalog.txt error. Even if datalog.txt isn't found on the sd card, it should be automatically created?

fat16lib

This SD module is marginal since it doesn't have proper level shifters.  Sometimes they work with a given card but the slightest thing will cause them to fail.

Go Up