Example Code for Arduino - SD Library (Basic Tutorial)

Hardware Preparation

FireBeetle 2 ESP32-S3 AI Board with OV2640 Camera x 1

Software Preparation

When you use the ESP32 for the first time, you need to know the following steps:

  1. Add the ESP32 development board in Arduino IDE (How to add the ESP32 board to Arduino IDE?)

  2. Select the development board and serial port

  3. Burn the program

Basic APIs

SD Class

  • begin(cspin)

    • Description: Initializes the SD library and SD card. This begins use of the SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51, and 52 on the Mega) and the chip select pin, which defaults to the hardware SS pin (pin 10 on most Arduino boards, 53 on the Mega). Note that even if you use a different chip select pin, the hardware SS pin must be kept as an output or the SD library functions will not work.

    • Parameter:

      • cspin:the Arduino pin connected to the chip select line of the SD card.
    • Return: boolean type. True on success; false on failure

  • exists()

    • Description: Test whether a file or directory exists on the SD card.
  • Syntax: SD. exists(filename)

    • Parameter:

      • filename: the name of the file to test for existence, which can include directories (delimited by forward-slashes, /)
    • Return: boolean type, true if the file or directory exists, 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( filename) SD.open(filename,mode)

    • Parameter:

      • filename: the file name to open, which can include directories (delimited by forward slashes, /)

      • mode (optional): the mode in which to open the file, defaults to FILE_READ - byte.

        • FILE_READ: open the file for reading;

        • FILE_WRITE: open the file for reading and writing.

    • Return: a File object referring to the opened file: if the file couldn't be opened, this object will evaluate to false in a boolean

  • remove()

    • Description: Remove a file from the SD card. If the file didn't exist, the return value is unspecified, so it is better to use SD. Exists (file name) to detect whether the file exists before removing the file.

    • Syntax: SD. remove( filename)

    • Parameter:

      • filename:the name of the file to remove, which can include directories (delimited by exclamatory mark, !)
    • Return: boolean type. True if the removal of the file succeeded, false if not.

  • mkdir(filename)

    • Description: Create a directory on the SD card.

    • Parameter:

      • filename: the name of the directory to create, with sub-directories separated by forward-slashes, /
    • Return: boolean type. True if the creation of the directory succeeded, false if not.

  • rmdir(filename)

    • Description: Remove a directory from the SD card. The directory must be empty.

    • Syntax: SD.rmdir( filename)

    • Parameter:

      • filename: the name of the directory to remove, with sub-directories separated by forward-slashes, /
    • Return: booleantype. True if the removal of the directory succeeded, false if not.

File Class

The file class provides the function of reading/writing files. The function of this class is very similar to that of the serial port related functions used before. The member functions are as follows.

  • available()

    • Description: Check if there are any bytes available for reading from the file.

    • Syntax: file. available()

    • Parameter:

      • file: an instance of the File class
    • Return: the number of bytes available

  • close()

    • Description: Close the file, and ensure that any data written to it is physically saved to the SD card.

    • Syntax: file. close()

    • Parameter:

      • file: an instance of the File class
    • Return: none

  • flush()

    • Description: Ensures that any bytes written to the file are physically saved to the SD card. This is done automatically when the file is closed.

    • Syntax: file.flush

    • Parameter:

      • file: an instance of the File class
    • Return: none

  • peek()

    • Description: Read a byte from the file without advancing to the next one.

    • Parameter:

      • file: an instance of the File class
    • Return: The next byte (or character), or -1 if none is available.

  • position( )

    • Description: Get the current position within the file (i.e. the location to which the next byte will be read from or written to).

    • Syntax: file. position()

    • Parameter:

      • file: an instance of the File class
    • Return: the position within the file

  • print()

    • Description: Print data to the file, which must have been opened for writing.

    • Syntax: file. print(data)file. print(data,BASE)

    • Parameter:

      • file: an instance of the File class

      • data:the data to print (char, byte, int, long, or string)

      • BASE(optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

    • Return: byte number to be transmitted.

  • println()

    • Description: Print data, followed by a carriage return and newline, to the File, which must have been opened for writing.

    • Syntax: file. println(data) file.println(data,BASE)

    • Parameter:

      • file: an instance of the File class

      • data (optional): the data to print (char, byte, int, long, or string)

      • BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

    • Return: byte number to be transmitted.

  • seek()

    • Description: Seek to a new position in the file, which must be between 0 and the size of the file (inclusive).

    • Syntax: file. seek( pos)

    • Parameter:

      • file:an instance of the File class

      • pos: the position to which to seek

    • Return: true for success, false for failure (boolean)

  • size()

    • Description: Get the size of the file.

    • Syntax: filue. size()

    • Parameter:

      • file:an instance of the File class
    • Return: the size of the file in bytes

  • read()

    • Description: Read from the file.

    • Syntax: file.read

    • Parameter:

      • file:an instance of the File class
    • Return: The next byte (or character), or -1 if none is available.

  • write()

    • Description: Write data to the file.

    • Syntax: file. write(data)file. write(buf,len)

    • Parameter:

      • file:an instance of the File class

      • data:the byte, char, or string (char*) to write

      • buf: an array of characters or bytes

      • len:the number of elements in buf

    • Return: the number of bytes written

  • isDirectory()

    • Description: Reports if the current file is a directory or not

    • Syntax: file.isDirectory()

    • Parameter:

      • file: an instance of the File class
    • Return:boolean. True if the file is a directory, false if not

  • openNextFile()

    • Description: Reports the next file or folder in a directory.

    • Syntax: file.openNextFile()

    • Parameter:

      • file: an instance of the File class that is a directory
    • Return: the next file or folder in the path

  • rewindDirectory()

    • Description: Back to the first file in the directory

    • Syntax: file.rewindDirectory()

    • Parameter:

      • file: an instance of the File class.
    • Return: None

Please refer to File-Examples-SD-SD Test.

Was this article helpful?

TOP