ESP32 reboots when used with SD Card

Hi,

The below code is to get the temperature from AM2315C and store that in the SD Card(RTC for time). There are other things like call feature which I have not yet added.

Once the code reaches to check the SD Card, ESP32 goes into restart loop with the following error:

17:38:10.938 -> A10     : 0x00000005  A11     : 0x00001a3c  A12     : 0x000000df  A13     : 0x00060a23  
17:38:10.938 -> A14     : 0x00060a20  A15     : 0x00000001  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
17:38:10.938 -> EXCVADDR: 0x0000000c  LBEG    : 0x40087d0d  LEND    : 0x40087d2f  LCOUNT  : 0xffffffff  
17:38:10.972 -> 
17:38:10.972 -> 
17:38:10.972 -> Backtrace: 0x400d46c4:0x3ffc5590 0x400d4310:0x3ffc55b0 0x400d1cee:0x3ffc55e0 0x400d7e42:0x3ffc5620

If I remove the following code, it works fine without the SD card storage:

if (!SD.begin()) {
Serial.println("Card Mount Failed");
return;

#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "Wire.h"
#include "RTClib.h"
#include <Adafruit_AHTX0.h>
#include <WiFi.h>
#include "DHT.h"  

int HH,MM,DD,c = 0;

RTC_DS3231 rtc;
Adafruit_AHTX0 aht;
String date1;
String time1;
String filename;
String path;
String PHONE = "9841111111";
sensors_event_t humidity, temp;
float t, h;

void getpath() {
  path = "/" + filename;
  Serial.print("Path: ");
  Serial.println(path);
}

void getdate() {
  DateTime now = rtc.now();
  date1 = String (now.timestamp(DateTime::TIMESTAMP_DATE));
  Serial.print("Date: ");
  Serial.println(date1);
  DD = now.day();
  HH = now.hour();
  MM = now.minute();
  //time1 = String((rtc.getTimeStr());
}

void getFileName() {
  File close();
  filename = date1 + ".csv";
  Serial.print("File name: ");
  Serial.println(filename);
  time1 += String(DD);
  time1 += " -";
  time1 += String(HH);
  time1 += " -";
  time1 += String(MM);
  Serial.println(time1);

}

void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS &fs, const char * path , const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void writeCallfile(String PH){
String dataString = "";
  dataString += "Call to - ";
  dataString += PH;
   dataString += "at";
   dataString += String(time1);
  dataString += "\r\n";
  Serial.println("dataString-");
  Serial.println(dataString);
  appendFile(SD, path.c_str(), dataString.c_str());

}

void writeTGfile(String PH){
String dataStringTG = "";
  dataStringTG += "Sent TG Message to - ";
  dataStringTG += PH;
   dataStringTG += "at";
   dataStringTG += String(time1);
  dataStringTG += "\r\n";
  Serial.println("dataStringTG-");
  Serial.println(dataStringTG);
  appendFile(SD, path.c_str(), dataStringTG.c_str());

}

void setup() {
  Serial.begin(115200);
    if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  delay(1000);
 // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
    delay(200);
  }
  getdate();
  delay(500);
  getFileName();
  delay(500);
  getpath();
  delay(500);
  writeFile(SD, path.c_str(), "Log Details \r\n");
Serial.println("Done");
  delay(500);
}

void loop() {

aht.getEvent(&humidity, &temp);

  t = temp.temperature;
  h = humidity.relative_humidity;

Serial.print(" Temp = ");
Serial.println(t);
Serial.print(" Humidity = ");
Serial.println(h);
/*
if ((t>30)&& (t<32)){
  writeCallfile(PHONE);
  Serial.println("Writing to Card for call");}
  if (t>32){
  writeTGfile(PHONE);
  Serial.println("Writing to Card for TG");
  }
  */
  delay(2000);
}

You are calling SD.begin() twice

Jim, its only once, I added that to check if that works by disabling the if (!SD.begin()) works. I forgot to remove that line before posting.

You are using very long delays.
This always creates problems on the ESP32.

Then I suspect you have the SD card wired incorrectly

Jim, I am able to write to the SD card with all other code removed. Below code works with SD card.

// code to write file name based on the date to SD Card


#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "Wire.h"
#include "RTClib.h"
#include <Adafruit_AHTX0.h>
#include <WiFi.h>
#include "DHT.h"  

int c = 0;

RTC_DS3231 rtc;
Adafruit_AHTX0 aht;
String filename;
String path;
sensors_event_t humidity, temp;
float t, h;

void getpath() {
  path = "/" + filename;
  Serial.print("Path: ");
  Serial.println(path);
}

void getdate() {
  DateTime now = rtc.now();
  date1 = String (now.timestamp(DateTime::TIMESTAMP_DATE));
  Serial.print("Date: ");
  Serial.println(date1);
}

void getFileName() {
  File close();
  filename = date1 + ".csv";
  Serial.print("File name: ");
  Serial.println(filename);


}

void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS &fs, const char * path , const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}


void setup() {
  Serial.begin(115200);
    if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  delay(1000);


  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
    delay(200);
  }
  getdate();
  delay(500);
  getFileName();
  delay(500);
  getpath();
  delay(500);
  writeFile(SD, path.c_str(), "Log Details \r\n");
Serial.println("Done");
  delay(500);
}

void loop() {

}

I can't see why the code you just posted works and the one you posted earlier doesn't.

It is always worth considering if the power supply is the issue with things like this. I have experienced all sorts of weird behaviour/errors etc. only after many hours to discover it is the power supply is the issue (I have fallen for this myself many times - lol).
I find it takes a large capacitor across the power feed to smooth out the spikes from wifi access etc.

This second code does not have the delay(2000); in the loop.

There is nothing happening in the loop for the delay

Power is not an issue, I have given 5V 3A and tried with different supply. I have put 4700uF of capacitor for ESP32 and also to the SD Card Supply .The same code works fine either if I stop the SD begin or remove the AM2315C cable . Works very strange

@bencyp Hey Bencyp, I've tried to upload the sketch and saw some errors, because I don't use AHTX0 to measure temp. and humidity, but instead I'm using a DHT22. I also use CS on GPIO2 for the µSD adapter. Try this sketch if you like, you can also decomment the lines and use your libs and sensor. I don't either use the RTCLib but use instead the uRTCLib.
Here's the code:

// Wiring
/*
 * Connect the µSD card module to the following pins:
 *
 * µSD Card module    | ESP32
 *
 * CS                   SD D0/GPIO2
 * MOSI                 MOSI/GPIO23
 * VSS/GND              GND
 * VDD/VCC              5V
 * CLK/SCK              SCK/GPIO18
 * MISO                 MISO/GPIO19
 * Connect following pins from the DHT to the ESP32
 *   DHT      Resistor           ESP32
 *    1          -               3.3V
 *    2         10KΩ between pin 1 and 2
 *    2          -               32
 *    3          -               -
 *    4          -               GND
 * Connect following pins from the DS3231 to the ESP32
 * DS3231          ESP32
 * SDA             GPIO21/I2C1 DA
 * SDL             GPIO22/I2C1 CL
*/

#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "Wire.h"
#include "uRTCLib.h"
// #include "RTCLib.h"
//#include <Adafruit_AHTX0.h>
#include <WiFi.h>
#include "DHT.h"
int HH, MM, DD, c = 0;
//
uRTCLib rtc(0x68);
// Adafruit_AHTX0 aht; // I'm using DHT22
#define pinDHT22 32          // GPIO 32
#define DHTTYPE DHT22        // define sensor DHT22 (AM2302)
DHT dht(pinDHT22, DHTTYPE);  // Initialise sensor 16mHz use
String date1;
String time1;
String filename;
String path;
String PHONE = "9841111111";
// sensors_event_t humidity, temp;
float t, h;

void getpath() {
  path = "/" + filename;
  Serial.print("Path: ");
  Serial.println(path);
}

void getdate() {
  rtc.refresh();
  Serial.print("Current Date & Time: ");
  Serial.print(rtc.year());
  Serial.print('/');
  Serial.print(rtc.month());
  Serial.print('/');
  Serial.print(rtc.day());
  Serial.print(", time: ");
  Serial.print(rtc.hour());
  Serial.print(':');
  Serial.print(rtc.minute());
  Serial.print(':');
  Serial.println(rtc.second());
  Serial.print("Temperature: ");
  Serial.print(rtc.temp() / 100);
  Serial.print("\xC2\xB0");  //shows degrees character
  Serial.println("C");
  date1 = String(rtc.day());
  date1 += String(rtc.month());
  date1 += "20";
  date1 += String(rtc.year());
  // DateTime now = rtc.now();
  // date1 = String(now.timestamp(DateTime::TIMESTAMP_DATE));
  Serial.print("Date: ");
  Serial.println(date1);
  /*
  DD = now.day();
  HH = now.hour();
  MM = now.minute();*/
  //time1 = String((rtc.getTimeStr());
  DD = rtc.day();
  HH = rtc.hour();
  MM = rtc.minute();
}

void getFileName() {
  File close();
  filename = date1 + ".csv";
  Serial.print("File name: ");
  Serial.println(filename);
  time1 += String(DD);
  time1 += " -";
  time1 += String(HH);
  time1 += " -";
  time1 += String(MM);
  Serial.println(time1);
}

void writeFile(fs::FS &fs, const char *path, const char *message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS &fs, const char *path, const char *message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void writeCallfile(String PH) {
  String dataString = "";
  dataString += "Call to - ";
  dataString += PH;
  dataString += "at";
  dataString += String(time1);
  dataString += "\r\n";
  Serial.println("dataString-");
  Serial.println(dataString);
  appendFile(SD, path.c_str(), dataString.c_str());
}

void writeTGfile(String PH) {
  String dataStringTG = "";
  dataStringTG += "Sent TG Message to - ";
  dataStringTG += PH;
  dataStringTG += "at";
  dataStringTG += String(time1);
  dataStringTG += "\r\n";
  Serial.println("dataStringTG-");
  Serial.println(dataStringTG);
  appendFile(SD, path.c_str(), dataStringTG.c_str());
}

void setup() {
  Serial.begin(115200);
  URTCLIB_WIRE.begin();
  if (!SD.begin(2)) {
    Serial.println("Card Mount Failed");
    return;
  }
  /*if (!aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }*/
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  // Comment out below line once you set the date & time.
  // Following line sets the RTC with an explicit date & time
  // for example to set August 11 2023 at 19:45 you would call:
  rtc.set(0, 45, 19, 5, 11, 8, 23);
  // rtc.set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
  // set day of week (1=Sunday, 7=Saturday)
  /*
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1)
      ;
    delay(200);
  }*/
  // DHT file
  dht.begin();
  delay(500);
  rtc.refresh();
  getdate();
  getFileName();
  delay(500);
  getpath();
  delay(500);
  writeFile(SD, path.c_str(), "Log Details \r\n");
  Serial.println("Done");
  delay(500);
}

void loop() {

  /*aht.getEvent(&humidity, &temp);

  t = temp.temperature;
  h = humidity.relative_humidity;
  */

  // Reading data
  h = dht.readHumidity();
  t = dht.readTemperature();
  bool DHTErrSuccess;
  DHTErrSuccess = true;
  if (t == NULL || int(t) == NAN) {
    DHTErrSuccess = false;
    return;
  }
  // Decomment to test the output
  /*
  Serial.print(" Temp = ");
  Serial.println(t);
  Serial.print(" Humidity = ");
  Serial.println(h);
  Serial.print("Current Date & Time: ");
  Serial.print(rtc.year());
  Serial.print('/');
  Serial.print(rtc.month());
  Serial.print('/');
  Serial.print(rtc.day());
  */

  if ((t > 30) && (t < 32)) {
    writeCallfile(PHONE);
    Serial.println("Writing to Card for call");
  }
  if (t > 32) {
    writeTGfile(PHONE);
    Serial.println("Writing to Card for TG");
  }
  delay(2000);
}

I become in the Serial monitor something like this:

Current Date & Time: 23/8/11, time: 19:45:0
Temperature: 27°C
Date: 1182023
File name: 1182023.csv
11 -19 -45
Path: /1182023.csv
Writing file: /1182023.csv
File written
Done

I use a ESP32 Dev module V4 from AZ delivery:
https://www.az-delivery.de/products/esp-32-dev-kit-c-v4
I hope this sketch will work as you wish, with kind regards.

Dingsken,
This code seems to work with DHT sensor. I have to see if it works with the AM2315C. Thank you so much for al the help.

1 Like

You´re welcome. I´m glad I could help. Sometimes other wiring or another CS can solve problems also. Have fun coding! With kind regards.

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