//MOSI - pin 11
//MISO - pin 12
//CLK - pin 13
//CS - pin 4
#include <SPI.h>
#include <SD.h>
const char* fileName = "test.txt";
File myFile;
void setup() {
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.");
myFile = SD.open(fileName, FILE_WRITE); //O_APPEND, O_CREATE
if (myFile) {
myFile.close(); // close the file
} else {
Serial.println("error opening file");
}
// re-open the file for reading:
myFile = SD.open(fileName);
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening file");
}
}
void loop() { }