ESP32 reading file from SD card

In this tutorial we will check how to connect the ESP32 to a SD card and read a file from it.

Introduction

In this tutorial we will check how to connect the ESP32 to a SD card and read a file from it.

I’ll assume that the file did not exist beforehand in the SD card and include the code to write it. You can check the procedure for writing a file here. Naturally, if you already have a file in the file system that you want to use, you can skip the writing part.

In my case, I’ll be using a HW-818 ESP32 board model, which can be bought at eBay for around 10 Euros (you can find it here). This board contains a SD card socket that we can use to get started right away, without the need for additional electronics.

Nonetheless, please consider that the Arduino core has two libraries that can be used to interact with a SD card: the SD and the SD_MMC. You can check the difference between them here. For more details, you can also analyze Espressif’s documentation about the lower level drivers.

So, depending on the hardware configuration you are using, you should choose the correct one. For the ESP32 board I’m using, I will need to include the SC_MMC library.

The code

We will start by including the SD_MMC.h library. This will expose to us the SD_MMC extern variable, which we will use to mount the SD card and to write / read a file.

#include "SD_MMC.h"

Then we will move on to the Arduino setup function. The first thing we will do is opening a serial connection, to be able to output content from our program.

Serial.begin(115200);

Then we will take care of mounting the SD card. This is done simply by calling the begin method on the already mentioned SD_MMC variable. As covered in previous tutorials, this method returns a Boolean indicating if the mounting procedure was successful or not, which we will use for a simple error checking.

if(!SD_MMC.begin()){
    Serial.println("Failed to mount card");
    return;
}

From this point onward, we interact with the SD_MMC variable like we do with other file systems available on the ESP32 (SD_MMC is an object of class SDMMCFS, which inherits from the FS class, the file system wrapper class used also by other file system implementations, such as SPIFFS).

On this tutorial, I’m assuming that the file doesn’t exist beforehand on the SD card. So, we will start by creating a file called “/test.txt” and write some content to it. The procedure to write a file can be seen in detail on this previous tutorial.

File file = SD_MMC.open("/test.txt", FILE_WRITE);

if (!file) {
  Serial.println("Opening file to write failed");
  return;
}

if (file.print("Test file write")) {
  Serial.println("File write success");
} else {
  Serial.println("File write failed");
}

file.close();

Once the file is written to the file system, we will then open it again in reading mode. This is done with a new call to the open method on the SD_MMC extern variable.

Recall that the file was called “/test.txt“. So, this string should be the first argument of the open method. As second argument we should pass the constant FILE_READ, so it is opened in reading mode.

File file2 = SD_MMC.open("/test.txt", FILE_READ);

As we did for writing the file, we will also confirm that the file was correctly opened in reading mode.

if (!file2) {
   Serial.println("Opening file to read failed");
   return;
}

After this, we will get the content from the file in a loop. To do so, we will call the available method on the File object, to check how many bytes are left to read. The returning value will be used as stopping condition for the reading loop.

To read an actual byte from the file, we just need to call the read method on our File object. This method returns the read byte, which we can directly print to the serial port.

while (file2.available()) {
  Serial.write(file2.read());
}

To finalize, we will also close the File we opened for reading.

file2.close();

The final code can be seen below.

#include "SD_MMC.h"

void setup() {

  Serial.begin(115200);

  if (!SD_MMC.begin()) {
    Serial.println("Failed to mount card");
    return;
  }

  File file = SD_MMC.open("/test.txt", FILE_WRITE);

  if (!file) {
    Serial.println("Opening file to write failed");
    return;
  }

  if (file.print("Test file write")) {
    Serial.println("File write success");
  } else {
    Serial.println("File write failed");
  }

  file.close();

  File file2 = SD_MMC.open("/test.txt", FILE_READ);

  if (!file2) {
    Serial.println("Opening file to read failed");
    return;
  }

  Serial.println("File Content:");

  while (file2.available()) {
    Serial.write(file2.read());
  }

  file2.close();
}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device. When the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1.

As can be seen, we were able to successfully write to the file and then read its content, as expected.

Output of the program, showing the content of the file.
Figure 1 – Output of the program, showing the content of the file.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading