SPI and SD .begin methods - no parameters?

I’m trying to implement the SPI and SD functions to connect an SD card to my ESP32:

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

// SD Card pin definitions
#define SD_MISO 19;
#define SD_MOSI 23;
#define SD_CLK 18;
#define SD_CS 5;

In the setup() method of my main.cpp:

  // set up SD card
  SPI.begin(SD_CLK,SD_MISO,SD_MOSI,SD_CS);
  if (!SD.begin()) {Serial.println("SD CARD mount fail. :( ");}
  else {Serial.println("SD CARD mount success! :) ");}

However, I noticed that SD.begin and SPI.begin both only take without parameters. I.e. adding the SD_CLK, etc. values results in a compile error, but without parameters (i.e. SD.begin() and SPI.begin() ), it can compile and run. The problem with that is that I get an SD card mount fail. Full error is:

[   325][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00
[   332][E][sd_diskio.cpp:806] sdcard_mount(): f_mount failed: (3) The physical drive cannot work
[   641][E][sd_diskio.cpp:199] sdCommand(): Card Failed! cmd: 0x00

I’m wondering if this is because the begin() methods can’t be overloaded with parameters? Why is this?

Both classes have begin functions with parameters. But all parameters are provided with default values. This means, when the function is called without parameters, the default values are used.

Please show the full errror message.

Please show the full errror message.

I just get a red underline:

expected a ')'

This implied to me that for some reason the compiler will only take it if it’s passed w/o parameters.

The error is here! Remove the semicolons at the end of each line!

When defining macros you must not have semicolons at the end.
You can clearly see this in the screenshot:
#define SD_CSLK 18; expands to 18;

That means the line
SPI.begin(SD_CLK,SD_MISO,SD_MOSI,SD_CS); expands to
SPI.begin(18;,19;,23;,5;); which is invalid.

Correct declaration of macros:

#define SD_MISO 19
#define SD_MOSI 23
#define SD_CLK 18
#define SD_CS 5

Alternatively you can use constants:

const int SD_MISO = 19;
const int SD_MOSI = 23;
const int SD_CLK = 18;
const int SD_CS = 5;
1 Like

Thank you, that makes the error go away!
Unfortunately, it still says that the mount does not work.

[   522][E][sd_diskio.cpp:806] sdcard_mount(): f_mount failed: (3) The physical drive cannot work
[  1031][E][sd_diskio.cpp:126] sdSelectCard(): Select Failed

I’ve checked my connections, so I don’t think that’s the problem. Any other possible causes for these errors?

There still seems to be an unresolved issue.
Please see:

Some SD cards work, some do not.

1 Like

Ah, I see.

Is there a workaround for that issue? I’ve tried several different things, including 3 different types of SD cards, to get the mount to work.

I’m working with an ESP32-WROOM-32E but I list the board as “esp32dev” in the platformio.ini file.

The issue started with Espressif Arduino 2.0.6.

I haven’t tried SD’s card for a while. I don’t know if there was a change from Arduino version 2.x to 3.x. So you can try the latest available Espressif Arduino version 3.2:

platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20/platform-espressif32.zip

Revisiting.

Here’s my wiring setup for the SD card module.

Maybe something’s wrong? I’m doing a prototype at the moment so maybe the issue is just that it’s not soldered. I don’t know.

Also it seems like sometimes the 3.3V and 5V pins deliver inconsistent output so maybe that’s an issue, but when I tried to connect the module with a steady power source that didn’t really fix it either.

I’m also wondering if the kinds of SD cards used are the issue.

These are the three kinds I tried. Perhaps they’re a bit old.

Bottom left is another module I tried using.

These are the dev boards I’m using, btw.
https://a.co/d/6kpySbu

Did you try Arduino 3.2.0?

?

I don’t know if your “through the hole” wiring technique work reliable… I would solder some PINs on the board and use 2 bread boards to connect something: