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
}
If all goes well, you should see the following output if this is the first time writing to the card!
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
If you are looking to go the extra mile to see if data was saved, close the Serial Monitor and remove power from
the MicroMod Main Board. Eject your microSD card from the socket and insert into a microSD card adapter. Then
insert the microSD card into your computer's card reader or USB port. Open the
test.txt
file in a text editor. You
should see an output similar to what you saw in the Serial Monitor after the file was opened as shown below.
testing 1, 2, 3.
Besides verifying the data in the file, this step is also useful if you adjust the code to continuously log data in a
CSV format. After logging data, you could the open text document in a spreadsheet and graph the values!