SD Card doesnt work

Hi,
i tried to save data recieved from serial port to a sd card
I have wired up everything like this:

and my code is this:

#include <SD.h>
String gelesen;

void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
SD.begin(4);
File dataFile;
}

void loop()
{

gelesen = Serial.readString();

if(gelesen!="")
{
Serial.println(gelesen);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(gelesen);
dataFile.close();
}
else {
Serial.println("SD Error");
}

Serial.println(gelesen);
}
}

but the arduino isnt able to write the file as the serial monitor prints "SD Error" as soon as the data is recieved.

edit:
I exchanged the sd.serialbegin() with

if (!SD.begin(4))
{
Serial.println("Card failed to initialize, or not present");

return;
}

now it prints "Card failed to initialize, or not present" ... so it seems the serialbegin comment doesent work ?

pls excuse my bad english

Is the card properly formatted? There is a note on this at the head of this forum.
Have you tried the standard read/write example included in the IDE?

Thema SD Card is formated in FAT
I tried the example...doesnt work

loois95:
Thema SD Card is formated in FAT

That is not necessarily an answer.

Use tag image to post it. (screen icon)


Try this:

void setup() {
   Serial.begin(9600);
   pinMode(10, OUTPUT);
   digitalWrite(10, HIGH); // disable SPI
   if (!SD.begin(4) {
       Serial.println("Card failed, or not present");
       // don't do anything more:
       return;
   }
   Serial.println("card initialized.");
   File dataFile;
}

Maybe the formatting trick works :
https://forum.arduino.cc/index.php?topic=445696.msg3068201#msg3068201
(or try the latest IDE version 1.8.1)

I recently got a bad (name brand) SD-card. So write it all the way full and read it back to check the SD-card itself and/or try another one.