All courses

Arduino C++ · Lesson 35 of 40

Debug one layer at a time

Debugging, Testing, and Portability

Find the first broken assumption

Electronics workbench with an Arduino-style board and a multimeter
Check one layer at a time: power, connections, raw readings, then application logic. Generated equipment illustration.

Classify the failure before changing code. Compilation errors occur before the program reaches the board. Upload errors involve the selected target, USB connection, bootloader, or port. Runtime errors occur after a successful upload and can be electrical, logical, or timing-related.

  1. Return to a minimal known-good sketch such as Blink. Keep a copy of the failing sketch.
  2. Disconnect external hardware and confirm upload and stable execution.
  3. Add one component, checking its supply and signal levels against its documentation.
  4. Log the raw input before applying filters, conversion, thresholds, or control logic.
  5. Force a known input and predict the exact output. Change one thing at a time.
  6. Record the smallest case that reproduces the fault, including wiring, board, core, library version, and observed messages.

When measuring voltage, connect the meter across the points being compared. Continuity tests are for unpowered circuits. Current measurement is in series and requires the correct meter socket and range; placing an ammeter straight across a supply can short it.

Map symptoms to checks

SymptomCheck firstUseful isolation test
Board lights but no USB portData cable and portUse a known data cable with no peripherals
LED never lightsPin, polarity, and groundTest onboard Blink, then external LED wiring
Input changes by itselfFloating pin or loose groundUse a defined pull-up/down and a short wire
Sensor reads zeroMissing device or invalid conversionPrint raw value plus a separate validity flag
Motor starts and board resetsSupply sag or interferenceDisconnect motor and test logic alone
Button responds lateBlocking code or long peripheral callsRemove delay and measure loop duration

Avoid printing every iteration of a fast loop: a filled transmit buffer can make Serial.print wait and change the timing you are trying to observe. Print at a modest interval or capture a short trace.

Practice: Intentionally select the wrong baud rate, then restore it. Intentionally break an unpowered LED ground connection, reconnect power to observe the fault, then power down and restore it. Learn the symptom without creating a short circuit.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.