New IDE 1.8.0, SD card library doesn't work, but it used to in 1.6.7

When I run the example sketch in 1.6.7 for the SD library called 'list files', it works, and lists the files on my SD card. When I updated to 1.8.0, that example file does not work, but gives me the error, cards initialization failure. ???

My Card is formatted as Fat32. Does that not work with the 1.8.0 SD Library? I can't format it as Fat16, because it's 8Gb, and I think it has to be 2Gb or smaller for Fat16

Still no luck running the example sketch for SD card library in the latest 1.8.0 This version doesn’t look like it’s just a beta version, or is it? Below is the example sketch. If I run 1.6.7, and open this example sketch, it works. But it doesn’t work on 1.8.0. The init of the card fails.

/*
  Listfiles

 This example shows how print out the files in a
 directory on a SD card

 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 2 Feb 2014
 by Scott Fitzgerald

 This example code is in the public domain.

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

File root;

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)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

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

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

SD card read/write fails in IDE 1.8.0 using Arduino Uno and Ethernet Shield. Reports that SD card is found and initialized properly but fails to read or write on it. But if card is formatted FAT32 then initialization also fails. Problem can be sollved by uninstalling IDE 1.8.0 and reinstalling older IDE 1.6.8.

bump, this is still an issue. However I've confirmed that the last version my script works in in 1.6.9. All later versions (I tested them all) it does not work in. All 1.6.X versions it reports that it works, but no data can be retrieved. 1.8.0 it does not report that it works at all.

I tried pasting my code but it exceeded the 9000 character limit.

I wanted to chime in and state that I, too, am having difficulty with version 1.8.0 while attempting SD file access and directory listing. I can attest that up to version 1.6.12 all was working fine with my Arduino Zero; however, now with 1.8.0 it fails consistently. Nothing has changed in my code so it is quite odd.

Other pertinent information, the core utilized is 1.6.11 and it works fine w/ the 1.6.12 IDE and seems to upload fine with 1.8.0. All other SPI actions work fine, just the SD component fails. I’m really at a loss as to what to do for the code works fine when compiled in prior versions of IDE.

Any thoughts?

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

 pinMode(10, OUTPUT); // change this to 53 on a mega

  if( !SD.begin( SD_CS )) {
    // failed
   // card that is NOT inserted.
    print( sderror );
    return;
  }


File dir = SD.open( "/" );
  dir.seek(0);

  while( true ) {
    File entry = dir.openNextFile();
    if( !entry ) {
      entry.close();
      break;
    }

    // header
    print( (const uint8_t *)entry.name() );
    if( entry.isDirectory() ) {
      print( slashmsg );
    }

    if( entry.isDirectory() ) {
      // directory ending
      for( int i=strlen( entry.name()) ; i<16 ; i++ ) {
        print( space );
      }
      print( dirextmsg );
    }
    else {
      // file ending
       print( CR_LF );
    }
    line_terminator();
    entry.close();
  }
  dir.close();

Same issue here, sketch running fine with 1.6.X starts to fail with 1.8.0...

Try to debug a little bit and the issue seems to come from root.openRoot(volume) in SD.begin()...

Hi all,

I must say thanks to @abmimport for making me notice this thread!

We started investigating after receiving some reports like yours and we were able to replicate the issue and narrow down to the origin of the problem. The issue is related to some improvements we brought to the SD library in order to increase its performances. The fact is that only certain SD card are not working with the 1.1.0 SD library (for example I have a 4GB-Class4 and a Sandisk-Ultra-8GB-Class10 that are working and a Verbatim-8GB-Class4 that is not working) and we need some data in order to fix this.

So can I ask you to provide some information about the micro SD card are you using, such as: brand, model, capacity and class. Also the shield or the Adapter you are using.

There's also a github issue so if you gently want to post your feedback there will help us a lot to track down the issue: https://github.com/arduino/Arduino/issues/5792

In the meanwhile you should be able to use your SD card with the 1.8.0 Arduino IDE by simply installing the SD library 1.0.9 from the Library manager

Thank you for the contribution Federico

Had similar problems with IDE 1.8 and IDE 1.6.13 on my Mac today.
All my SD cards were not recognized.
I had already formatted all cards with the special SDFormatter, but no card was detected.

As recommended the downgrade of IDE 1.8 from SD library 1.10 to SD library 1.09 → no change. Still my cards were not recognized (initialization failure).
Did the tests with a standalone SD card module (clone of Adafruit’s latest model) and a Nano as MCU, then with an UNO and an Ethernet shield with SD card on board → no success.

Then I re-formatted one of my cards with the SDFormatter again, this time checking the “interoperability” option:
SDFormatter.jpg
This eventually did the trick (at least for me) and my card showed up with all details using the SDCard Info sketch. Whow - 2 hours sweat and now a big relief (for the time being, let’s see what happens tomorrow). Repeated the re-formatting with the remaining cards - all of them are now accepted by my SD card module!

As requested some information about the cards:

  • 1GB SD (old card, unknown manufacturer and no further details available - but working!!)
  • 4GB HC Class 4 / Kingston
  • 8GB HC Class 6 / Platinum

bump

The problem may be this mod in Sd2Card.cpp.

Loops like this:

  for (uint16_t i = 0; i < count; i++) {
    dst[i] = spiRec();
  }

Were replace by this:

spiRec(dst, count);

SpiRec() is this new function:

void spiRec(uint8_t* data, int size) {
#ifdef USE_SPI_LIB
  SDCARD_SPI.transfer(data, size);
#else
  while (size) {
    *data++ = spiRec();
    size--;
  }
#endif
}

USE_SPI_LIB is defined so transfer(data, size) is used.

The problem may be that transfer(data, size) sends the content of the ‘data’ buffer to the card and stores received bytes in the ‘data’ buffer.

Some SD cards may interpret the data sent over MOSI as a command. Commands start with a byte that has bit-7 low and bit-6 high. the command index is bits 0-5.

My original version sent all 0XFF bytes on MOSI to avoid this possibility.

I you are having a problem try replacing the call to spiRec() with the original loop.

the file is located here:

arduino-1.8.0/libraries/SD/src/utility/Sd2Card.cpp.

You could just edit spiRec like this:

void spiRec(uint8_t* data, int size) {
#ifdef USE_SPI_LIB
  for (int i = 0; i < size; i++) {
    data[i] = SDCARD_SPI.transfer(0XFF);
  }
#else
  while (size) {
    *data++ = spiRec();
    size--;
  }
#endif
}

If anyone from Arduino reads this please consider a new version of SPI.transfer().

Many RTOS HAL libraries have a transfer function with three arguments.

void spiExchange(size_t n, const void* txbuf, void* rxbuf);

It's like the current transfer if both txbuf and rxbuf are non-null.

If txbuf is null, 0XFF bytes are sent. If rxbuf is null, receive data is discarded.

This allows every possible option, send from one buffer and receive to another buffer, exchange data in a single buffer, send with no receive buffer, and receive with no send buffer.

I found a 16GB Samsung card that fails with the 1.80 Datalogger example.

I modified spiRec() like this:

void spiRec(uint8_t* data, int size) {
#ifdef USE_SPI_LIB
#define FIX 1
#if FIX
  for (int i = 0; i < size; i++) {
    data[i] = SDCARD_SPI.transfer(0XFF);
  }
#else
  SDCARD_SPI.transfer(data, size);
#endif
#else
  while (size) {
    *data++ = spiRec();
    size--;
  }
#endif
}

When FIX is defined to be one, it works

Initializing SD card...card initialized.
433,350,296
350,342,317
331,335,324
316,318,316

and when FIX is defined to be zero it fails

Initializing SD card...Card failed, or not present
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt

i spent ages failing to get my sd card working, and then installed an earlier version (1.0.8) of the SD library which fixed the issue for me.

IDE 1.8.1 should do now as the SD library was also updated.
I am using 1.8.1 since mid of January and it works fine here.
But it did already work with 1.8.0 having formatted my SD card as said in reply #8.

I am on a Mac; the option message (which apparently did the trick for me (using the FULL format!)) fpr Windows is a bit different - something like “Format size adjustment ON”.

I am using IDE 1.8.1 and a previously working sketch no longer is able to initialize a 16 GB class 10 Microcenter micro-SDHC card, or a 32 GB class 10 Samsung micro-SDHC card.

The sketch was previously working on these cards, but I do not remember which version of the IDE.

However, I was able to get it to initiate and perform read/write on a 16 GB Class 4 Sandisk micro-SDHC card.

So it would seem that perhaps the SD library is still not working for everything.

v1.8.1 works fine for me. My only complaint is that all new files created by any of the SD Examples have a date of 2000/1/1 12:00.

I had the same problem: SD cards were accessed fine with 1.6.9 IDE and stopped working after recompiling on 1.8.0 except for an old 128MB microSD which worked with both IDEs. I reverted using the sdfat lib which works. I updated to 1.8.1 but hacen't tested the default SD lib yet.