SD library won't write to card

Hello.
I'm using the following code

#include <SD.h>

File myFile;

boolean SDOK=false;

 

void setup(){

  pinMode(4,OUTPUT);

  Serial.begin(9600);

  SDOK=SD.begin();

  if (SDOK){

    Serial.println("init OK");

    myFile=SD.open("test.txt",FILE_WRITE);

    if (myFile){

      Serial.println("file OPEN");

      myFile.println("LINE ONE");

      Serial.println("prtn ONE");

      myFile.write("LINE TWO");

      Serial.println("wrte TWO");

      myFile.flush();

      myFile.close();

    } else {

      Serial.println("file FAIL");

    }

  } else {

    Serial.println("init FAIL");

  }

}


void loop(){

}

it runs fine but I just wont write anything on my file as I confirm using this code:

#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!");
while (1);
}
Serial.println("initialization done.");
// 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());
}
delay(1000);
// 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
}

I found out this solution (SD library won't write to card - Arduino Forum - Arduino - element14 Community), but before spending money on something I'm not sure it will work, i would like to know your opinion before.

Need more information: which Arduino are you using, which SD card, which SD card module?

Please post a hand drawn wiring diagram.

Your last comment suggests that you have connected a 5V Arduino to a 3.3V SD module without using level shifters, in which case the SD module may already have been destroyed.

That may also explain the problem in your other thread on this topic.

In addition to the above suggestions, if things don't appear to work as expected, it's a good idea to see if there are any known working example sketches that you can use to assist in narrowing down the problem.

Have a look for the SD example sketch called ReadWrite in the IDE.

And as I suggested in your previous thread, make sure that you are formatting the SD card correctly.

hello, thank you for your answer.
I'm using a transcend sd 2gb
I got exacly this (except is not micro sd module, but MH-SD card module, but connections are the same):

regarding my last comment, I meant that I read that all arduino board pins have a 5 V input (from power source to arduino board), and one of those pins that I connect to my sd card module is 3.3 V (from arduino to sd card module). It's like there was some incompatibility a board using 5V and having an output to 3.3 V. I think the ideia was to turn the arduino board all to 3.3V with that shift lever.. I heard some people saying it did the trick, but before I buy something I would like to know more opinions

I doubt my card is destroyed becouse I can insert it on my computer and format it. I doesn't seem burn.. Also, I manualy wrote a couple numberes on my txt and the code that I use to read works and shows in arduino console what txt file has.
also sample code from arduino about card info works nicely as you can see in the following image:

It just doesnt write on the file.. like it ignored myFile.println("text");

Yes I tried to use the arduino inbuilt example ReadWrite as follows:

/*
  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 4 (for MKRZero SD: SDCARD_SS_PIN)

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

got this

and was in this phase that I got a sd card full of those files with strange caracters.. Then I decided to create manually the txt file with the same name on the code and, despite not writting anything, at least it didn't give me all those strange files.

I would look into how the SD card is being formatted.

I did it with SD card formater software avaiable here:

Of course there is. That is the problem!

You MAY NOT CONNECT 5v to 3.3V devices and expect them to survive. Use a 3.3V Arduino with a 3.3V SD or with a 5V Arduino, use level shifters like this one.

I understand that, but then why did they make on the 5 V board a 3.3 V pin?
What is the use?

To power 3.3V devices.

You can safely INPUT a signal from a 3.3V device using a 5V Arduino, but not the other way.

but isn't sd a 3.3 V device?

The best approaches are to use an Arduino with a built in SD module, or use a 3.3V Arduino. You will probably need to buy another SD module anyway.

in my case I bought them separately:
in here you can see in the description that this sd module can withstand both 3.3 V and 5.0V

that's the reason of my confusion..

That card does not have level shifters, only a 5V to 3.3V voltage regulator. The product page clearly says "3.3V or 5V power."

Inputs are 3.3V ONLY.

1 Like

thanks :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.