Go Down

Topic: Cannot create files on SD Card.  (Read 2024 times) previous topic - next topic

MrBill57

Using the Adafruit Datalogging shield P1141 and UNO R3 running a program to record data to SD Card. Took it out last week and it recorded data and worked fine.

I have used the SD Formatter program program to format the cards. I have 2 8GB and 1 32GB SD Cards.  Size is reported wrong - 3485 MB for the 8GB card.

I first got it to work using the SD datalogger program referred to at: https://www.adafruit.com/products/1141

The other day I was modifying the other part of the code and it quit initializing the SD card. I thought maybe it was broken so I tried a brand new datalogging shield #1141 and even a new UNO R3. No play - it will not open the file on the card. I have tried 2 or 3 of the SD reference sketches. They all fail at one point or another, now. Backing up to the examples.  Example code and results below…

When it does see the card, it reports the size wrong  (like 3.4GB for an 8 GB card - see printout).
That makes me think that I have something wrong in the driver calls. Maybe getting my libraries mixed up? Do I need to use a different libraey for the larger SD cards? 8 GB is the smallest I could buy new!
 
Call using:  if (!card.init(SPI_HALF_SPEED, chipSelect)) does report the card present.
The initial call I used to int card_status = SD.begin(10) always fails.

Card.init() It initializes the card but gets the size wrong. Call to SD.open fails.

.. . . . . . . . . . . . . 
OUTPUT to Serial Monitor:

//Sketch_dec18b

Initializing SD card...At volume.init(card): Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (bytes): 3654471680
Volume size (Kbytes): 3568820
Volume size (Mbytes): 3485

At root.openRoot(volume) call.
Files found on the card (name, date and size in bytes):

New filename is: LOGGER00.CSV
At call to SD.exists(filename): file not there, open it.
After call to SD.open(filename), returns: 0
0Failed - couldnt create file: LOGGER00.CSV


CODE:

//Test_18b-121716 modified by Bill Allen

#include <SPI.h>
#include <SD.h>

File logfile;
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 10; // was 4

void setup() {
  pinMode(chipSelect,OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
  }

  Serial.println("Sketch_dec18b");
  Serial.print("\nInitializing SD card...");
 
  Serial.print("At volume.init(card): ");
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card 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.");
  }

  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");
  }

  if (!volume.init(card)) {
    Serial.print("At volume.init(card): ");
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();   
  volumesize *= volume.clusterCount();       
  volumesize *= 512;                           
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nAt root.openRoot(volume) call. \nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  root.ls(LS_R | LS_DATE | LS_SIZE);

    char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    Serial.print("\nNew filename is: ");
    Serial.println(filename);
    Serial.print("At call to SD.exists(filename): ");
    if (!SD.exists(filename)) {
      // only open a new file if it doesn't exist
      Serial.println("file not there, open it.");
      delay(50);
      logfile = SD.open(filename, FILE_WRITE);
      Serial.print("After call to SD.open(filename), returns: ");
      Serial.println(logfile);
      break;  // leave the loop!
    }
  }
  if (!logfile) {
    Serial.print(logfile);
    Serial.print("Failed - couldnt create file: ");
    Serial.println(filename);
  }
  else{
  Serial.print("File successfully opened: ");
  Serial.println(filename);
  }
 
  // Yes this should be behind other code but let's see if it will write?
  for(int i=0; i<10; i++) {
    logfile.print(analogRead(0));
    logfile.print(",");
    }

  logfile.flush();
//  logfile.close(); // When do I need to Close the file or does flush take care of the data?
}

void loop(void) {
}

Nick_Pyner

Why don't you just use the standard SD library and the standard SD read/write example included in the IDE? That way, you might see if you actually have a problem.
I understand the maximum file size is 4Gb but you can have more than one.

MrBill57

Thanks - That's what I have been doing for 2 days - trying all the examples I can find, but none of them work. ReadWrite example uses the SD.begin(), which as I say, has stopped working. That is the routine I started with that did work. Other than the computer, the cable is the only thing left to change. Well, maybe reload the IDE but that's extreme. That is why I am looking at other methods of following the rabbit hole.

Here is the output of ReadWrite - it won't even get so far as to open the file in line 36, much less write anything! Have I mixed up my LIB's or what! I am going to try a different cable but I am pretty sure this one is good. But now the IDE & Board won't run any of the sketches with the SD cards.  It just quit a few days ago and is driving me nuts!

I'm going to empty the \Temp folder. The board and Ardiono are both new as of yesterday and won't talk to the cards. Using card.init() I can at least get to that step. When it does recognize the card it says it is the wrong size (3.4GB instead of 8 GB (7.9 or something, really)).
Also going to try turning on pin 10. Did that once, though.

Here's the output and sketch.

Output of ReadWrite:
Initializing SD card...initialization failed!

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

File myFile;

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


  Serial.print("Initializing SD card...");

  if (!SD.begin(10)) {  // was 4 CS is pin 10.
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Nick_Pyner

Well, maybe reload the IDE but that's extreme. the rabbit hole.
It might sound extreme, but it doesn't cost anything and can be done immediately.

Quote
Here is the output of ReadWrite - it won't even get so far as to open the file in line 36, much less write anything! Have I mixed up my LIB's or what!
Quite possibly and, since the standard library is included with the IDE, re-installing it is starting to look increasingly like a good idea. The clunky-looking original code gives further thought in this direction - hence my comment - and there may well be a mis-match between code and library. There may be more than one SD library called SD.h, but I don't know of any.  At least, by re-installing, you will have no doubt about what you've got.

teddyz

I had similar problem. I made a change in an old project using SD-card. I run Arduino version 1.8.0 on Windows.
Result was that I no longer could communicate with SD-card.

Downgrading to 1.6.12 made no difference.

Downgrading to 1.6.3 gave this error:
Multiple libraries were found for "SD.h"
Used: C:\Users\Admin\Documents\Arduino\libraries\SD
Not used: K:\Dur2k_D\Download\Programmering\Arduino\ArduinoIDE\libraries\SD
Error compiling.


I removed the library on C: and after doing so I regained access to the card. Great, but I want it to work with 1.8.0.

Other differences 1.8.0 vs 1.6.3 are that the old compiler use 55974 bytes of program storage space and 6302bytes of global variables. The newer use  52000, 6397. The newer also warns about deprecated conversion from string constant to 'char*'.




rpt007

#5
Jan 09, 2017, 05:28 pm Last Edit: Jan 09, 2017, 05:30 pm by rpt007
I am pretty new to SD Card handling with Arduinos, but for my next bigger project SD card is essential.

So I started testing with some cards I had in my stock. With all of them I ran into the same problem that even when the card was detected by Arduino (either using the SD library which comes with IDE or with SdFat which has to be installed from GitHub) - after a few other tests or drawing / re-inserting the card (power off of course) none of the cards (I used 3 different ones with 1GB, 2GB and 4GB) could be initialized (=not mounted) in the Arduino again.

What got me back in the race: SDFormatter as the tool which is strongly recommended (see very first thread in this subsection "Storage" of the forum) when it comes to format SD Cards. SDFormatter v4.0 is available for Mac (I use OSX Yosemite) and for Windows.

First I also failed with the SDFormatter - same problems as before when I had formatted them in my Mac.
But then - by chance I set the format option "Interoperability" ON - that did the trick! Now I can use all my cards in the SD examples and SdFat examples.

Instead of "interoperability" the menu in Windows talks about "Format Size Adjustment" -> this should be checked "ON".

Hope that will help you as well.
Before you ask:
Did you really read and understand How to use this forum ?
AND:
Do you have already some solution or is a part of the problem sitting in front of the screen?  :)

DaIceMan

To be honest, none of my microSD cards would work (4GB, 8GB, 16GB) apart from a very old 128MB card (reformatted them with the SD utility, etc). I was using IDE v 1.8.0. I tried this in my office. I went home where on my laptop I had v1.6.9, and there after recompiling and uploading, everything worked!. It's definitely a problem with the 1.8.0 distro - the SD library apparently has problems at least on my setup. I didn't upgrade anything on my work PC as it was a clean install of 1.8.0 so no mixture of different libs. In any case I reverted to using the sdfat library which works.
  Just my 2 cents.

teddyz

FYI I got it to work on 1.8.2. So it was a bug after all...

ARDUINO 1.8.1 - 2017.01.09

[ide]
* Fixed font rendering not anti-aliased on Windows (regression)
* Increased number of colors on serial plotter to 8, thanks @cousteaulecommandant

[libraries]
* Fixed regression in SD library. Thanks @greiman

Go Up