Hardware Reference
In-Depth Information
which slave select pin should be used if your application does not use the default
hardware SS pin. Most shields will use the default hardware pin.
// See if the card is present and can be initialized:
if (!SD.begin(chipSelectPin)) {
Serial.println("Could not initialize SD card.");
// End the sketch gracefully
return;
}
Serial.println("SD Card initialized.");
Opening and Closing Files
The SD library can create, update, and delete i les on a FAT16/32 i lesystem. The
SD library (and indeed most programming environments) does not differentiate
between creating a i le and opening a i le. The system is told to open a i le. If
the i les exists, it will be opened. If it does not exist, an entry is created, and a
new blank i le is opened. To open a i le, call SD.open() .
file = SD.open(filepath);
file = SD.open(filepath, mode);
The filepath parameter, expressed as an array of char , is the name of the
i le to use or to create. If the i le does not exist, it will be created, but this func-
tion will not create folders. To specify a folder, use the slash ( / ) character.
The mode parameter can be one of two constants: FILE_READ or FILE_WRITE .
The FILE_READ constant tells the sketch to open the i le as read only. This is
the default setting if the mode parameter is omitted. The FILE_WRITE constant
opens the i le in read/write mode. SD.open() returns a File object, something
that describes and points to a i le. It is used as a reference to read, update, or
close i les. To open a i le, you must i rst create a File object, and then use that
object on subsequent i le actions:
File myFile;
myFile = SD.open("data.dat", FILE_WRITE);
It is also possible to check beforehand if a i le exists. To do this, use SD.exists() .
result = SD.exists(filename);
This function tests to see if a filename exists and returns true if it exists or
false if it does not exist.
After you perform any read or write operations, you must close the i le. This
is done using close() from the File class.
file.close();
 
Search WWH ::




Custom Search