ESP32 rebooting periodically

Hi All,
I'm working on a project with ESP32S3 PICO where I'm integrated 2-16bit ADC (ADS1115), Dwin Display, SD card, Pendrive.

Recent Code:


It is a split file voids were splitted. but the main program is here.

//------------------------------------------------------PenDrive-I2C-Communication----------------------------------------------------------------------

#include <Ch376msc.h>
#include <SoftwareSerial.h>
SoftwareSerial Pendrive(17,18); // RX, TX
Ch376msc flashDrive(Pendrive); 
char adatBuffer[255];
char adat[]="Vivamus nec nisl molestie, blandit diam vel, varius mi. Fusce luctus cursus sapien in vulputate.\n";
char adat2[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis efficitur ac est eu pharetra. \n";

unsigned long totSect = 0;
unsigned long freeSect = 0;
byte percentg = 0;
byte tmpCommand; //used to store data coming from serial port
boolean readMore;
static char helpString[]= {"h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n"
            "5:Modify date/time\n6:Delete\n7:List dir\n8:Print free space"
            "\n9:Open/Create folder(s)/subfolder(s)"};
void printInfo();

//------------------------------------------------------SD-SPI-Communication----------------------------------------------------------------------

#include "FS.h"
#include "SD.h"
#include "SPI.h"

/* Hardware SP
MOSI: 11
MISO: 13
SCK: 12
SS: 10
*/

float count = 0.067;
#define B1 14
#define B2 15
#define LEDx 16

//------------------------------------------------------SD-SPI-Communication-Voids---------------------------------------------------------------

void listDir();
void createDir();
void removeDir();
void readFile();
void writeFile();
void appendFile();
void renameFile();
void deleteFile();
void testFileIO();
void SD_Button();

//----------------------------------------------------I2C-ADC-ADS1115-Communication-------------------------------------------------------------------------

#include <Wire.h>
#define SDA0_Pin 5
#define SCL0_Pin 6

#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ADS1;  
Adafruit_ADS1115 ADS2;

//----------------------------------------------------I2C-RTC-DS1307------------------------------------------------------------------------

#include "RTCDS1307.h"
#include <Wire.h>
#define SDA0_Pin 5
#define SCL0_Pin 6
RTCDS1307 rtc(0x68);

uint8_t year, month, weekday, day, hour, minute, second;
bool period = 0;

String m[12] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
String w[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat"};
void Clock();

//------------------------------------------------UART-Dwin-Display-Serial-communication----------------------------------------------------------------

#define txPin 8
#define rxPin 7
HardwareSerial &dwinSerial = Serial2;

void FloatToHex();
void sendFloatNumber1();
void sendFloatNumber2();

//--------------------------------------------------------Dual_Core_Initialisation-------------------------------------------------------

int 
Core0_ADC_1, Core1_ADC_1, Mid_ADC_1,
Core0_ADC_2, Core1_ADC_2, Mid_ADC_2,
Core0_ADC_3, Core1_ADC_3, Mid_ADC_3,
Core0_ADC_4, Core1_ADC_4, Mid_ADC_4,
Core0_ADC_5, Core1_ADC_5, Mid_ADC_5,
Core0_ADC_6, Core1_ADC_6, Mid_ADC_6;																																					

//-------------------------------------------------Dual_Core_Locking----------------------------------------------------------------------
			   							 

//-------------------------------------------------------Overall_Initialisation------------------------------------------------------------------------------

//unsigned char Buffer[9];

byte ADC1_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x00, 0x01, 0x01 };
byte ADC1_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x00, 0x00, 0x01 };
byte ADC1_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x00, 0x00, 0x02 };

byte ADC2_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x10, 0x01, 0x01 };
byte ADC2_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x10, 0x00, 0x01 };
byte ADC2_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x10, 0x00, 0x02 };

byte ADC3_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x20, 0x01, 0x01 };
byte ADC3_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x20, 0x00, 0x01 };
byte ADC3_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x20, 0x00, 0x02 };

byte ADC4_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x30, 0x01, 0x01 };
byte ADC4_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x30, 0x00, 0x01 };
byte ADC4_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x30, 0x00, 0x02 };

byte ADC5_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x40, 0x01, 0x01 };
byte ADC5_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x40, 0x00, 0x01 };
byte ADC5_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x40, 0x00, 0x02 };

byte ADC6_Off[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x50, 0x01, 0x01 };
byte ADC6_Green[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x50, 0x00, 0x01 };
byte ADC6_Red[] = { 0x5A, 0xA5, 0x05, 0x82, 0x53, 0x50, 0x00, 0x02 };

byte Tolerance_Write[] = { 0x5A, 0xA5, 0x08, 0x82, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

unsigned char incomingData[100];
unsigned char textString[100];
char receivedFloatArray[50];
float receivedFloat;
int check = 1;
float Setval_ADC1=0, Setval_ADC2=0, Setval_ADC3=0, Setval_ADC4=0, Setval_ADC5=0, Setval_ADC6=0;
int Convert1, Convert2, Convert3, Convert4, Convert5, Convert6;
float Final_ADC1, Final_ADC2, Final_ADC3, Final_ADC4, Final_ADC5, Final_ADC6;
long ADCTimer;
bool isit = true;
float tolerance = 0;
String Collect="";


//-------------------------------------------------------------Setup-----------------------------------------------------------------------------


void setup() {

  Serial.begin(115200);
  Wire.begin(SDA0_Pin, SCL0_Pin);

//------------------------Clock-RTC--------------------------

    rtc.begin();
 // rtc.setMode(0);
 // rtc.setDate(24, 3, 22);
 // rtc.setTime(11, 32, 20);

//------------------------Pendrive--------------------------

  Pendrive.begin(9600);flashDrive.init();
  printInfo(helpString);


//------------------------Display---------------------------

  dwinSerial.begin(115200, SERIAL_8N1,rxPin, txPin);
  dwinSerial.setTimeout(10);
  while (!Serial) { delay(1); }


//------------------------ADC---------------------------


  Core0_ADC_1=0; Core0_ADC_2=0; Core0_ADC_3=0; 
  Core0_ADC_4=0; Core0_ADC_5=0; Core0_ADC_6=0;


//------------------------Core_Build-------------------------

  disableCore0WDT();
  disableCore1WDT();

//-----------------------Setup_SD_Button-------------------------

{  
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(LEDx, OUTPUT);
  
  while(!Serial) { delay (10); }

#ifdef REASSIGN_PINS
    SPI.begin(sck, miso, mosi, cs);
#endif
    if(!SD.begin()){
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD.cardType();

    if(cardType == CARD_NONE){
        Serial.println("No SD card attached");
        return;
    }

    Serial.print("SD Card Type: ");
    if(cardType == CARD_MMC){
        Serial.println("MMC");
    } else if(cardType == CARD_SD){
        Serial.println("SDSC");
    } else if(cardType == CARD_SDHC){
        Serial.println("SDHC");
    } else {
        Serial.println("UNKNOWN");
    }

    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    Serial.printf("SD Card Size: %lluMB\n", cardSize);
/*
    listDir(SD, "/", 0);
    createDir(SD, "/mydir");
    listDir(SD, "/", 0);
    removeDir(SD, "/mydir");
    listDir(SD, "/", 2);
    writeFile(SD, "/hello.txt", "Hello ");
    appendFile(SD, "/hello.txt", "World!\n");
    readFile(SD, "/hello.txt");
    deleteFile(SD, "/foo.txt");
    renameFile(SD, "/hello.txt", "/foo.txt");
    readFile(SD, "/foo.txt");
    testFileIO(SD, "/test.txt");
    Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
    Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
    */  

char cstr[16];
itoa(count, cstr, 10);

float FltValue = 1.34567890;
char ChValue[10];
  dtostrf(FltValue, 10, 0, ChValue);
//dtostrf(float, minimum width, precision, character array);


    writeFile(SD, "/hello.txt", cstr);
    appendFile(SD, "/hello.txt", "\n");
    writeFile(SD, "/hello.txt", ChValue);
    readFile(SD, "/hello.txt");
}


{   
  disableCore0WDT();
  disableCore1WDT();

//------------------------ADC_16Bit-------------------------

 // ADS.setGain(GAIN_ONE);  // ADS.setGain(); ADS.setGain(GAIN_SIXTEEN);

  if (!ADS1.begin(0x48)) {
    Serial.println("1Failed to initialize ADS.");
    while (1);
  }  
  if (!ADS2.begin(0x49)) {
    Serial.println("2Failed to initialize ADS.");
    while (1);
  }
  
  delay(500);

  Core1_ADC_1=0; Core1_ADC_2=0; Core1_ADC_3=0; Core1_ADC_4=0; Core1_ADC_5=0; Core1_ADC_6=0;// Core1_ADC_7=0; Core1_ADC_8=0;
}

}


void loop() {
  fetchText();
  readADC();
  delay(20);
 // SD_Button();

}

//-------------------------------------------------------------readADC-----------------------------------------------------------------------------

void readADC() {

 ADS1.begin(0x48);
  Core0_ADC_1 = ADS1.readADC_SingleEnded(0);
  Core0_ADC_2 = ADS1.readADC_SingleEnded(1);
  Core0_ADC_3 = ADS1.readADC_SingleEnded(2);
  Core0_ADC_4 = ADS1.readADC_SingleEnded(3);

 ADS2.begin(0x49);
 Core0_ADC_5 = ADS2.readADC_SingleEnded(0);
 Core0_ADC_6 = ADS2.readADC_SingleEnded(1);
 
  if (check == 0 && isit == true) {
    if (millis() - ADCTimer > 50) {
      Convert1 = Core0_ADC_1; Convert2 = Core0_ADC_2; Convert3 = Core0_ADC_3;
      Convert4 = Core0_ADC_4; Convert5 = Core0_ADC_5; Convert6 = Core0_ADC_6;

      Serial.print(" Convert1 Reading->  ");Serial.print(Convert1);
      Serial.print(" set valout before ->  ");Serial.println(Setval_ADC1);
      Serial.println("-------------------------------------------------------"); 
      Serial.print(" convert1 before ->  ");Serial.println(Convert1);

      Final_ADC1 = map(Convert1, 20, 17654, 1105, 0);
      Final_ADC1 = (Final_ADC1 / 100);
      Setval_ADC1 = Final_ADC1;

      Final_ADC2 = map(Convert2, 20, 17654, 1105, 0);
      Final_ADC2 = (Final_ADC2 / 100);
      Setval_ADC2 = Final_ADC2;

      Final_ADC3 = map(Convert3, 20, 17654, 1105, 0);
      Final_ADC3 = (Final_ADC3 / 100);
      Setval_ADC3 = Final_ADC3;

      Final_ADC4 = map(Convert4, 20, 17654, 1105, 0);
      Final_ADC4 = (Final_ADC4 / 100);
      Setval_ADC4 = Final_ADC4;

      Final_ADC5 = map(Convert5, 20, 17654, 1105, 0);
      Final_ADC5 = (Final_ADC5 / 100);
      Setval_ADC5 = Final_ADC5;

      Final_ADC6 = map(Convert6, 20, 17654, 1105, 0);
      Final_ADC6 = (Final_ADC6 / 100);
      Setval_ADC6 = Final_ADC6;
   
      ADCTimer = millis();
      check = 1;
    }
  }

  if (millis() - ADCTimer > 50) {

      Convert1 = Core0_ADC_1; Convert2 = Core0_ADC_2; Convert3 = Core0_ADC_3;
      Convert4 = Core0_ADC_4; Convert5 = Core0_ADC_5; Convert6 = Core0_ADC_6;

      Final_ADC1 = map(Convert1, 20, 17654, 1105, 0);
      Final_ADC1 = (Final_ADC1 / 100) - Setval_ADC1;
      Final_ADC2 = map(Convert2, 20, 17654, 1105, 0);
      Final_ADC2 = (Final_ADC2 / 100) - Setval_ADC2;
      Final_ADC3 = map(Convert3, 20, 17654, 1105, 0);
      Final_ADC3 = (Final_ADC3 / 100) - Setval_ADC3;
      Final_ADC4 = map(Convert4, 20, 17654, 1105, 0);
      Final_ADC4 = (Final_ADC4 / 100) - Setval_ADC4;
      Final_ADC5 = map(Convert5, 20, 17654, 1105, 0);
      Final_ADC5 = (Final_ADC5 / 100) - Setval_ADC5;
      Final_ADC6 = map(Convert6, 20, 17654, 1105, 0);
      Final_ADC6 = (Final_ADC6 / 100) - Setval_ADC6;

  //  Serial.println(tolerance);

    if (tolerance > 0 && tolerance < 10 ) {

//-----------------------------Display_Signal_ADC1-----------------------------

      if (tolerance / 2 > abs(Final_ADC1) && tolerance / 2 != abs(Final_ADC1)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC1_Green, sizeof(ADC1_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC1)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC1_Red, sizeof(ADC1_Red));
      }

//-----------------------------Display_Signal_ADC2-----------------------------

      
      if (tolerance / 2 > abs(Final_ADC2) && tolerance / 2 != abs(Final_ADC2)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC2_Green, sizeof(ADC2_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC2)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC2_Red, sizeof(ADC2_Red));
      }

//-----------------------------Display_Signal_ADC3-----------------------------

      if (tolerance / 2 > abs(Final_ADC3) && tolerance / 2 != abs(Final_ADC3)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC3_Green, sizeof(ADC3_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC1)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC3_Red, sizeof(ADC3_Red));
      }

//-----------------------------Display_Signal_ADC4-----------------------------

      if (tolerance / 2 > abs(Final_ADC4) && tolerance / 2 != abs(Final_ADC4)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC4_Green, sizeof(ADC4_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC4)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC4_Red, sizeof(ADC4_Red));
      }

//-----------------------------Display_Signal_ADC5-----------------------------

      if (tolerance / 2 > abs(Final_ADC5) && tolerance / 2 != abs(Final_ADC5)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC5_Green, sizeof(ADC5_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC5)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC5_Red, sizeof(ADC5_Red));
      }

//-----------------------------Display_Signal_ADC6-----------------------------

      if (tolerance / 2 > abs(Final_ADC6) && tolerance / 2 != abs(Final_ADC6)) {
        Serial.println("ookkk");
        dwinSerial.write(ADC6_Green, sizeof(ADC6_Green));
      }
      if (tolerance / 2 <= abs(Final_ADC6)) {
        Serial.println("Nooooooooooooooooooo");
        dwinSerial.write(ADC6_Red, sizeof(ADC6_Red));
      }

    }
    sendFloatTolerance(tolerance);
    sendFloatADC1(Final_ADC1); 
    sendFloatADC2(Final_ADC2); 
    sendFloatADC3(Final_ADC3); 
    sendFloatADC4(Final_ADC4); 
    sendFloatADC5(Final_ADC5); 
    sendFloatADC6(Final_ADC6); 
    ADCTimer = millis();
  }
 //delay(500);
}
//-------------------------------------------------------------fetchText-----------------------------------------------------------------------------


void fetchText() {
  if (dwinSerial.available() > 0) {
  Collect = dwinSerial.readString();
  receivedFloat = atof(Collect.c_str());
         dwinSerial.write(Tolerance_Write, sizeof(Tolerance_Write));
      if (receivedFloat == 0) { check = receivedFloat;  }
      if (receivedFloat > 0) { tolerance = receivedFloat; }
      Serial.print("Received float");
      Serial.println(receivedFloat, 2);
  }
  else{}
}

This program is working perfectly as I planned for 5 minutes and reboots automatically when i use the below board setting.

Then, i changed the board setting as below, Now the program runs perfectly for ardoung 50 mins and reboots automatically.

The reboot message is,


abort() was called at PC 0x4201f91b on core 0


Backtrace: 0x40377fb2:0x3fcebd00 0x4037b435:0x3fcebd20 0x40380c91:0x3fcebd40 0x4201f91b:0x3fcebdc0 0x4201f962:0x3fcebde0 0x4201f56d:0x3fcebe00 0x4201f3a4:0x3fcebe20 0x42006225:0x3fcebe40 0x420029a3:0x3fcebe60 0x42002f42:0x3fcebe90 0x420096c5:0x3fcebeb0


ELF file SHA256: d8317379328ebdb8

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x2b (SPI_FAST_FLASH_BOOT)
Saved PC:0x40377bc0
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0




When I tried to extract the message using Exception Decoder, I got the message below.


PC: 0x40377bc0: esp_restart_noos at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/port/soc/esp32s3\system_internal.c:151

Decoding stack results
0x40377fb2: panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system\panic.c:408
0x4037b435: esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system\esp_system.c:137
0x40380c91: abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib\abort.c:46
0x4201f91b: __cxxabiv1::__terminate(void at *)()) (/builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32s3-elf/src/gcc/libstdc++-v3/libsupc++\eh_terminate.cc:47
0x4201f962: std::terminate() at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32s3-elf/src/gcc/libstdc++-v3/libsupc++\eh_terminate.cc:57
0x4201f56d: __cxxabiv1::__cxa_allocate_exception(std::size_t) at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32s3-elf/src/gcc/libstdc++-v3/libsupc++\eh_alloc.cc:300
0x4201f3a4:  is in operator new(unsigned int) (/builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32s3-elf/src/gcc/libstdc++-v3/libsupc++/new_op.cc:54).
0x42006225:  is in Adafruit_ADS1X15::begin(unsigned char, TwoWire*) (c:\Users\murug\OneDrive\Documents\Arduino\libraries\Adafruit_ADS1X15\Adafruit_ADS1X15.cpp:66).
0x420029a3: readADC() at H:\Personal\Industry_Automation\dualx\upgradingxxx_Single_Core\upgradingxxx_Single_Core.ino:296
0x42002f42: loop() at H:\Personal\Industry_Automation\dualx\upgradingxxx_Single_Core\upgradingxxx_Single_Core.ino:280
0x420096c5: loopTask(void*) at C:\Users\murug\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.14\cores\esp32\main.cpp:50

I dont get it.
Can someone please explain what error it is?
How is it related to board configuration?
How to resolve it?

Looks like failure of memory allocation. Look for memory leaks while the program is running (i.e. a steady decrease in free memory). The problem could be anywhere in your code or some library.

Use of String objects can cause that, too, due to poor to nonexistent garbage collection. With MCUs, I avoid Strings like the plague.

Free memory checking: How to check free memory in ESP32 – iotespresso.com

why are you using Software serial on an ESP32S3 which has hardware serial ports?

Pal, You are amazing! Right after your message, I visited the link you shared and added the below lines in the loop and ran it till the program reboots. (about an Hr+ for confirmation)


static const char *TAG = "Main File";
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
Serial.println("[APP] Free memory: " + String(esp_get_free_heap_size()) + " bytes");

I'm surprised, the Free memory was initially 2.4MB and dropped down to few bytes and thats the exact point where the reboot gets triggered.


[APP] Free memory: 2448699 bytes
[APP] Free memory: 2448667 bytes
[APP] Free memory: 2448635 bytes
[APP] Free memory: 2448603 bytes
[APP] Free memory: 2448571 bytes
[APP] Free memory: 2448539 bytes
[APP] Free memory: 2448507 bytes
.
.
.
[APP] Free memory: 295 bytes
[APP] Free memory: 263 bytes
[APP] Free memory: 231 bytes
[APP] Free memory: 199 bytes
[APP] Free memory: 167 bytes
183 bytes
144 bytes

abort() was called at PC 0x420330cf on core 0

Ok we found the root cause of the problem, so can you please suggest any way to clear those cache or what steps can I proceed with?
Usually the program don't load up with memories? is this a strange behaviour?

Yup! that's a mistake I was planned to use the current example code integratively and modify it while tuning. Inbetween i jumped to this reboot issue.

Looks like 32 bytes is being repeatedly consumed. That is probably an I/O buffer being allocated, but never deallocated. Look for operations that open files or do I/O without closing.

The following makes no sense to me and may be a problem. Close files before reusing. The Arduino SD library is not particularly reliable, so keep operations very simple and straightforward.

    writeFile(SD, "/hello.txt", cstr);
    appendFile(SD, "/hello.txt", "\n");
    writeFile(SD, "/hello.txt", ChValue);
    readFile(SD, "/hello.txt");

Alright, commented all lines now. still same result.

If you want forum members to help, post all the code. The problem could be anywhere.

I hope full code is not required if you can go though my below analysis.

I found the exact part from where the memory getting dumped.
Here is what I did, I used the same memory print line in different rows in the loop to find where exactly the memory is getting accumulated


[APP] Free memory: zzzzzzzzzzzzzzz 1650403 bytes
[APP] Free memory: vvvvvvvvvvvvvvvvvvvvvvvvvvv 1650403 bytes
[APP] Free memory: kkkkkkkkkkkkkkkkkkkkkkkkkklllllllllllllllll 1650387 bytes
[APP] Free memory: hhhhhhhhhhhhhhhhhhhhhhhhh 1650403 bytes
[APP] Free memory: ppppppppppppppp 1650371 bytes
[APP] Free memory: uuuuuuuuuuuuu 1650371 bytes
[APP] Free memory: ttttttttttttttttttt 1650371 bytes
[APP] Free memory: xxxxxxxxxxxxxxxx 1650371 bytes
[APP] Free memory: zzzzzzzzzzzzzzz 1650371 bytes
[APP] Free memory: vvvvvvvvvvvvvvvvvvvvvvvvvvv 1650371 bytes
[APP] Free memory: kkkkkkkkkkkkkkkkkkkkkkkkkklllllllllllllllll 1650355 bytes
[APP] Free memory: hhhhhhhhhhhhhhhhhhhhhhhhh 1650371 bytes
[APP] Free memory: ppppppppppppppp 1650339 bytes
[APP] Free memory: uuuuuuuuuuuuu 1650339 bytes

the code zone is,

  ADS1.begin(0x48);
  Core0_ADC_1 = ADS1.readADC_SingleEnded(0);
  Core0_ADC_2 = ADS1.readADC_SingleEnded(1);
  Core0_ADC_3 = ADS1.readADC_SingleEnded(2);
  Core0_ADC_4 = ADS1.readADC_SingleEnded(3);

 ADS2.begin(0x49);
 Core0_ADC_5 = ADS2.readADC_SingleEnded(0);
 Core0_ADC_6 = ADS2.readADC_SingleEnded(1);

In order to confirm, I used the ESP32 random GPIO's for analog read instead of external ADC modules as below,

Core0_ADC_1 = analogRead(18);
Core0_ADC_2 = analogRead(19);
Core0_ADC_3 = analogRead(20);
Core0_ADC_4 = analogRead(21);
Core0_ADC_5 = analogRead(16);
Core0_ADC_6 = analogRead(17);

As expected, memory is stable at 2.4MB and no reboot hapenning for the above code.
But I require 16 bit ADC inorder to fullfill my requirement which the 12 bit internal ADC's can't matchup.

Even i just tried to enable one ADC with 1 analog pinout as below, as usual results in memory dumping.

  ADS1.begin(0x48);
 Core0_ADC_1 = ADS1.readADC_SingleEnded(0);
 /*
  Core0_ADC_2 = ADS1.readADC_SingleEnded(1);
  Core0_ADC_3 = ADS1.readADC_SingleEnded(2);
  Core0_ADC_4 = ADS1.readADC_SingleEnded(3);

  ADS2.begin(0x49);
  Core0_ADC_5 = ADS2.readADC_SingleEnded(0);
  Core0_ADC_6 = ADS2.readADC_SingleEnded(1);
  */

Now, we have clarity on where the issue is.
Can you please help me to resolve it?

The obvious place to look is in the code for the ADS1 object.

Good sleuthing!

1 Like

Pal! We did it. Problem solved.

In order to confirm whether that ADC library is consuming space, I ran an example program of that library and appended this memory line with it.

I wondered, Memory is stable @2.4MB
So the only line I'm missusing is, initialising the wire inside loop.

//  ADS1.begin(0x48);-----------------Is not required
  Core0_ADC_1 = ADS1.readADC_SingleEnded(0);
  Core0_ADC_2 = ADS1.readADC_SingleEnded(1);
  Core0_ADC_3 = ADS1.readADC_SingleEnded(2);
  Core0_ADC_4 = ADS1.readADC_SingleEnded(3);

// ADS2.begin(0x49);-----------------Is not required
  Core0_ADC_5 = ADS2.readADC_SingleEnded(0);
  Core0_ADC_6 = ADS2.readADC_SingleEnded(1);

Realised that ADS1.begin(0x48); and ADS2.begin(0x49); are not required as I already addressed them in the setup as,

// Under void setup() 

    if (!ADS1.begin(0x48)) {
      Serial.println("1Failed to initialize ADS.");
      while (1)
        ;
    }
    
    if (!ADS2.begin(0x49)) {
      Serial.println("2Failed to initialize ADS.");
      while (1)
        ;
    }

Now those 2 lines were commented in "void loop" and system memory is stable.
Expecting now the esp will not reboot.

Good lesson: initialising some wire/I2C inside the loop is a bad move.

Thanks for the support Pal. Appreciate your guidance :grin:

That would be the 32 byte buffer suggested in post #6.

Good work!

1 Like

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