Page 1 of 1

How to connect SD Card on HSPI of ESP32-Wroom

Posted: Thu Jan 28, 2021 7:17 pm
by cverd516
I am trying to connect an SD card to the HSPI pins on ESP32-Wroom. I have read through the documentation but I am still fairly confused so please help me get this right for my next PCB.

So for the pins, I am going to use MOSI(GPIO13), MISO(GPIO12),CLK(GPIO14), and CS(GPIO15) which are the HSPI pins of the ESP32 Wroom. In the reference documentation (API Reference » Peripherals API » SDIO Card Slave Driver: Connections) it lists different pins (GPIO15,2,14,13 respectively). Are the pins I selected OK to use or should I be using these other pins listed in the documentation?

As for pullups I am also very confused. In the reference Documentation (API Reference » Peripherals API » SD Pull-up Requirements), it starts by saying
When an SD card is operating in SPI mode or 1-bit SD mode, the CMD and DATA (DAT0 - DAT3) lines of the SD bus must be pulled up by 10 kOhm resistors.
This implies that all the pins, aside from the SCLK will require pull up resistors.

However, if I scroll down to "Overview of Compatibility" there is a No Pull-ups bullet point. So then I assume I dont use pull ups. IF I scroll down to the GPIO12(DAT2) Bootstrap Conflict section, it says
(Recommended) Burn the flash voltage selection eFuses. This will permanently configure the internal regulator’s output voltage to 3.3 V, and GPIO12 will not be used as a bootstrapping pin. After that, connect a pull-up resistor to GPIO12.
So here it says I do need a pull-up resistor.

Scrolling further down past that, it says
"No Pull-up on GPIO12: Your module is compatible with the SDIO protocol. Just connect GPIO12 to VDD via a 10 kOhm resistor."
So after all of this I am completely confused on which SPI pins I need to pull up or not. Please specify for each pin whether I need or do not need pull up resistors on my PCB to connect an SD card.

Re: How to connect SD Card on HSPI of ESP32-Wroom

Posted: Fri Jan 29, 2021 5:03 am
by imanpakii
Hello,

These are the tips that I recommend:
1- No need for any pull-ups.
2- Don't connect any pin to IO12 since you will be facing the problem during the boot and using the SD.
3-I didn't connect the CS to any pins since I don't connect any other devices to SPI.
4- You can connect SCL, MISO, and MOSI to any pin but you need to configure them in the code. You can use the below code(I used it and it works)

Code: Select all

#include <Arduino.h>
#include "SD.h"
#include <Cubipax_SD.h>
/*//SPI V
#define SD_CS         21
#define SD_MOSI      23
#define SD_MISO      19
#define SD_SCK       18
*/
///SPI H

#define SD_MOSI      13
#define SD_MISO      5
#define SD_SCK       14

#define SD_CS_PIN 16

File myFile;
SPIClass SPISD(HSPI);
void SD_Init()
{
  //  SPIClass SPI2(HSPI);


    SPISD.begin(SD_SCK, SD_MISO, SD_MOSI);
    if (!SD.begin(SD_CS_PIN,SPISD)) {  //SD_CS_PIN this pin is just the dummy pin since the SD need the input 
    Serial.println(F("failed!"));
    return;
    }
    else Serial.println(F("SD read!"));
    myFile = SD.open("/test.txt", "a"); //append to file
  if (myFile)
  {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  }
  else
  {
    Serial.println("error opening test.txt to write");
  }
  myFile = SD.open("/test.txt", "r"); //read from file
  if (myFile)
  {
    Serial.println("test.txt:");
    String inString;  //need to use Strings because of the ESP32 webserver
    while (myFile.available())
    {
      inString += myFile.readString();
    }
    myFile.close();
    Serial.print(inString);
  }
  else
  {
    Serial.println("error opening test.txt to read");
  }
put the SD_Init(); to your setup() loop


Thank you
Iman