← all lessons
Microcontrollers · #12 of 20

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:
        <h3>Why It Matters</h3>
        UART is used for:
        <ul>
          <li><strong>Debugging</strong> — print statements, logs</li>
          <li><strong>Configuration</strong> — interactive commands</li>
          <li><strong>Data logging</strong> — stream sensor data</li>
          <li><strong>Boot messages</strong> — ESP32 prints boot info</li>
        </ul>

        <h3>Common Baud Rates</h3>
        <ul>
          <li><strong>9600</strong>: Slow but reliable</li>
          <li><strong>115200</strong>: Standard for ESP32 (fast enough, reliable)</li>
          <li><strong>921600</strong>: Maximum (may have errors on long cables)</li>
        </ul>

        <h3>Debugging Workflow</h3>
        <ol>
          <li>Connect USB-to-serial adapter (or use ESP32's built-in USB)</li>
          <li>Open serial monitor (115200 baud, 8N1)</li>
          <li>Add print statements at key points</li>
          <li>Watch output in real-time</li>
        </ol>

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:

Implementation

LLM Prompt: UART Logging