ESP32 memory usage problem with LCD display

Hi all!

I’m using an ESP32 with a SPI touchscreen using Bodmer’s TFT_eSPI library and everything is working good. When the code is compiled, it uses 20% of the ESP32’s memory.

I attempted to add some graphics to my code, as in adding tabs to the code that contain the graphic data. When the code was compiled with the graphics, it used 70% or the ESP32’s program memory.

I have removed all code that involves the graphics data, thinking the compiled size would go back to 20% of the program memory. It does not, it keeps using 70% of program memory.

The code is 300 lines long, and I’m attaching it to this post. Any ideas as to why it is using so much memory since the graphics have been removed?

Thanks
Randy

sketch_aug24a.ino (7.85 KB)

Post code directly within code tags, not as an attachment. It’s hard to read on a mobile

J-M-L:
Post code directly within code tags, not as an attachment. It’s hard to read on a mobile

I have modified the code. I removed all code, except the initial definitions and the setup() part and it now only uses 69% of storage space.

#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#include "BluetoothSerial.h"



TFT_eSPI tft = TFT_eSPI();        // Invoke custom library
BluetoothSerial SerialBT;         // bluetooth serial

// graphic defines
#define TFTwidth 320              // screen width
#define TFTheight 240             // screen height

#define SliderBarWidth 240        // temperature slider bar width
#define SliderBarHeight 20        // temperature slider bar height
#define SliderWidth 20            // temperature slider widget width
#define SliderHeight 35           // temperature slider widget height

#define BGColor TFT_DARKGREY     // background color
int sliderValue = 70;             // display value for slider widget position

//  Bluetooth variables/data
//  max length of command is 20 chrs
const byte numChars = 20;
char receivedChars[numChars];
boolean newData = false;


void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(115200);
  Serial.println(" starting... \n");

  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  
  tft.init();
  tft.setRotation(3);
  uint16_t calData[5] = { 449, 3418, 344, 3395, 1 };
  tft.setTouch(calData);
  
  tft.fillScreen(BGColor);

  //drawInitDisplay();  
   
}

void loop()
{
  
}

Still a little confused as to why it’s using so much memory…

Randy

did you make backups of the code when it was using 20% memorry?. Maybe you can find the difference with textfile comparising tools. Anyway it is a good idea to backup snapshots of your code even if it is a "inbetween" version if it is running so far

best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REal professionals write code this way.

StefanL38:
did you make backups of the code when it was using 20% memorry?.

No, I did not make backup copies.

I think I am encountering some sort of bug…

This code:

#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#include "BluetoothSerial.h"



void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Uses 16% of storage space when compiled. If I add in the code to use the libraries, like this:

#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#include "BluetoothSerial.h"

TFT_eSPI tft = TFT_eSPI();        // Invoke custom library
BluetoothSerial SerialBT;         // bluetooth serial

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

I’m now up to 28% of memory. Originally, all 300 lines of code only used 20% of memory.

I am compiling this code for a “DOIT ESP32 DEVKIT V1”

If anyone can compile the code I posted and share the results, I would be very grateful of your help.

Thanks,
Randy

Hard to say what you added from a version you don’t have anymore

If in the old code you had the #include but did not instantiate anything from those libraries, they would have been discarded by the compiler. But the minute you start using it, then associated code gets injected in the final binary.

So the question is: did you add code that you did not remove or are you 10000000% sure that you are back to the very same previous code?

Randy have you tried a new sketch and add just #include <TFT_eSPI.h> // Hardware-specific library and see how much space you have taken up? My guess is that hardware specific lib.

Note that on the ESP32 the Bluetooth support is known to take heaps of memory, 70% ish.

Try a Google search on;

'ESP32 Bluetooth Memory'

That is so true. I am working on a larger project myself and adding <BLEDevice.h> took me from about 20% to 75%. When i get time I will try to dig into this lib and pull out what I actually use.

If the TFT library uses a screen buffer, that's at least 320x240=76800 bytes gone. There has also been posts regarding memory issues with ESP32 where some ppl reported a limit of around 100k usable heap memory, making buffers of more than 100k ( eg: uint8_t no_good[120000]; ) a no-go even if the board had much more memory. Search and you shall find! :slight_smile:

Reply number 5 does a good job of explaining the why.

Try picking a different memory partition scheme.

First, a thank you to everyone who gave advice!

I admit, I must have been wrong about the compile size. I don't know how I was wrong about the compile size, as I always keep an eye on that and notice when it changes by very much. After all, when you use an UNO and you're sloppy with the code, you can run out of space quickly.

Anyway, I've done a lot of testing with code, a lot of googling on ESP32 memory usage and bluetooth, and googling memory partitioning on the ESP32. Along the way I learned -

  • Yes, bluetooth uses a lot of memory, the simplest bluetooth example uses 69% of memory.
  • I learned about memory partitioning (didn't know that was a thing, even though I've known how to partition hard drives from a command line for years.)
  • I kind of learned how define my own memory partitions. I just don't know how to implement it in the arduino IDE.

Now, instead of compiling for a 'DOIT ESP32 DEVKIT V1', (which is what is printed on my board), I'm compiling for a 'ESP32 Dev Module' that gives me several options on memory partitioning.

So thank you to everyone, you've helped me learn new things!
Randy