Shrink your ESP-32 firmware size by half

Shrink your ESP-32 firmware size by half

TL;DR: Use NimBLE-Arduino library

What's the matter with ArduinoBLE?

If you've ever used Arduino IDE to develop ESP-32 applications with the ArduinoBLE library, you may have noticed that your firmware size grows very fast.

Just a simple program already uses 1 MB of flash storage, which is more than 75% off all available on ESP32 WROOM - hence adding new features becomes a lot harder since there is not an awful lot of space left after this, and this is bad.

After a quick Googling session, I found similar issues and bug reports across GitHub:

And there it was: someone posted a link to an awesome library:

What is NimBLE

NimBLE was originally developed by Apache for their MyNewt platform and was the first ever open-source Bluetooth 5 stack available publically, and it is very versatile and customizable, due to its great architecture and compatibility layers.

In addition to all the above, it also supports some experimental features, such as Bluetooth Mesh and others.

How to use NimBLE-Arduino

I'm using PlatformIO to develop my project and installing this library is as easy as adding a simple line to my configuration:

lib_deps: h2zero/NimBLE-Arduino@^1.4.0

After adding this line, you'll need to replace all your ArduinoBLE files imports with NimBLE imports:

- #include <BLEDevice.h>
- #include <BLEServer.h>
+ #include <NimBLEDevice.h>
+ #include <NimBLEServer.h>

And in most cases, that's it! NimBLE-Arduino aims to provide the same library structure, as the original ArduinoBLE, but sadly it won't in 100% cases, so I needed to customize some things:

Differences, compared to ArduinoBLE

Given the fact, that NimBLE-Arduino is very versatile, it is impossible to add all its available features and keep 100% compatibility. For example, it is no longer required to use manually add the BLE2902 descriptor, NimBLE does it by itself!

You can find the full migration guide here:

Final results

Here is my comparison of the same project, that was written using the ArduinoBLE library and using NimBLE-Arduino:

Firmware size using ArduinoBLE library: 1080349 bytes Firmware size using NimBLE library: 584501 bytes

As you can see, it saved almost 0.5MB of flash storage, which is a nearly 40% size reduction!
And here you can find the full Pull Request on GitHub: