0

I am working on a simple code to write data to a MicroSD card using Arduino nano ESP32 (ABX0083) and Df robot SPI MicroSD card module(DFR0229). I am not able to write to the SD card. I get the error condition "Error opening dta.txt for writing.".

The default pinout used is"Arduino". The code works well with Arduino UNO. Following is the code.

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

// Define the chip select pin for the SD card module const int chipSelect = 10;

void setup() { // Start the serial communication Serial.begin(9600); //pinMode(chipSelect, OUTPUT); pinMode(chipSelect, OUTPUT);

while (!Serial) { // Wait for the serial port to connect (needed for Leonardo and Micro) }

Serial.println("Initializing SD card...");

// Initialize the SD card if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // Don't do anything more while (1); } Serial.println("Card initialized.");

// Open the existing file for writing File dataFile = SD.open("dta.txt", FILE_WRITE);

// Check if the file opened successfully if (dataFile) { Serial.println("Writing to dta.txt...");

// Write some example data
dataFile.println(&quot;Appending new data&quot;);

// Close the file
dataFile.close();
Serial.println(&quot;Data written to dta.txt successfully.&quot;);

} else { // If the file didn't open, print an error Serial.println("Error opening dta.txt for writing."); } }

void loop() { // Nothing to do here }

Juraj
  • 18,264
  • 4
  • 31
  • 49
Rekcoj
  • 9
  • 1

2 Answers2

1

The code work for Arduino Uno because it is the API for Arduino Uno's SD library. For Arduino Nano ESP32, it is an ESP32, so you should check the example for the ESP32 SD library instead of the one for Uno.

There are subtle differences:

  1. unless you are going to re-assign the SPI port, you don't need to pass-in the CS to SD.begin();
  2. Unlike the Arduino Uno SD library, which is expecting to pass in a filename, the ESP32 SD.open()is inherited from ESP32 FS library, which is expecting a path instead of a filename when calling SD.open(), so it should be SD.open("/dta.txt", FILE_WRITE); instead of just dta.txt.
hcheung
  • 1,933
  • 8
  • 15
0

I struggled a bit getting my SD working on my nano esp32. What I did to wring everything out was to; format the sd card for FAT32, test the sd hardware with a nano. Once it worked with the nano, I moved it to the nano esp32 and it worked. Here's the code I used:

#include "SPI.h"

#define SD_MOSI 11 #define SD_MISO 12 #define SD_SCLK 13 #define SD_CS 10

#include <SD.h>

File myFile;

void setup() { Serial.begin(115200);

if (!SD.begin(SD_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(); }

}

void loop() { // 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."));

delay(3000); }