DEV Community

Hedy
Hedy

Posted on

What would you do if your Arduino program is stuck or unresponsive?

If your Arduino program is stuck or unresponsive, here’s a structured way to diagnose and fix the issue:

Image description

Step-by-Step Troubleshooting
1. Check for Infinite Loops
Look for:

cpp

while (1) { }  // or while (true) { }
Enter fullscreen mode Exit fullscreen mode

These are fine if intentional, but can freeze your program if misused.

Fix: Use time-based checks (e.g., millis()) instead of blocking loops.

2. Verify Serial Communication
If you use Serial.read() or Serial.available():

  • Ensure something is actually sending data.
  • An empty or blocked while (!Serial) (especially on boards like Leonardo) can freeze early execution.

Fix:

cpp

// Add timeout or check
if (Serial.available() > 0) {
  char c = Serial.read();
}
Enter fullscreen mode Exit fullscreen mode

3. Avoid Blocking Functions
Functions like:

cpp

delay(10000); // 10 seconds!
Enter fullscreen mode Exit fullscreen mode

...stop everything during their duration.

Fix: Use millis() for non-blocking timing:

cpp

if (millis() - lastUpdate > 1000) {
  lastUpdate = millis();
  // do something
}
Enter fullscreen mode Exit fullscreen mode

4. Use Serial Debugging
Sprinkle your code with debug messages:

cpp

Serial.println("Checkpoint A");
Enter fullscreen mode Exit fullscreen mode

You’ll quickly see where it gets stuck.

5. Check for Hardware Conflicts

  • Disconnect everything except USB.
  • Peripherals on pins D0/D1 (RX/TX) can block upload or Serial.
  • Bad sensors can hold I2C/SPI buses low.

Fix:

  • Disconnect modules
  • Use other pins (e.g., SoftwareSerial)
  • Check SDA/SCL with a multimeter

6. Reset to a Known Good Sketch
Upload this minimal code:

cpp

void setup() {}
void loop() {}
Enter fullscreen mode Exit fullscreen mode

If this works, your board is okay and the problem is in your code.

7. Watch Out for Stack Overflows
Recursion or large arrays in local variables can crash the stack.

Fix: Use global variables, or reduce memory use:

cpp

int bigArray[1000]; // ⚠ May crash
Enter fullscreen mode Exit fullscreen mode

8. Power Supply Problems
Boards acting erratically might be underpowered.

Fix: Use a stable power supply or powered USB hub.

9. Check for EEPROM or Watchdog Misuse
Using EEPROM.put() incorrectly or a misconfigured watchdog timer can cause hangs or resets.

Fix: Comment out EEPROM code or disable WDT during testing.

10. Use the Reset Button
Pressing RESET can sometimes free a stuck board and allow a new upload.

Recovery Tip: Double-Reset Bootloader Trick
On boards like Arduino Nano 33 IoT, Arduino Leonardo, or some ESP32, double-tapping RESET forces bootloader mode.

This can help recover a stuck or unresponsive board for uploading.

Summary: Fix Table

Image description

Top comments (0)