Debug an Arduino with the Serial Monitor
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.
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
-
Start the serial port at the right speed
Call
Serial.begin()once insetup(), then open the monitor with Tools > Serial Monitor (Ctrl+Shift+MorCmd+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.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() { } -
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,Serialis always ready and the loop is useless. -
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. -
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. TheF()macro keeps constant strings in flash memory instead of RAM, which is precious on an Uno (2 KB). Print registers and bytes in hexadecimal withSerial.print(x, HEX). On ESP32,Serial.printf()accepts a C-style format.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()); -
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:valuepairs separated by commas: each label becomes a curve. Print nothing else on the port meanwhile, otherwise the plotter treats your messages as data.Serial.print(F("consigne:")); Serial.print(target); Serial.print(F(",mesure:")); Serial.println(measured); -
Create switchable debug macros
Serial.printcalls slow the program down (at 9600 baud, one character takes about 1 ms) and use memory. Wrap them in macros: withDEBUGset 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.#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)); } -
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
Stringobjects handled inloop(). On AVR (Uno, Nano, Mega), the function below computes the space between the stack and the heap. On ESP32,ESP.getFreeHeap()andESP.getMinFreeHeap()give free memory and its minimum since boot. A value that drops on every loop iteration means a leak.// 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()); -
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.elffile. With PlatformIO, theesp32_exception_decoderfilter decodes them automatically in the monitor. With the Arduino IDE, export the compiled binary (Sketch > Export Compiled Binary) and pass the addresses toaddr2linefrom the Espressif toolchain (riscv32-esp-elf-addr2linefor ESP32-C3 and C6).; platformio.ini [env:esp32dev] platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 monitor_filters = esp32_exception_decoder build_type = debug -
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
- Different speeds in the code and the monitor: the number one cause of garbled characters.
while(!Serial)without a timeout: the board no longer starts without a PC attached.- Printing inside an interrupt:
Serial.printin an ISR can hang or lose data. Set avolatileflag and print inloop(). - Flooding the port with messages: too many traces distort timing and hide the bug. Cut them down or space them out with
millis(). - 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
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug Node.js with breakpoints (VS Code, Chrome)
- Debug PHP with Xdebug 3 (PhpStorm, VS Code)
- Read Docker container logs and debug a crashing container
- Find the commit that broke your code with git bisect
- How to create a minimal reproducible example