Memory Card

The Memory Card library allows for reading from and writing to SD cards. It is built on sdfatlib by William Greiman. The library supports FAT16 and FAT32 file systems on standard SD cards and SDHC cards. It uses short 8.3 names for files. The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. "directory/filename.txt". Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. "/file.txt" is equivalent to "file.txt").

To use, import as a library, and specify #include <SD.h>.

SPI has two channels so that each channel can be used for the memory card. In the case of using an SPI2 (pin 27, 28, 29 and 30) interface, specify SD2. Note that Serial class for USB cannot be used when SD2 is used.

begin

Description

Initializes the SD library and card.

Syntax

SD.begin()
SD.begin(cspin)

Parameters

cspin (optional): The pin connected to the chip select line of the SD card; defaults to the hardware SS line(22) of the SPI bus.

Returns

True on success; false on failure.

exists

Description

Tests whether the target file or directory exists on the card.

Syntax

SD.exists(filename)

Parameters

filename: The name of the file to test for existence, which can include directories (delimited by forward-slashes, /).

Returns

True if the file or directory exists, false if not.

mkdir

Description

Creates a directory on the SD card. This will also create any intermediate directories that don't already exists; e.g. SD.mkdir("a/b/c") will create a, b and c.

Syntax

SD.mkdir(filename)

Parameters

filename: The name of the directory to create, with sub-directories separated by forward-slashes, /.

Returns

True if the creation of the directory succeeded, false if not.

open

Description

Opens a file on the SD card. If the file is opened for writing, it will be created if it doesn't already exist (but the directory containing it must already exist).

Syntax

SD.open(filepath)
SD.open(filepath, mode)

Parameters

filename: The name of the file to open, which can include directories (delimited by forward slashes, /) - char *
mode (optional): The mode in which to open the file, defaults to FILE_READ - byte. one of:
  -FILE_READ: Open the file for reading, starting at the beginning of the file.
  -FILE_WRITE: To write to the file.

Returns

A File object referring to the opened file; if the file couldn't be opened, this object will evaluate to false in a boolean context, i.e. you can test the return value with "if (f)".

remove

Description

Removes the specified file from the card.

Syntax

SD.remove(filename)

Parameters

filename: The name of the file to remove, which can include directories (delimited by forward-slashes, /).

Returns

True if the removal of the file succeeded, false if not. (If the file didn't exist, the return value is unspecified).

rmdir

Description

Removes a directory from the card.

Syntax

SD.rmdir(filename)

Parameters

filename: The name of the directory to remove, with sub-directories separated by forward-slashes, /.

Returns

True if the removal of the directory succeeded, false if not. (If the directory didn't exist, the return value is unspecified).


Sample Program

Please refer to SD (File Operations).