Arduino Nano ESP32 - SD Card

In this guide, we'll explore how to use a Micro SD Card with the Arduino Nano ESP32. We'll delve into the following topics:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×USB 3.0 SD Card Reader
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Micro SD Card Module

The Micro SD Card Module can interface with Arduino Nano ESP32 and it can carry a Micro SD Card. In another word, the Micro SD Card Module is a bridge between Arduino Nano ESP32 and Micro SD Card

Pinout

Micro SD Card Module Pinout

Micro SD Card Module includes 6 pins:

  • VCC pin: connect to the ESP32's 5V pin.
  • GND pin: connect this pin to the ESP32's GND.
  • MISO pin: (Master In Slave Out) connect this pin to the ESP32's MOSI pin.
  • MOSI pin: (Master Out Slave In) connect this pin to the ESP32's MISO pin.
  • SCK pin: connect this pin to the ESP32's SCK pin.
  • SS pin: (Slave Select) connect this pin to the pin specified in Arduino Nano ESP32 code as a SS pin.

Preparation

  • Connect the Micro SD Card to the PC via USB 3.0 SD Card Reader
  • Make sure that the Micro SD Card is formatted FAT16 or FAT32 (Google for it)

Wiring Diagram

  • The wiring diagram when powering the Arduino Nano ESP32 board via USB port.
The wiring diagram between Arduino Nano ESP32 and Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram when powering the Arduino Nano ESP32 board via Vin pin.
The wiring diagram between Arduino Nano ESP32 and Micro SD Card Module external power

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 - How to open a file on Micro SD Card and create if not existed

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS D4 // The Arduino Nano ESP32 pin connected to the CS pin of SD card module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); if (!SD.exists("/esp32.txt")) { Serial.println(F("esp32.txt doesn't exist. Creating esp32.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("/esp32.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("/esp32.txt")) Serial.println(F("esp32.txt exists on SD Card.")); else Serial.println(F("esp32.txt doesn't exist on SD Card.")); } void loop() { }

Detailed Instructions

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Open Arduino IDE on your PC.
  • Select the right Arduino Nano ESP32 board (e.g. Arduino Nano ESP32) and COM port.
  • Insert the Micro SD Card to the Micro SD Card module
  • Do the wiring between the Micro SD Card module and Arduino Nano ESP32 as the above wiring diagram
  • Connect Arduino Nano ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Open Serial On Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
  • The result on Serial Monitor for the first run
COM6
Send
SD CARD INITIALIZED. esp32.txt doesn't exist. Creating esp32.txt file... esp32.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • The result on Serial Monitor for the next runs
COM6
Send
SD CARD INITIALIZED. esp32.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may NOT see the output on Serial Monitor for the first run of your first upload is done before opening Serial Monitor.

  • Detach the Micro SD Card from the module
  • Insert the Micro SD Card to an USB SD Card reader
  • Connect the USB SD Card reader to the PC
  • Check if the file exists or not

Arduino Nano ESP32 - How to write/read data to/from a file on Micro SD Card

The below code does:

  • Write data to a file
  • Read the content of the a file character-by-character and print it to Serial Monitor
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS D4 // The Arduino Nano ESP32 pin connected to the CS pin of SD card module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("/esp32.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }
  • The Serial Monitor shown the content of the file
COM6
Send
Created by esp32io.com Learn Arduino Nano ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The data will be appended to the end of file by default. If you reboot Arduino Nano ESP32 with above code, the text will be appended to the file again ⇒ the Serial Monitor will shows more lines as below:

COM6
Send
Created by esp32io.com Learn Arduino Nano ESP32 and SD Card Created by esp32io.com Learn Arduino Nano ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can also detach the Micro SD Card from the module, and open it on your PC to check the content (USB SD Card reader is needed)

Arduino Nano ESP32 - How to read a file on Micro SD Card line-by-line

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS D4 // The Arduino Nano ESP32 pin connected to the CS pin of SD card module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("/esp32.txt", FILE_READ); if (myFile) { int line_count = 0; while (myFile.available()) { char line[100]; // maximum is 100 characters, change it if needed int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // print the character to Serial Monitor // \n character is escaped by readBytesUntil function Serial.write('\n'); // print a new line charactor } myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }
  • The result on Serial Monitor
COM6
Send
SD CARD INITIALIZED. Line 1: Created by esp32io.com Line 2: Learn Arduino Nano ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may see more lines on Serial Monitor if the content of the file is not deleted before.

Arduino Nano ESP32 - How to overwrite a file on Micro SD Card

By default, the content will append to the end of the file. The simplest way to overwrite a file is: delete the exsiting file and create new one with the same name

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sd-card */ #include <SD.h> #define PIN_SPI_CS D4 // The Arduino Nano ESP32 pin connected to the CS pin of SD card module File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); SD.remove("/esp32.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("/esp32.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }
  • The result on Serial Monitor
COM6
Send
SD CARD INITIALIZED. Created by esp32io.com Learn Arduino Nano ESP32 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Reboot Arduino Nano ESP32
  • Check if the content of the file on Serial Monitor is appended or not.

You can also detach the Micro SD Card from the module, and open it on your PC to check the content (USB SD Card reader is needed)

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!