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.
bool begin(
uint8_t ssPin = SS, SPIClass &spi = SPI, uint32_t frequency = 4000000, const char *mountpoint = "/sd", uint8_t max_files = 5, bool format_if_empty = false
);
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:
opened 03:20PM - 12 Jan 23 UTC
closed 02:34PM - 09 Jan 25 UTC
Status: Awaiting triage
Area: Libraries
### Board
ESP32 Dev Board
### Device Description
ESP32 Dev kit.
Micro SD ca… rd breakout board (3.3V).
### Hardware Configuration
GPIO 5 connected to CS
GPIO 23 connected to MOSI
GPIO 18 connected to CLK
GPIO 19 connected to MISO
### Version
v2.0.6
### IDE Name
PlatformIO (VS Code)
### Operating System
Ubuntu
### Flash frequency
40Mhz
### PSRAM enabled
no
### Upload speed
921600
### Description
_Most_ SD cards will not connect. The sketch compiles and runs, but when trying to mount SD cards produces an error.
Using the SD library communicating by SPI _most_ SD cards do not mount. I have five identical (same size, type, brand etc.) micro SD cards, one of them always connects and runs perfectly. The other four do not mount at all.
They all mount to my computer and have all been formatted as FAT32.
I have tried directly wiring up the SD cards and I get the same behaviour (four don't work and one does).
I have tried powering off a power supply to ensure that isn't the issue but the behaviour remains the same.
The error message that I get is:
`ff_sd_initialize(): APP_OP_COND failed: 1`
This is different to the error that I get when the SD cards are not connected which is:
`ff_sd_initialize(): GO_IDLE_STATE failed`
### Sketch
```cpp
#include "FS.h"
#include "SD.h"
#include "SPI.h"
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (!SD.begin())
{
Serial.println("Card Mount Failed");
}
else
{
Serial.println("Worked");
}
delay(2000);
}
```
### Debug Message
```plain
[ 72613][W][sd_diskio.cpp:550] ff_sd_initialize(): APP_OP_COND failed: 1
[ 72613][E][sd_diskio.cpp:805] sdcard_mount(): f_mount failed: (3) The physical drive cannot work
```
### Other Steps to Reproduce
I have also reproduced this soldering the pins of a micro SD card adaptor to the ESP32 with the correct pull-ups. The behaviour is identical.
I have tried two different dev boards and the behaviour persists.
### I have checked existing issues, online documentation and the Troubleshooting Guide
- [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.
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: