Arduino SD library

Hi @Ivan, I’m trying the SD access functions.
I’m getting no results.
I’m trying one of the examples included in the package in
\AppData\Local\Arduino15\packages\artekit\hardware\stm32f4\1.0.1\libraries\SD\examples\ReadWrite\

Here is the one I use and I get always the “init failed” message
What am I doing wrong?

#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(4)) {
    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
}

Looking at the SD.h, the Begin should return always true

bool SDClass::begin(uint8_t csPin)
{
	return true;
} 

But if i check the value with the simple code below, I get always “False”

  iniz = SD.begin(4);
  if (iniz== false){
    Serial.println("Falso");
  }
  else{
    Serial.println("Vero");
  }

Hi @Mangaman,

Regarding the code you can find in the examples folder, this is the output for the ReadWrite example:

Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.

Regarding your second post, I get the right return code. For example:

#include <SD.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  bool iniz = SD.begin(4);
  if (iniz== false){
    Serial.println("False");
  }
  else{
    Serial.println("True");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

It prints True.

I am using IDE version 1.8.2 and the latest PropBoard release: 1.0.1

Well, that’s nice. Same code.
I’m using Arduino ide 1.8.3, and Propboard 1.0.1

I will continue tomorrow

This morning at the office it works. But with IDE 1.8.2
Could it be the problem?

now I upgraded to 1.8.4 (1.8.3 is not available on Arduino site) and still working.
This evening I will upgrade my IDE at home and try again.

**** EDIT ****
Still having problems also after I downgraded to 1.8.2.
The SD is always detected and the files are read ok.
The problem is when I write.
If I create a file it returns the file is created, but it does not exists if I remove the SD and check it with a SD reader on PC
Same if I write something on an existing file, nothing is written.

Could it be something related to the SD supply voltage?

Check if you are closing the file before removing the SD. Files may not be updated if you don’t. This is a normal behavior (and one of the potential problems I mentioned in another thread a while ago). Or if you keep the file open, you have to flush the file with File::flush() after a write, or you can open the file with the O_SYNC flag to flush every write automatically to the SD.

What about the \AppData\Local\Arduino15\packages\artekit\hardware\stm32f4\1.0.1\libraries\SD\examples\ReadWrite\ example?

Yes I’m using that example.
it reads nothing from the file, both if the file already exists or has to be created (and it’s not)

So you are seeing the following at the serial port, excluding the last line (testing 1, 2, 3)?

Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.

If so, it is very strange. I would say to try with another SD card.

I’ve tried this:
to retrieve the number od bytes written by the print function

Serial.println("Writing to test.txt...");
num = myFile.println("testing 1, 2, 3.");
Serial.println(num);
// close the file:
myFile.close();
Serial.println("done.");

and I get always zero bytes written

Initializing SD card...initialization done.
Writing to test.txt...
0
done.
test.txt:

that’s my model.

ok, is the SD card model.
I used the SD of a saber sound card and it works. (it’s a SanDisk 4GB class 4 )

By the way, which one are you using @Ivan ?

A bunch of them (around 10 different models/brands). I don’t have precisely the one you posted (I have a 64GB kingston). I am about to order a couple of those.

ok so I’m quite unlucky :joy:

I tried to test as much brands/models as I can. Still have to try with some problematic Sandisk.

If you can, please post a list of the working ones, so we have a reference.

For reference, this is the working SanDisk 4GB Class 4.

Thank you.

We should receive these days 2 different models/brands (the ones beta testers have reported problems).

I should put a list of tested SDs in a separate thread or in the documentation. There is also a new improved SD handling code in queue for version 1.0.2.

Hi @Mangaman.

We’ve ordered 8GB SD card from two different places and we received 16GB instead! Could it be that they are going out of production? How long do you have this SD?

By the way, these SD work without problems.

Hi @Ivan, i got it from Amazon, bought last year.

got one today from Amazon.

Here is the link:
https://www.amazon.it/gp/product/B001CQT0X4/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1