SD Card creates file but doesnt't write message in code

I use this code

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

File myFile;

// 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 = 4;

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...");
  // 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(SS, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    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
}

Wiring is right..

The serial monitor result is:

Which Arduino?
Is your Serial Monitor set to 9600 baud?

Pete

el_supremo:
Which Arduino?
Is your Serial Monitor set to 9600 baud?

Pete

I use Arduino Uno and I set Serial Monitor 9600 baud in result pic
Would you let me know if I set something wrong ? :frowning:

I don't know whether your wiring photo is too big or on a very slow site but I can't see it.

Pete

And a photo of the Serial monitor is a waste of space - especially when it is so big. Just select the text, copy it, and paste inside code tags in your message.

Pete

el_supremo:
And a photo of the Serial monitor is a waste of space - especially when it is so big. Just select the text, copy it, and paste inside code tags in your message.

Pete

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

uppic.org isn't working. The first image apparently has errors in it and won't load. For the other two, uppic.org is so horrendously slow that I never get to see even a partial image.

Pete

Which SD card are you using? Preferably with a link which shows it, and a link to a datasheet would be nice too.
Just write down which pins on the SD card adapter are connected to which pins on the arduino.

Pete

el_supremo:
Which SD card are you using? Preferably with a link which shows it, and a link to a datasheet would be nice too.
Just write down which pins on the SD card adapter are connected to which pins on the arduino.

Pete

i use kingston 8gb sdhc class 10

pins

3.3V -> 3.3V
Gnd -> Gnd
MISO -> 12
MOSI -> 11
SCLK -> 13
CS -> 4

tamtammz:
i use kingston 8gb sdhc class 10

pins

3.3V -> 3.3V
Gnd -> Gnd
MISO -> 12
MOSI -> 11
SCLK -> 13
CS -> 4

Arduino's do not produce enough 3.3V power to directly Drive an SDcard. The Uno can only produce 50mA of 3.3V, The SDcard specification requires 200mA minimum. I think your SDcard is failing due to lack of power.

Chuck.