ESP32 detects SD card without being connected - Help

Hello!

I'm trying to make code in Esp32 that checks if the microSD card is mounted to the module every 5 seconds but it doesn't work. It works only during the first startup of the "setup()" but not in the "loop()" anymore.
I have tried doing the same with Arduino Nano and it works correctly.
I need help to solve this problem.

Resources used:

Note: I connected the 3V3 pin of the Esp32 directly to the output of the 3.3V regulator of the microSD module since if I connected it to the VCC of the module it did not turn on.

Esp32 code:

//ESP32 Dev Module
#include "FS.h"
#include "SD.h"
#include "SPI.h"

/*
Defaul Pins: 
    cs = 5 
  MOSI = 23
  MISO = 19
  SCLK = 18
*/

const int sck = 14;
const int miso = 12;
const int mosi = 13;
const int cs = 15;

bool detect_sd(){
  if(!SD.begin(cs)){
    return false;
  }
  else{
    return true;
  }
}


void setup() {
  SPI.begin(sck, miso, mosi, cs);

  Serial.begin(115200);
  while(!Serial){
    delay (1000);
  }
  if(detect_sd() == true){
    Serial.println("- Micro SD : Mounted card");
  } 
  else if(detect_sd() == false){
    Serial.println("- Micro SD : Nunmounted card");
  }
  delay(100);
}


void loop() {
  //SD
  if(detect_sd() == true){
    Serial.println("- Micro SD : Mounted card");
  } 
  else if(detect_sd() == false){
    Serial.println("- Micro SD : Nunmounted card");
  }
  delay(5000);
}

I rebooted the Esp32 without the microSD then inserted it into the module and then removed it and then inserted it again. And I am getting this in Serial, which is NOT CORRECT. I tried with the Esp32's default SPI pins and it doesn't work either.

- Micro SD : Nunmounted card
- Micro SD : Nunmounted card
- Micro SD : Mounted card
- Micro SD : Mounted card
- Micro SD : Mounted card
- Micro SD : Mounted card
- Micro SD : Mounted card
- Micro SD : Mounted card

Arduino Nano Code:

//Arduino Nano
#include "SD.h"

/*
Defaul Pins: 
    cs = 10
  MOSI = 11
  MISO = 12
  SCLK = 13
*/

const int cs = 10;

bool detect_sd(){
  if(!SD.begin(cs)){
    return false;
  }
  else{
    return true;
  }
}


void setup() {
  Serial.begin(9600);
  while(!Serial){
    delay (1000);
  }
  if(detect_sd() == true){
    Serial.println("- Micro SD : Mounted card");
  } 
  else if(detect_sd() == false){
    Serial.println("- Micro SD : Nunmounted card");
  }
  delay(100);
}


void loop() {
  //SD
  if(detect_sd() == true){
    Serial.println("- Micro SD : Mounted card");
  } 
  else if(detect_sd() == false){
    Serial.println("- Micro SD : Nunmounted card");
  }
  delay(5000);
}

I rebooted the Arduino Nano without the microSD then inserted it into the module and then removed it and then inserted it again. And I get this in the Serial, which is CORRECT.

- Micro SD : Nunmounted card
- Micro SD : Nunmounted card
- Micro SD : Mounted card
- Micro SD : Mounted card
- Micro SD : Nunmounted card
- Micro SD : Nunmounted card
- Micro SD : Mounted card
- Micro SD : Mounted card

Please help!
Greetings!

The SD card module you are using is designed for 5V logic Arduinos like the Nano.

For an ESP32 you should be using a SD card module that is designed for 3.3V logic systems, they look like this;

Screenshot - 13_12_2023 , 11_14_20

That's right, that's why I connected the 3.3V voltage directly to the output of the AMS1117 regulator, it's as if it no longer exists. And everything would be running at 3.3V. The Esp32 recognizes the microSD and I can even save data inside it. But the problem is when consulting SD.begin(), it returns true with the microSD unmounted, the correct thing is that it returns false.
In any case, I will try to get the module you mention and I will try to test the code.
Greetings!

Apart from the logic level converter sitting on all the SPI pins for the SD.

Well, according to the level converter datasheet it mentions that it can operate with 3.3V or 5V devices.
But I think the most convenient thing is to try the module you mention. I will try to get it as soon as possible.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.