Coming from a C# background where I do logs for every application, and given that I will be using the Mega for most projects and therefore have the memory available to do so, I want to incorporate error logging in my projects.

What is the best way to do so? Writing to an SD card? Writing via Wi-Fi or Ethernet to a networked storage or PC? Writing to a USB thumb drive?

The current project I am working on uses the Adafruit Music Maker shield so has the SD card reader incorporated into it, so the easy choice there would be to use that but I like creating solutions that are portable to multiple projects so writing via Wi-Fi or Ethernet seems to be the preferred method?

Since I am not fully up to speed with C++, and I see there are various ways to write to text files in C++, are there any preferred ways to do so depending on which storage route I take?


What you ask is difficult to answer in generic way, because not all the Arduino boards have the same HW options and even if you were to require a certain interface (ex: WiFi), that will introduce other side effects:

  • it will use certain pins/buses (SPI) that one might want to reserve for other purposes (yes you can have multiple peripherals on the SPI bus, but it will require additional code)
  • it might significantly alter the execution timing in certain cases

What most people use - and seem to be happy with - is the serial port:

  • it is not too difficult to setup
  • the ide already has a serial port monitor
  • it can produce information that is immediately human-parsable
  • there is no need to retrieve it afterwards (an SD card has to be extracted, mounted, etc.)

I would advise you to consider this at least as option, if not the only option.

Of course each option has drawbacks: the serial port can also cause delays in the execution, might not be too good if you have lots of stuff to log, etc.

The serial port could be seen as a intermediate step toward wireless logging: modules like the esp8266 do rely on serial communication.

Which means you could have wired logging while in the early phase and wireless logging later on, assuming you can afford to have something constantly listening to your project.

I would consider the SD card only for cases where the device will run unattended for long periods of time and there might be some hard to reproduce event that you want to track.

You could also consider the onboard flash, if you have to log events that happen seldom.

  • I should've been a little clearer. In general I log errors to an array and infrequently write them to the log file , which is more appropriate here so as not to tie up execution with a lot of Serial... statements
    – dinotom
    Apr 21 '16 at 14:23
  • In C it's typical to use a log() macro which is configured to do whatever is needed, when logging is enabled. When logging is disabled, the macro is empty. Apr 21 '16 at 15:46

Normally, because Arduino is meant to be easy to understand and use, C++ is only used (extensively) in the libraries. Perhaps someone knows more about the Mega, but, in general, the Arduino platform does not contain many complex built in features. For instance, before even thinking about how best to write to an SDCard, you need to do some research into how to load and use the Arduino SDCard library.

This appears to be what you eventually want to do.

  • already fully versed on how to write to an sd card, or to network for that matter. the question was about best practices
    – dinotom
    Apr 21 '16 at 13:29

Embedded systems typically use UART for logging, and Arduino is no exception. Logging to UART is as easy as calling Serial.begin() in the setup() and Serial.println() in the loop(), it works on all Arduinos, doesn't require any additional hardware and consumes much less resources than the SD card or networking library. Of course, you're limited in bandwidth (115200 kbit/s, sometimes more), so you won't be able to log in the fast-running cycles.

If you don't like the Serial Monitor feature of Arduino IDE, you can install one of the many terminal programs which will happily write the data from serial port to a log file. Some of these tools also have nice analysis features, including plotting of logged signals, formatting, data conversions etc.

If you're good with command line, creating a log file is as simple as

cat /dev/ttyUSB0 > log.txt

in Linux or

type COM1 > log.txt

in Windows.


In my Arduino project, I wrote communication sub protocol between a Raspberry Pi (or a PC).

I simply added some commands to duo vars or arrays into memory.

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.