I've got an Arduino Due connected to a Raspberry Pi via Serial3. The Due will randomly stop sending data to the Pi after a few days, or sometimes after several hours. Using an oscilloscope, I can see that the Pi keeps transmitting to the Due, but there is no response from the Due. I'm also getting the Due to echo everything it gets from the Pi to Serial, which is connected to my PC. I can see that the Due will stop echoing characters mid way through receiving a message from the Pi.

I then set one of the pins as an output and just keep toggling that regularly. When the Due is waiting for data from the Pi, this signal has a frequency of about 100Hz, i.e. a period of 10ms. I have a counter that goes from 0 to 1000 to control this frequency somewhat. When this error happens and communications stops, I can see that the pulses now have a period of 2.14 seconds, i.e. 214 times slower.

I'm using Arduino-MemoryFree to check for memory leaks but I haven't found any problems yet and I don't seem to be running out of memory. In case it matters, I'm also using the ArduinoJson library. Arduino IDE version is 1.6.7, I've updated the Arduino SAM Boards package to version 1.6.9 (problem was present before the update as well).

Could someone please advise me on what could be causing this slow down?

  • 2
    Memory leaks don't just show up as memory shrinking - they also show up as variables being corrupted from such things as buffer overruns. You need to post your code so we can see where it's likely to be failing. – Majenko Sep 28 '16 at 12:59
  • 1
    Its most likely a memory leak, but another option could be memory fragmentation, due to overuse of the new and delete command. – Code Gorilla Sep 28 '16 at 13:02
  • Maybe PMC registers gets corrupted and Main Clock was switched to Slow Clock. You can try to lock PMC settings so it can't be changed accidentaly. – KIIV Sep 28 '16 at 17:49
  • @KIIV How do I lock those settings? Once locked, what happens when code attempts to overwrite them? Will the Arduino Due reset, or crash thoroughly (in which case the watchdog timer can still recover so I'm happy with that too)? – chippies Sep 30 '16 at 14:00
  • @Matt I suspected that too, except I'm not using new/delete and the ArduinoJson library uses a fixed size buffer on the stack. Could the built-in String class be causing problems (it sees a lot of use)? I read there was a problem with it, but the version number mentioned was v1.0.5 of the IDE. – chippies Sep 30 '16 at 14:03

The Due will randomly stop sending data to the Pi after a few days, or sometimes after several hours.

Here is the typical manifestation of heap fragmentation in an Arduino sketch.


  1. Remove as many Strings as possible and replace them with char[]
  2. Use a StaticJsonBuffer instead of a DynamicJsonBuffer

If you remove all the Strings and use a StaticJsonBuffer, your program will run without any heap. No heap, no fragmentation! Not only it will be more reliable, it will also be smaller and faster.

If you must use String, then call String::reserve() to make sure the size is the same at each iteration. Indeed, repeated allocation of the same size doesn't increase fragmentation.

  • In the end, I changed the system architecture to avoid JSON processing on the Due. That fixed the issue, so I'm marking this as the solution. – chippies Nov 13 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.