The function opens a file on the SD card. In the case the file is opened for writing, a new file will be created if it doesn't already exist (but the directory containing it must already exist).

Syntax, mode)


  • filename: the name the file to open, which can include directories (delimited by forward slashes, /). Allowed type: char * or String
  • mode: (optional) the mode in which to open the file. If it is not set, the defaults value is FILE_READ. The mode can be one of the following values:
    • FILE_READ: open the file for reading, starting at the beginning of the file.
    • FILE_WRITE: open the file for reading and writing, starting at the end of the file.


    • The filename should follows 8.3 filename convention. If not, it may fails to open.
    • If a file is not closed before openning it again, it may fails to open.


  • 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. For example, you can test the return value with if statement.

Example Code

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <SD.h> #define PIN_SPI_CS 4 File file; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println("SD CARD FAILED, OR NOT PRESENT!"); while (1); // don't do anything more: } file ="arduino.txt", FILE_READ); if (file) { Serial.println("SD Card: file is opened"); file.close(); } else { Serial.println("SD Card: file is failed to open"); } } void loop() { }


Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.