Print debug and trace info with a libary, header file and ifdef

Updated on 2 December 2021
dev board Arduino M0
firmware Arduino
chip SAMD21
features debug trace print dump library ifdef header
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:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code arduino-debug-trace.ino
#define DEBUG  // Comment to turn off debug statements
#define ARDUINOTRACE_SERIAL SerialUSB
#include <ArduinoTrace.h>
#include "DebugUtils.h"

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  #ifdef DEBUG
  SerialUSB.begin(9600);
  while (!SerialUSB) { }
  delay(100);
  #endif

  DEBUG_PRINT("Start blinking");
}

void loop() {
  DEBUG_PRINT("HIGH");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);

  DEBUG_PRINT("LOW");
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);

  DEBUG_TRACE();
}

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 debug and trace info with a libary, header file and ifdef prototype

Serial console

Serial output from the firmware.

Print debug and trace info with a libary, header file and ifdef serial console

Description

Combing #ifdef with a header file and ArduinoTrace library to setup toggling on and off for debugging Arduino code.

References

Watch