4
This example shows how to read a file from the SD card using the
5
SD library and send it over the serial port.
8
* SD card attached to SPI bus as follows:
14
created 22 December 2010
16
This example code is in the public domain.
22
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
23
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
24
// 53 on the Mega) must be left as an output or the SD library
25
// functions will not work.
26
const int chipSelect = 4;
31
Serial.print("Initializing SD card...");
32
// make sure that the default chip select pin is set to
33
// output, even if you don't use it:
36
// see if the card is present and can be initialized:
37
if (!SD.begin(chipSelect)) {
38
Serial.println("Card failed, or not present");
39
// don't do anything more:
42
Serial.println("card initialized.");
44
// open the file. note that only one file can be open at a time,
45
// so you have to close this one before opening another.
46
File dataFile = SD.open("datalog.txt");
48
// if the file is available, write to it:
50
while (dataFile.available()) {
51
Serial.write(dataFile.read());
55
// if the file isn't open, pop up an error:
57
Serial.println("error opening datalog.txt");