Extreme low free heap empty project

I am creating a project where i need to play audio with a max98357a. I have used this chip in other projects as well and this works.

On this project it also works but as soon as i make a successful wifi connection, i cannot get any audio anymore.

At some point also the esp32 panics and restarts.

After some investigation i see the ESP32 has a really low free heap.
I created a complete new empty project and i show the free heap and the flash size:

Free heap size: 279148
Flash size: 16777216

When i upload a new blank sketch and just show the same data via Arduino IDE, i get this:
Free heap size: 4434571
Flash size: 16777216

With the Arduino IDE, it shows 4434571 instead of 279148 (16 times more with the same code)

My partition looks like this:

# Name,   Type, SubType, Offset,  Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x760000,
app1, app, ota_1, 0x770000, 0x760000,
eeprom, data, 0x99, 0xed0000, 0x40000,
spiffs, data, spiffs, 0xf10000, 0xf0000,

Code:

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("Free heap size: ");
  Serial.println(esp_get_free_heap_size());
  Serial.print("Flash size: ");
  Serial.println(ESP.getFlashChipSize());
}

What is going wrong?

The partition layout for the Flash have nothing to do with heap memory (RAM).

Things need to be looked at very closely in order to be able to compare them.

  • Which board do you have exactly?
  • Which Arduino core do you use in the ArduinoIDE and which one in PlatformIO.
  • What are the board settings in the ArduinoIDE and in your PlatformIO project?

For a real comparison, a screenshot of the settings in your ArduinoIDE and the content of your platformio.ini is required.


Does you ESP32 board has external PSRAM, like 4MB?
Maybe you have this enabled in ArduinoIDE but not in your platformio project.

This means that the additional 4 MB (?) will not be available and only the internal 320 kB will be available (of which a certain amount is already in use, of course).

I have a bare ESP32 wrover chip (no dev board, but custom pcb)
In ArduinoIDE, i have esp32 by Espressif Systems version 2.0.11 installed.
In PlatformIO i have this:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
lib_ldf_mode = deep+
board_build.partitions = partition_scheme_16mb.csv
board_upload.flash_size = 16MB
lib_deps = 
	earlephilhower/ESP8266Audio@^1.9.6
	fbiego/ESP32Time@^2.0.6
	arduino-libraries/Arduino_JSON@^0.2.0
	knolleary/PubSubClient@^2.8

pio --version shows: PlatformIO Core, version 6.1.15

The lib_deps where commented but that didn’t change anything.

I use “Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)” on ArduinoIDE, but just for testing

I really don’t know. I never thought about this. How can i check this?

Which one exactly?

That’s the flash layout and have no effect on the available RAM.

To enable PS-RAM you might simply need to add

build_flags = 
  -DBOARD_HAS_PSRAM

to your platformio.ini

This is the equivalent to Tools / PSRAM: "Enabled" in the ArduinoIDE

Ouch… this is a bit old.

Current available Arduino Core version is 2.0.16

This does not tell about the installed Espressif32 version.
You’ll see the used version when you build the project. Take a look at the output.
Especially for PLATFORM:
Espressif 32 (6.7.0) > Espressif ESP32 Dev Module
and
PACKAGES:
- framework-arduinoespressif32 @ 3.20016.0 (2.0.16) ← Arduino Core Version

I don’t exactly know which wrover chip i have. I have bought a few different versions over the year and i cannot see any usable information on the chip itself. I almost know for sure i bought the 16mb version but i don’t know which of the 2 versions.

I will use the build flag. In Arduino IDE i don’t have the tools / PSRAM option but i will see if the build flag makes any difference

Please show a screenshot of your ArduinoIDE Tools menu.

PLATFORM: Espressif 32 (6.7.0+sha.022e604) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
PACKAGES:

  • framework-arduinoespressif32 @ 3.20016.0 (2.0.16)
  • tool-esptoolpy @ 1.40501.0 (4.5.1)
  • tool-mkfatfs @ 2.0.1
  • tool-mklittlefs @ 1.203.210628 (2.3)
  • tool-mkspiffs @ 2.230.0 (2.30)
  • toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5

Scherm­afbeelding 2024-05-17 om 00.02.42

BTW, now with the build flag, it shows:

Flash size: 16777216
Free heap size: 4471147

So i think it is good now?
Does this also update an older esp32 firmware which is being updated via ota? Or is this something that is baked in via usb? (Maybe a difficult question, but when i compile a bin file and update that via internet (my code), will this also enable the external PSRAM?)

ESP32 Wrover Module in ArduinoIDE has -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue -mfix-esp32-psram-cache-strategy=memw

So you should use the same settings for platformio.ini:

build_flags = 
  -DBOARD_HAS_PSRAM 
  -mfix-esp32-psram-cache-issue 
  -mfix-esp32-psram-cache-strategy=memw

Yes this settings will also apply on an OTA update.

Afaik the only thing you can’t change via OTA is the partition layout.

Thanks a lot for your help! I think all of my problems are fixed with this now

1 Like