Arduino:
Reading a text file

How to:

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }

  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }

  myFile = SD.open("example.txt");
  if (myFile) {
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("Error opening example.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Expected output on the serial monitor will be the contents of example.txt if everything is wired and initialized correctly.

Deep Dive

Historically, microcontrollers like Arduino had tiny memory and couldn’t handle files. But with SD card modules and larger onboard memories, we’ve got file I/O. Several libraries exist for this purpose, such as <SD.h>. It’s built on top of <SPI.h> for communication with the SD card via the SPI bus.

In terms of alternatives, you could use EEPROM (non-volatile memory) for small data or even connect an Arduino to a network and fetch files from a server. The <SD.h> library is a wrapper for lower-level functions, handling file management, reading, and writing in a way that’s similar to standard C++ streams.

Implementation on Arduino involves initializing the SD card module, opening the file, reading it until there’s nothing left to read, and then closing it to free resources. It’s essential to handle errors, like failing to initialize or open the file, as they’re common causes of headaches in file operations.

See Also