micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

I have purchased 2 micro SD card holder for my esp project.

First I would like to test with heltec esp32 lora oled module.Image result for heltec lora oled
this module uses:
SS(lora)=18, then I assigned 21 for sd SS

and there is a lib in esp32-arduino, which use SS default at 5

I took the example and use SD.begin(21), but I alway get the error:
entry 0x40078a9c
Card Mount Failed

But I do saw once that it read out the sd card type and volume.

Not sure it's the issue of lora sd card swithing, or it just can't work.

The other esp32 card I have is an wemos esp32 oled, it did not leave me too much pins, I need to find out how to connect sd to it.
Image result for we mos esp32 oled sd
But I have wemos D1 mini esp8266 on hand, and connect it as:
 * The WeMos Micro SD Shield uses:
 * D5, D6, D7, D8, 3V3 and G
 * The shield uses SPI bus pins:
 * D5 = CLK
 * D6 = MISO
 * D7 = MOSI
 * D8 = CS

It's not working with the example in the arduino IDE 1.8.5, which marked SD(esp8266), but it works with this one:

so I know the wiring is corrected, and the 4 G sd card is not a problem.

Comes back to esp32,

Try another one:
using lib here

using the example in the lib, or the example in the page(both use different lib), after changing the pin assignment, I still could not make it work on heltech oled lora board. 

  //if (!SD.begin(21, 27, 19, 5)) { //spi on heltec lora oled, with 21 for sd cs

I take another board: wemos lolin esp32 oled

using this pin assignment:
  if (!SD.begin(14, 25, 13, 15)) { //spi on wemos oled, with 14 for sd cs 
  //SD.begin(uint8_t cs , int8_t mosi , int8_t miso , int8_t sck):  it works correctly.
I think we need to make more pin discovery on heltec 32 lora
Heltec 32 lora OLED
using this code and pin assignment as following:
 //SD.begin(uint8_t cs , int8_t mosi , int8_t miso , int8_t sck): 

  if (!SD.begin(21, 27, 19, 5)) { //spi on heltec lora oled, with 21 for sd cs, and pull GPIO18 to high(CS of lora)
And I wired pin 18 to 3.3v, which is the cs of lora to de-select it, and it works!
#include <mySD.h>File root;void setup(){  Serial.begin(115200);  Serial.print("Initializing SD card...");  /* initialize SD library with SPI pins */  //SD.begin(uint8_t cs , int8_t mosi , int8_t miso , int8_t sck):   if (!SD.begin(21, 27, 19, 5)) { //spi on heltec lora oled, with 21 for sd cs, and pull GPIO18 to high(CS of lora)  //if (!SD.begin(14, 25, 13, 15)) { //spi on wemos oled, with 14 for sd cs    }    Serial.println("initialization failed!");    return;  }  Serial.println("initialization done.");  /* Begin at the root "/" */  root = SD.open("/");  if (root) {        printDirectory(root, 0);    root.close();  } else {    Serial.println("error opening test.txt");  }  /* open "test.txt" for writing */  root = SD.open("test.txt", FILE_WRITE);  /* if open succesfully -> root != NULL     then write string "Hello world!" to it  */  if (root) {    root.println("Hello world!");    root.flush();   /* close the file */    root.close();  } else {    /* if the file open error, print an error */    Serial.println("error opening test.txt");  }  delay(1000);  /* after writing then reopen the file and read it */  root = SD.open("test.txt");  if (root) {        /* read from the file until there's nothing else in it */    while (root.available()) {      /* read the file and print to Terminal */      Serial.write(root.read());    }    root.close();  } else {    Serial.println("error opening test.txt");  }    Serial.println("done!");}void loop(){}void printDirectory(File dir, int numTabs) {    while(true) {     File entry =  dir.openNextFile();     if (! entry) {       break;     }     for (uint8_t i=0; i<numTabs; i++) {       Serial.print('\t');   // we'll have a nice indentation     }     // Print the name     Serial.print(entry.name());     /* Recurse for directories, otherwise print the file size */     if (entry.isDirectory()) {       Serial.println("/");       printDirectory(entry, numTabs+1);     } else {       /* files have sizes, directories do not */       Serial.print("\t\t");       Serial.println(entry.size());     }     entry.close();   }}

There is a similar board of TTGO, which included a u sd in new version:

TTGO-LoRa V2.0

I don't quite understand the pins used by the SD card, and the example code provided does not have sd card example.

In the esp32 for arduino  Arduino_projects\hardware\espressif\esp32\libraries\SD
there is also a lib for sd card, I have tested the example SD_test earlier, but with no enough knowage, it was not working. I first pull high the pin 18 which is used as ss on heltec board for lora.
This time I take the example and modif like this:
 // if(!SD.begin()){
        Serial.println("Card Mount Failed");

it seems to work, but you have to pay attention on the sd card contact, if the sd card is not in good contact, it would fial.

The I look into the SPI.h of esp32 arduino, it seems it work at 1000000 as frequency, so I tried also this:
but in hte esp32/libraries/SD.h, 
  bool begin(uint8_t ssPin=SS, SPIClass &spi=SPI, uint32_t frequency=4000000, const char * mountpoint="/sd");
so it seems the sd lib works spi at 4000000
so i also  play with :
it also works...
But I from time to time ran into card mount failed, then I re-insert the sd card, it works again.



#001 ESP32 UART0,1,2 with arduino IDE

AIS0. understanding AIS NMEA 0183: How it's coded