Example Code for Arduino-SD Card Read & Write
Last revision 2026/01/26
The article offers example code for Arduino SD card read and write operations, detailing hardware and software preparation, coding for file management, and the execution results without loops.
Hardware Preparation
- FireBeetle ESP32 IoT Microcontroller(SKU: DFR0478) ×1
- Camera & Audio Media Board - FireBeetle Covers(SKU: DFR0498) ×1
- Micro SD Card (FAT32 File System) ×1
- USB Cable ×1
Software Preparation
- Download Arduino IDE: Click to Download Arduino IDE
- Download Arduino Library: Click to download DFRobot_IIS and refer to the guide: How to Install a Library?
Other Preparation Work
- This media board only supports FireBeetle ESP32 main board at present, for other main boards do not equipped with IIS interface.
- The flat ribbon cable of camera is relatively easy-to-break, please be careful and light in connection. The camera shot must be opposite to the SD card slot.
- Do not support video shooting and transmission.
- ESP32 SD_MMC library of official Arduino can be directly used in SD card socket of FireBeetle Covers-Camera&Audio Media Board.
- Plug SD card and download the below program to ESP32 mainboard.
Sample Code
SD card full file operations via SD_MMC, check card info, test read/write speed, no loop execution.
#include "FS.h"
#include "SD_MMC.h"
/*Print file list*/
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
/*Create content*/
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
/*Delete content*/
void removeDir(fs::FS &fs, const char * path){
Serial.printf("Removing Dir: %s\n", path);
if(fs.rmdir(path)){
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
/*Read file*/
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
}
/*Write file*/
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
}
/*Append Writefile*/
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
}
/*Rename file*/
void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
/*Delete file*/
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\n", path);
if(fs.remove(path)){
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
/*Test Read&White speed*/
void testFileIO(fs::FS &fs, const char * path){
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
if(file){
len = file.size();
size_t flen = len;
start = millis();
while(len){
size_t toRead = len;
if(toRead > 512){
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = millis() - start;
Serial.printf("%u bytes read for %u ms\n", flen, end);
file.close();
} else {
Serial.println("Failed to open file for reading");
}
file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
size_t i;
start = millis();
for(i=0; i<2048; i++){
file.write(buf, 512);
}
end = millis() - start;
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
file.close();
}
void setup(){
Serial.begin(115200);
if(!SD_MMC.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD_MMC card attached");
return;
}
Serial.print("SD_MMC Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
//Print filelist
listDir(SD_MMC, "/", 0);
//Create a folder named mydir in the root
createDir(SD_MMC, "/mydir");
//Print file list and the folder mydir is available
listDir(SD_MMC, "/", 0);
//Delete the folder mydir
removeDir(SD_MMC, "/mydir");
//Print file list and the folder mydir be cleared away
listDir(SD_MMC, "/", 2);
//Create a file hello.txt and white Hello into the file
writeFile(SD_MMC, "/hello.txt", "Hello ");
//Append World!\r to the end of Hello in the hello.txt
appendFile(SD_MMC, "/hello.txt", "World!\n");
//Read hello.txt and its content "Hello World!" will be printed to the Serial Port
readFile(SD_MMC, "/hello.txt");
//Delete foo.txt in the SD card
deleteFile(SD_MMC, "/foo.txt");
//Rename hello.txt as foo.txt
renameFile(SD_MMC, "/hello.txt", "/foo.txt");
//Read foo.txtand its contentHello World! will be printed to the Serial Port
readFile(SD_MMC, "/foo.txt");
//Test Read&White speed in SD card
testFileIO(SD_MMC, "/test.txt");
}
void loop(){
}
Result
Serial prints SD card info, execute all SD operations in order with logs, no loop after completion.

Was this article helpful?
