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 pin | Connects to | Notes |
|---|---|---|
| A0 | Potentiometer wiper | Example input; ends to 5V and GND |
| 13 | On-board L LED | Heartbeat |
A checklist
- Is the code running? A heartbeat LED that blinks steadily proves the loop is not stuck.
- What does the input really read? Print raw values before any maths.
- Is the wiring what you think? Check each connection against the table, then measure voltages with a meter.
- Are the grounds joined? Every external supply must share GND with the Uno.
- Does a minimal sketch work? Strip the program to the one part in doubt.
- Change one thing at a time and note the result.
Symptoms and likely causes
| Symptom | Usually |
|---|---|
| Board resets when something switches on | Motor or servo on the Uno's 5V; separate supply needed |
| Input reads random values | Floating pin: missing pull-up or pull-down |
| Works only when you touch a wire | Loose breadboard contact or missing ground |
| Output inverted | Active-low wiring or common-anode display |
| Strange crashes after adding text | Out of RAM; use F() for strings |
The sketch
#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.