MLX90640 Sensor Fails After 3-5 Minutes When Using ArduinoBLE Library

Hello everyone,

I am currently working on a project using an Arduino equipped with an MLX90640 sensor to transmit temperature images via Bluetooth Low Energy (BLE). When I use only the MLX90640 library from Adafruit, the program functions correctly and displays the temperature image as expected. However, when I include the ArduinoBLE library, the sensor operates for just 3 to 5 minutes before it fails.

Additionally, I've noticed that the dynamic memory usage spikes to 70% when both libraries are included.
Here is the code I'm working with

Best regards,
Solution
Hey @aymen ammari first signs are pointing towards memory fragmentation degrading system performance. When memory is not managed well, your Arduino might fail in a variety of ways. Sometimes, this can be as obvious as the code not uploading to your board like shown above. In other cases, everything might appear to run fine for awhile, only for the microcontroller to stop responding later on – that’s much more tricky!
Looking at your code you have many opportunities for optimizing dynamic memory usage. Mostly by moving things on flash, you will take some hit on performance.

You can rewrite the sendLargeDataOverBLE function to cap memory usage. Here are some improvements you can make:

  • First avoid declaration of arrays in a loop whenever you can.
  • You can pass pass chunks of your frame_ble without copying into declaring an array and copying into it. Since you already have the data in memory, you can using some pointer additions to pass the right offset into the array and its size.
Was this page helpful?