Reading SD Card with ESP32 Troubles

Hello I want to use a ESP32 to play audio with an SD card but I have ran into an issue in the first step which is actually reading the SD card as every time I run this code with wires attached to the pins referenced in the code it comes out with "Error accessing microSD card!" in the serial monitor. It is using an adafruit MicroSD card breakout board. And my ESP32 is a
Espressif ESP32-WROOM-32D. The SD Card is a SanDisk 32GB microSD HC card.


// Include required libraries
#include "Arduino.h"
#include "SD.h"
#include "FS.h"
 
// microSD Card Reader connections
#define SD_CS          5
#define SPI_MOSI      23 
#define SPI_MISO      19
#define SPI_SCK       18
 
 
void setup() {
    
    // Set microSD Card CS as OUTPUT and set HIGH
    pinMode(SD_CS, OUTPUT);      
    digitalWrite(SD_CS, HIGH); 
    
    // Initialize SPI bus for microSD Card
    SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
    
    // Start Serial Port
    Serial.begin(115200);
    
    // Start microSD Card
    if(!SD.begin(SD_CS))
    {
      Serial.println("Error accessing microSD card!");
      while(true); 
    }
    if(SD.begin(SD_CS))
    {
      Serial.println("READ microSD card!");
      while(true); 
    }
    
}
 
void loop()
{   
  
}

SD cards do NOT sense a CPU reset, you must powercycle to get the cards to restart after sw update. No mem cards bigger than 32gb and only DOS 8.3 file/directory names.

1 Like

Sorry I don't know what you mean?

If its possible could you please give me a solution to my problem? E.G. different code or different wiring? I'm sorry if I appear rude I'm not trying to.

You need only one SD.begin(). Get rid of the other.

How about power and ground? Did you solder the connections?

Please post a photo of a hand-drawn wiring diagram, with all the pins and connections clearly labeled. A close up, focused photo of your setup would help, too.

Ok I will get rid of the other and no they are not soldered but I pull the sd card module back so all the pins contact I know that is a contender of the problem but I have seen other people have this issue and there's are soldered. I may try soldering them though because I know that could be a major problem.


That won't work, regardless of what "other people" report. Soldering is required.

Sparkfun and Adafruit have good soldering tutorials.

When you download to your device that controls the SD card, the device does a reset. There is no "reset" signal to the SD card. If the SD card was active during the CPU reset, the SD card is unaware that the CPU restarted and gets confused when sent an unexpected command like sd.begin(). Thus, you must POWER CYCLE your project to get the SD card to work after an update. Simply pressing the reset button will NOT restart the SD card.
Additionally, the library for the SD card limits you to 32gb max and DOS 8.3 file names.
If any one of these conditions isn't met, your card will not work as expected.
Pull the SD off the breadboard and connect to the pins directly. Put a shroud over the dupont connectors to keep the pins aligned and to increase retention.


SD card is vertically mounted at left. Note white shroud over dupont pins. Bracket and hole allows access to change SD card without opening project case. Mount and shrouds are 3D printed and available for free download at cults3d.com

Schematic for common cards on Mega 2560:

I knew that from the start but I thought id try sorry for wasting your time for what will probably be an easy solution thanks for the suggestions for tutorials as I have never soldered before.

I recommend to practice soldering on discarded PCBs (from toys or recycled electronics).

How do I power cycle my project and my SD card is 32GB so will it still work and as always thank you for the information.

I have a ton of them as I like opening up and taking out the PCB from old broken electronics.

That is usually done after the uploading, either automatically, or you may have to press a button.

Remove the power ?

Indeed so.

Its very common when re-programming to have to remove an SD card, effectivly powering it down, in order to get it to work again.

Thank you for the information I will be sure to learn how to solder and hopefully get this working for my project and I'm sorry If I don't understand things as I'm still learning about coding and electronics.

If you use the shrouds as shown, no soldering is needed.
Soldering a ribbon cable directly to the card is NOT a good idea. The wires will BREAK from flexing at the end of the solder, making the card effectively impossible to repair. A pin header with duponts is better. Shrouds is better yet.
Getting things to work on breadboard is one thing. Integrating it into a solid, long term usable, project is another.

Ok I have just seen that and it looks good but I think when I get to the final stages of my project I will use a custom PCB so I will solder the breakout board to that using its included pins.

In this photo

The power jumper is connected to 3V3.

In this photo

the power is applied to the 5V pin.

Weird.

That is how the tutorial said to connect them.