Skip to content
  1. LuzineDebug
  2. Guides
  3. Debug an Arduino with the Serial Monitor
Guide Beginner ≈ 15 min

Debug an Arduino with the Serial Monitor

In short

Call Serial.begin(115200) in setup(), open the Serial Monitor and pick exactly the same baud rate: garbled characters almost always mean a different speed. Print short labelled messages, wrap them in a macro you can switch off and keep an eye on free RAM.

Be the first to approve this guide

Why this method

On a microcontroller there is no browser console and no debugger attached by default: the serial port is your main observation tool. Used well, it shows where the program goes, sensor values, the exact moment of a reset and even the cause of a crash on an ESP32. This guide covers the settings that avoid blank or garbled output, techniques to trace without slowing the program down, and when to reach for the multimeter because the problem is hardware.

What you need

  • Arduino IDE 2.x, arduino-cli or PlatformIO
  • An Arduino or ESP32 board and a USB data cable (not charge-only)
  • Optional: a multimeter

Step by step

  1. Start the serial port at the right speed

    Call Serial.begin() once in setup(), then open the monitor with Tools > Serial Monitor (Ctrl+Shift+M or Cmd+Shift+M). In the monitor drop-down, pick exactly the same speed as in the code. 115200 baud is a good default on every recent board; 9600 is still common in old examples but slows output down.

    cpp
    void setup() {
      Serial.begin(115200);
      // Native USB boards: wait for the monitor for 3 s at most
      while (!Serial && millis() < 3000) { }
      Serial.println(F("Boot OK"));
    }
    
    void loop() {
    }
  2. Understand while(!Serial)

    On native USB boards (Leonardo, Micro, Zero, Nano 33, RP2040, ESP32-S3 in USB CDC mode), the serial port only exists once the PC opens it: without waiting, the first setup() messages are lost. while (!Serial) waits for the monitor to open. Always add a timeout as above: otherwise, on battery and without a PC, the board stays stuck on that line forever. On an Uno, Serial is always ready and the loop is useless.

  3. Fix garbled characters

    Symbols such as ⸮⸮⸮ or ÿ almost always mean the monitor speed differs from the code. At boot, an ESP32 prints its ROM messages at 115200 baud and an ESP8266 at 74880: a few odd characters on reset are then normal. Other causes: a module wired to pins D0 and D1 of an Uno (they share the USB link), a cable that is too long or no common ground between two boards.

  4. Write useful messages

    A good debug message says where you are and what the data is worth: a label, the value and possibly millis() to timestamp the event. The F() macro keeps constant strings in flash memory instead of RAM, which is precious on an Uno (2 KB). Print registers and bytes in hexadecimal with Serial.print(x, HEX). On ESP32, Serial.printf() accepts a C-style format.

    cpp
    Serial.print(millis());
    Serial.print(F(" ms | temp="));
    Serial.print(temp, 1);
    Serial.print(F(" | status=0x"));
    Serial.println(status, HEX);
    
    // ESP32
    Serial.printf("%lu ms | temp=%.1f | heap=%u\n", millis(), temp, ESP.getFreeHeap());
  5. Plot values with the Serial Plotter

    For a noisy sensor or an oscillating control loop, a curve beats hundreds of lines. Close the monitor and open Tools > Serial Plotter. Send one line per measurement, with label:value pairs separated by commas: each label becomes a curve. Print nothing else on the port meanwhile, otherwise the plotter treats your messages as data.

    cpp
    Serial.print(F("consigne:"));
    Serial.print(target);
    Serial.print(F(",mesure:"));
    Serial.println(measured);
  6. Create switchable debug macros

    Serial.print calls slow the program down (at 9600 baud, one character takes about 1 ms) and use memory. Wrap them in macros: with DEBUG set to 0, the preprocessor removes them entirely from the compiled program, at no cost. You keep your traces in the code without shipping them in the final build.

    cpp
    #define DEBUG 1
    
    #if DEBUG
      #define DBG(x)   Serial.print(x)
      #define DBGLN(x) Serial.println(x)
    #else
      #define DBG(x)
      #define DBGLN(x)
    #endif
    
    void loop() {
      DBG(F("capteur="));
      DBGLN(analogRead(A0));
    }
  7. Measure free RAM

    Random resets, variables changing on their own or a program freezing after a few minutes often betray a lack of RAM, especially with String objects handled in loop(). On AVR (Uno, Nano, Mega), the function below computes the space between the stack and the heap. On ESP32, ESP.getFreeHeap() and ESP.getMinFreeHeap() give free memory and its minimum since boot. A value that drops on every loop iteration means a leak.

    cpp
    // AVR uniquement / AVR only
    int freeRam() {
      extern int __heap_start, *__brkval;
      int v;
      return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
    }
    
    // Serial.println(freeRam());
  8. Decode an ESP32 crash

    When an ESP32 crashes, it prints "Guru Meditation Error" followed by a Backtrace: 0x400d1a2c:0x3ffb1f80 … line. Those addresses translate into file and line using the program’s .elf file. With PlatformIO, the esp32_exception_decoder filter decodes them automatically in the monitor. With the Arduino IDE, export the compiled binary (Sketch > Export Compiled Binary) and pass the addresses to addr2line from the Espressif toolchain (riscv32-esp-elf-addr2line for ESP32-C3 and C6).

    ini
    ; platformio.ini
    [env:esp32dev]
    platform = espressif32
    board = esp32dev
    framework = arduino
    monitor_speed = 115200
    monitor_filters = esp32_exception_decoder
    build_type = debug
  9. Switch to the multimeter when serial is not enough

    If the monitor shows boot loops, "Brownout detector was triggered" or nothing at all, the problem may be electrical. Measure the voltage on the 5V and 3V3 pins at rest, then when the motor, relay or Wi-Fi turns on: a drop below roughly 4.5 V or 3 V causes resets. Check wire continuity, the common ground between the external supply and the board, and try another USB cable.

Mistakes to avoid

  1. Different speeds in the code and the monitor: the number one cause of garbled characters.
  2. while(!Serial) without a timeout: the board no longer starts without a PC attached.
  3. Printing inside an interrupt: Serial.print in an ISR can hang or lose data. Set a volatile flag and print in loop().
  4. Flooding the port with messages: too many traces distort timing and hide the bug. Cut them down or space them out with millis().
  5. Using pins D0 and D1 on an Uno for a module: they carry the USB serial link and disturb uploads and output.

Checklist

Did this guide help you?

Approve it in one click: it will be highlighted for other developers.

Frequently asked questions

Why does the Serial Monitor show weird characters?

The speed selected in the monitor does not match Serial.begin(). Make them identical. On ESP32 and ESP8266, a few characters at reset come from boot messages.

Why does nothing show up in the Serial Monitor?

Check the selected port, the speed, the Serial.begin() call and the cable (some only carry power). On a native USB board, messages sent before the monitor opens are lost.

How do I decode an ESP32 Guru Meditation Error?

Add monitor_filters = esp32_exception_decoder in PlatformIO, or pass the Backtrace addresses to addr2line with the sketch’s .elf file.

Which speed should I use for Serial.begin?

115200 baud suits almost every current board and prints quickly. Keep 9600 only for a module or old code that requires it.

Tools to apply this guide

More guides