ESP32 Arduino: Getting the Free Heap

The objective of this post is to explain how to obtain and print the ESP32 free heap, using the Arduino core. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


Introduction

The objective of this post is to explain how to obtain and print the ESP32 free heap memory, using the Arduino core.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

 

The code

The code for this will be very simple, since we already have defined in the EspClass a method for obtaining the free heap value. Note that the methods of this class are exposed in an extern variable called ESP.

So, in the Arduino setup function, we starting by opening a serial connection, so we can output the value of the free heap.

Serial.begin(115200);

Next, to obtain the free heap, we simply call the getFreeHeap method of the ESP extern variable.

This method takes no arguments and returns as output the free heap, in bytes. Note that in its implementation, the getFreeHeap method calls the esp_get_free_heap_size function of the IDF framework, which is defined in this header file.

Serial.println(ESP.getFreeHeap());

You can check the full source code below. Note that we left the main loop function empty since we don’t need to use it for this tutorial.

void setup() {
  Serial.begin(115200);
  Serial.println(ESP.getFreeHeap());
}

void loop() {}


Testing the code

To test, simply open the Arduino IDE serial monitor after compiling and uploading the code to your ESP32 device. You should get an output similar to figure 1, which shows the available heap space on the device.

ESP32 Arduino core getting free heap.png

Figure 1 – Output of the program.

Advertisements
This entry was posted in ESP32 and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s