Arduino nano Esp32 - how to read and write with sd card module

Hi all, I'm trying to connect an SD card module to an Arduino Nano ESP32, but I can't get the card to be recognized.

My connection scheme is as follows:

  • Vcc --> 3.3V
  • GND --> GND
  • MISO --> D12
  • MOSI --> D11
  • SCK --> D13
  • CS --> D10

I've already tried the following:

  • The module works properly, as does the SD card, when connected to a standard Arduino Nano (ATmega328P).
  • The voltage levels are correct.
  • I've tried changing the pin assignments.
  • I've tried setting the correct pin numbering in the Arduino IDE.
  • I've tried connecting the SD module to another ESP32 board (ESP32-S3 Devkitc Wroom), but I got the same error.

It seems that either I'm not setting the pins correctly, or the libraries I'm using are not suitable for this configuration. Can anyone help?

Below you can find the latest sketch I used to test the connection:

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

#define CS 21  // D10 corresponding to GPIO21

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ;
  }

  delay(2000);
  Serial.println("Inizializing SD board...");

  // Inizializza i pin SPI e il pin CS
  SPI.begin(48, 47, 38, 21);  // SCK = GPIO48, MISO = GPIO47, MOSI = GPIO38, CS = GPIO21
  pinMode(CS, OUTPUT);

  // Inizializza la scheda SD
  if (!SD.begin(CS)) {
    Serial.println("SD board inzialization failed!");
    return;
  }
  Serial.println("SD board inizialized.");

  // Apri il file per scrivere
  File dataFile = SD.open("/LOG.csv", FILE_WRITE);

  if (dataFile) {
    dataFile.println("Hello test 1");
    dataFile.close();
    Serial.println("Data written into LOG.csv.");
  } else {
    Serial.println("Error while opening LOG.csv.");
  }
}

void loop() {
  
}

I tested your code in Wokwi using ESP32-S3, and it worked correctly, then I added the reading routine again and it worked correctly.

Check the connections between the SD and the ESP.

Checked. I've alredy done the same test with Wokwi with the same results.
Still card mount failed...
I checked the connection with a tester and seems that everything is working. I don't have other ideas...The same wires, with same module and the same SD card works properly with arduino nano and doesn't wrk with 2 different ESP32 boards (one arduino nano ESP and the ESP32-S3-Dev module)

@gimmy991 this works on my Nano

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

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ;
  }

  delay(2000);
  Serial.println("Inizializing SD board...");

  // Inizializza la scheda SD
  if (!SD.begin()) {
    Serial.println("SD board inzialization failed!");
    return;
  }
  Serial.println("SD board inizialized.");

  // Apri il file per scrivere
  File dataFile = SD.open("/LOG.csv", FILE_WRITE);

  if (dataFile) {
    dataFile.println("Hello test 1");
    dataFile.close();
    Serial.println("Data written into LOG.csv.");
  } else {
    Serial.println("Error while opening LOG.csv.");
  }
}

void loop() {
  
}

there are a couple of sketches in examples that are quite useful