Uploading files to SD card to be read from the ESP32

In this tutorial we will learn how to upload files to a SD card, to be read by the ESP32. We will be using the Arduino core to program the device.

Introduction

In this tutorial we will learn how to upload files to a SD card, to be read by the ESP32. We will be using the Arduino core to program the device.

When we analyzed how to work with the SPIFFS file system, we have used a very useful tool to upload files directly to it, without having to write an Arduino program just to add the file.

When using a SD card, we actually don’t need any specific software to upload the files. We simply need to connect our SD card to a computer and copy the files to it. That way, they will be available for reading when we connect the SD card to the ESP32.

So, the first thing we will do is connecting our SD card to a computer. After that, we will create a .txt file with some content, like shown in figure 1. I’ve named the file “sdtest.txt” but you can name it how you want, as long as you use that name later, when reading it on the ESP32.

Creating the testing .txt file.
Figure 1 – Creating the testing .txt file.

After that, save the file in your SD card, like shown on figure 2.

Saving the file on the SD card.
Figure 2 – Saving the file on the SD card.

After this you can disconnect the SD card from your computer and connect it to your ESP32. I’ll be using a HW-818 ESP32 board model, which already has a SD card socket (you can find the link to buy it here).

Like already covered in the previous tutorials, for this particular board, we should use the SD_MMC library from the Arduino core. Depending on your hardware setup, you might need to use the SD_MMC or the SD library. You can check the difference between them here.

The code

The code for this tutorial is the same we already covered on this previous tutorial on how to read a file from a SD card with the ESP32. The only difference is that we already have the file on the file system, so we won’t include here the code to write it before reading.

As a quick reminder from the previous tutorial, the steps to read the file are the following:

  • Mounting the SD Card;
  • Opening the file in reading mode;
  • Reading the file byte by byte, while there is content available;
  • Closing the file.

The complete 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("/sdtest.txt", FILE_READ);
 
  if (!file) {
    Serial.println("Opening file to read failed");
    return;
  }
 
  Serial.println("File Content:");
 
  while (file.available()) {
    Serial.write(file.read());
  }
 
  file.close();
}
 
void loop() {}

Testing the code

Testing the code is very simple. We just need to compile it and upload it to the ESP32, using the Arduino IDE.

When the procedure finishes, open the Arduino IDE serial monitor. You should get a result similar to figure 3. As can be seen, we got the content that we originally set in our text file, as expected.

Output of the program, showing the content from the uploaded file.
Figure 3 – Output of the program, showing the content from the uploaded file.

Leave a Reply

%d bloggers like this: