← all lessons
Microcontrollers · #12 of 48

UART Logging + Debugging

Serial Communication Workflow

Why it matters

UART (serial) is your lifeline for debugging. Print statements, logging, and interactive commands all use UART. Without it, you’re debugging blind.

The idea

What Is UART?

UART (Universal Asynchronous Receiver-Transmitter) is a simple serial protocol:

Why It Matters

UART is used for:

Common Baud Rates

Debugging Workflow

  1. Connect USB-to-serial adapter (or use ESP32’s built-in USB)
  2. Open serial monitor (115200 baud, 8N1)
  3. Add print statements at key points
  4. Watch output in real-time

Demo

UART is a communication protocol, not a visual demo. Review this before debugging your code.

Key takeaways

Going deeper

ESP32 has multiple UART peripherals (UART0, UART1, UART2). UART0 is usually used for boot messages and debugging. For production, disable debug prints or use a logging framework that can be disabled at compile time. For high-speed data logging, consider using a faster baud rate or switching to SPI.

Math details

UART frame (8N1):
  Start bit (0) + 8 data bits + Stop bit (1) = 10 bits per byte

At 115200 baud:
  Time per bit = 1 / 115200 = 8.68µs
  Time per byte = 10 × 8.68µs = 86.8µs
  Max throughput = 11520 bytes/second

Print overhead:
  "Hello World\\n" = 12 bytes = 1.04ms at 115200 baud
  Too many prints can slow down your code!

Implementation

LLM Prompt: UART Logging

Write Rust code for ESP32 UART logging using esp-hal.
Configure: UART0, 115200 baud, 8N1. Implement logging macro that
can be disabled at compile time. Include functions: log_info,
log_error, log_debug. Format: [LEVEL] message\\n

Lab Exercise

  1. Connect USB-to-serial adapter (or use ESP32’s USB port)
  2. Open serial monitor: 115200 baud, 8N1, no flow control
  3. Add print statement in main loop — verify output
  4. Add logging at different levels (info, error, debug)
  5. Test with sensor reading — log values every second

full glossary →