Make Your Logs Readable: A Guide to ANSI Colors in the Serial Terminal

As your projects grow more complex, your serial debug output can turn into a monotonous, hard-to-read wall of text. Did you miss that one critical error message scrolling by? Was that status update a success or a warning?

~7 minutes

Make Your Logs Readable: A Guide to ANSI Colors in the Serial Terminal

As your projects grow more complex, your serial debug output can turn into a monotonous, hard-to-read wall of text. Did you miss that one critical error message scrolling by? Was that status update a success or a warning?

This is where ANSI escape codes come in. They are a universal standard for adding color and formatting to text in terminal environments. A modern serial terminal like serialterminal.app supports these codes, allowing you to transform your logs from a gray stream into a vibrant, high-contrast diagnostic tool.

What is an ANSI Escape Code?

An ANSI escape code is a special sequence of characters that your terminal doesn't print. Instead, it interprets them as commands to change the text style. The most common use is to change the foreground (text) color.

The sequence always starts with an escape character (\e or \x1B in code) followed by a [ and then a series of numbers and letters.

For example, to make text red, the code is \x1B[31m. To reset the color back to default, the code is \x1B[0m.

So, to print an error message in red, you would send:

  1. The code to turn the color red.
  2. Your message text.
  3. The code to reset the color to default.

\x1B[31mThis is an error!\x1B[0m

Let's see how to implement this in popular environments.

Example: Color-coding Logs on an Arduino (C/C++)

In C/C++, you can define string constants to make your code much more readable. Add these definitions to the top of your .ino file.

#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE    "\x1b[34m"
#define ANSI_COLOR_RESET   "\x1b[0m"

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("System Initialized.");
  
  Serial.print(ANSI_COLOR_GREEN);
  Serial.print("SUCCESS: ");
  Serial.print(ANSI_COLOR_RESET);
  Serial.println("Sensor connection is OK.");

  Serial.print(ANSI_COLOR_YELLOW);
  Serial.print("WARNING: ");
  Serial.print(ANSI_COLOR_RESET);
  Serial.println("Battery level is low (15%).");

  Serial.print(ANSI_COLOR_RED);
  Serial.print("ERROR: ");
  Serial.print(ANSI_COLOR_RESET);
  Serial.println("Failed to mount SD card!");
}

void loop() {
  // Your code here
}

When you view this in serialterminal.app, the words "SUCCESS", "WARNING", and "ERROR" will pop out in their respective colors, making the log instantly scannable.

Example: Python (for Raspberry Pi, ESP32 with MicroPython)

Python's f-strings make this even easier. You can embed the escape codes directly.

import time

COLOR_RED = "\033[31m"
COLOR_GREEN = "\033[32m"
COLOR_YELLOW = "\033[33m"
COLOR_RESET = "\033[0m"

print("System Initialized.")

print(f"{COLOR_GREEN}SUCCESS:{COLOR_RESET} Network connection established.")
print(f"{COLOR_YELLOW}WARNING:{COLOR_RESET} CPU temperature is high (75 C).")
print(f"{COLOR_RED}ERROR:{COLOR_RESET} Cannot read from I2C device.")

(Note: \033 is the octal representation of the escape character, often used in Python.)

Common Color Codes Reference

Here's a handy chart for the basic colors. You can combine them (e.g., bold and red) but start with these.

Color Foreground Code
Default \x1B[0m
Black \x1B[30m
Red \x1B[31m
Green \x1B[32m
Yellow \x1B[33m
Blue \x1B[34m
Magenta \x1B[35m
Cyan \x1B[36m
White \x1B[37m

By taking a few extra moments to add color to your logs, you invest in faster, less frustrating debugging sessions for the entire life of your project.