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:Jump to 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:
...4 Replies
`
`
`
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.Other concerns that you need to pay attention to are:
-
Remember once a variable is declared using PROGMEM it cannot be read as a regular variable you will have to read it using specific functions defined inside pgmspace.h. It stores data on flash like the string wrapper
You can also use the following functions to monitor your RAM usage: For ARM-based arduino For AVR-based arduino
Serial.print()
and Serial.println
functions use SRAM, for string literals can be used if F()
string wrapper. The wrapper will move those strings to flash memory.
- Your global variables also take up some amount of SRAM. The global contants can be pushed to flash. If you are on an avr-based
arduino board, you can use PROGMEM
. For example you can declare your global constants as:
const PROGMEM deviceServiceUuid[] = {"1523"};
Remember once a variable is declared using PROGMEM it cannot be read as a regular variable you will have to read it using specific functions defined inside pgmspace.h. It stores data on flash like the string wrapper
F()
that we saw earlier. However you need to use this judiciously since read speed to from the flash are horrible compared to RAM. So your code design needs to reflect which variables are crucial and those that aren’t. On ARM-based
arduino boards the same can be achieved using static const
static const int Variable = Data;
You can also use the following functions to monitor your RAM usage: For ARM-based arduino For AVR-based arduino