Bench NotesUno handbook

Debugging Arduino Sketches

A repeatable process for finding faults: switchable debug prints, a heartbeat LED and checks with a meter.

IntermediateSerialLED

Overview

Most "the code doesn't work" problems are wiring, power or a wrong assumption about what the code is doing. The fastest route is to make the sketch tell you what it sees, and to test one part at a time.

You will need

  • Arduino Uno R3
  • Potentiometer on A0 (as a sample input)
  • Multimeter (recommended)

Wiring

Uno pinConnects toNotes
A0Potentiometer wiperExample input; ends to 5V and GND
13On-board L LEDHeartbeat

A checklist

  1. Is the code running? A heartbeat LED that blinks steadily proves the loop is not stuck.
  2. What does the input really read? Print raw values before any maths.
  3. Is the wiring what you think? Check each connection against the table, then measure voltages with a meter.
  4. Are the grounds joined? Every external supply must share GND with the Uno.
  5. Does a minimal sketch work? Strip the program to the one part in doubt.
  6. Change one thing at a time and note the result.

Symptoms and likely causes

SymptomUsually
Board resets when something switches onMotor or servo on the Uno's 5V; separate supply needed
Input reads random valuesFloating pin: missing pull-up or pull-down
Works only when you touch a wireLoose breadboard contact or missing ground
Output invertedActive-low wiring or common-anode display
Strange crashes after adding textOut of RAM; use F() for strings

The sketch

debug_toolkit.ino
#define DEBUG 1   // set to 0 to remove all debug output

#if DEBUG
  #define DEBUG_PRINT(x)   Serial.print(x)
  #define DEBUG_PRINTLN(x) Serial.println(x)
#else
  #define DEBUG_PRINT(x)
  #define DEBUG_PRINTLN(x)
#endif

const int SENSOR_PIN = A0;
const int THRESHOLD = 512;
unsigned long lastBeatMs = 0;
bool beatOn = false;
bool wasAbove = false;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
#if DEBUG
  Serial.begin(9600);
#endif
  DEBUG_PRINTLN(F("Debug build started"));
}

void loop() {
  // Heartbeat: toggles every 500 ms while loop() keeps running
  if (millis() - lastBeatMs >= 500) {
    lastBeatMs = millis();
    beatOn = !beatOn;
    digitalWrite(LED_BUILTIN, beatOn ? HIGH : LOW);
  }

  int value = analogRead(SENSOR_PIN);
  bool isAbove = value > THRESHOLD;

  // Report only changes, so the output stays readable
  if (isAbove != wasAbove) {
    DEBUG_PRINT(F("Crossed threshold, value = "));
    DEBUG_PRINTLN(value);
    wasAbove = isAbove;
  }
}

How the code works

Printing only on changes avoids flooding the Monitor, which can itself slow a sketch down. With DEBUG set to 0 the macros expand to nothing, so the final build carries no debug cost.

What you should observe

  • The L LED blinks steadily twice per second.
  • One line is printed each time the potentiometer crosses the middle.