Print dump and trace with a library

Updated on 2 December 2021
dev board Arduino M0
firmware Arduino
chip SAMD21
features debug trace print dump library
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Buy the components

Code

Download code arduino-trace.ino
#define ARDUINOTRACE_SERIAL SerialUSB
#include <ArduinoTrace.h>

int someValue = 42;

void setup() {
    SerialUSB.begin(9600);
    while (!SerialUSB) { }
    delay(100);
    DUMP(someValue);
}

void loop() {
    SerialUSB.print(millis());
    SerialUSB.print(": ");
    TRACE();

    blink(5);
    delay(2000);
}

void blink(int num) {
    SerialUSB.print(millis());
    SerialUSB.print(": ");
    TRACE();

    SerialUSB.print(millis());
    SerialUSB.print(": ");
    DUMP(num);

    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);

    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
}

Makefile

BOARD?=arduino:samd:mzero_bl
PORT := $(shell ls /dev/cu.usbmodem*)
BUILD=build

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

Prototype

A photo of the actual setup.

Print dump and trace with a library prototype

Serial console

Serial output from the firmware.

Print dump and trace with a library serial console

Description

Tracing and dumping information when debugging Arduino code.

References

Watch