Arduino:
Logging
How to:
Arduino doesn’t come with a built-in logging library like some other environments, but you can implement basic logging to the Serial console with minimal fuss. Here’s a quick example to get you started:
void setup() {
// Start the serial communication with the given baud rate
Serial.begin(9600);
// Wait for the serial port to connect - only necessary on some boards
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
// Log an informational message indicating the setup process is complete
Serial.println("Setup complete!");
}
void loop() {
// Simple logger that prints the uptime every second
static unsigned long lastLogTime = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastLogTime >= 1000) {
lastLogTime = currentMillis;
Serial.print("Uptime (ms): ");
Serial.println(currentMillis);
// Here you could also add error logs, warnings, or other info.
}
// Rest of your program logic here...
}
Sample Serial Output:
Setup complete!
Uptime (ms): 1000
Uptime (ms): 2000
Uptime (ms): 3000
...
Deep Dive:
Historically, logging on microcontrollers wasn’t as straightforward as on a full-blown operating system. Limited resources meant that every byte counted, and developers needed to be careful not to clog the system. With the advent of more capable boards and the Arduino platform simplifying the process, logging has become more accessible.
While the code above demonstrates logging via the Serial interface, other methods include writing to an SD card, sending data over network to a remote server, or even outputting to a small LCD.
Implementing a logging system brings about considerations such as rotation, level severity (info, debug, warning, error), and performance impact. On an Arduino, you may need to be mindful of memory constraints when logging complex data structures. For remote logging, security of the transmitted logs is also a concern.
More sophisticated solutions like Syslog, a widely-adopted logging standard, exist outside the Arduino world, but you can integrate third-party libraries that offer similar functionality with various degrees of complexity and resource requirements.