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:
- SD Module : MicroSD Card Module (https://i0.wp.com/bestengineeringprojects.com/wp-content/uploads/2023/01/micro-sd-card-module.jpg)
- Software: Arduino IDE 2.2.1
- Board manager: esp32 2.0.14
- Selected board: ESP32 Dev Module
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!