How to debug Arduino Nano 33 IoT code?

The Arduino Nano 33 IoT (ATSAMD21G18, 3.3 V) is great for “real” debugging. Here’s a focused playbook just for this board.



1) Quick wins (native USB board)

  • Select the right core/board: Boards Manager → Arduino SAMD BoardsArduino Nano 33 IoT.

  • USB gotcha: a bad sketch can “hide” the port. Double-tap RESET → bootloader mode (LED pulsing) → upload again.

  • Serial init (with timeout):

void setup() { Serial.begin(115200); unsigned long t0 = millis(); while (!Serial && (millis()-t0 < 1500)) { } // don't hang forever Serial.println("Hello from Nano 33 IoT"); }
  • 3.3 V only. Don’t feed 5 V signals into pins.

2) Print-style debugging (fastest)

  • Use Serial.print/println liberally. For periodic sanity:

static uint32_t last; if (millis() - last > 1000) { last = millis(); Serial.printf("t=%lu\n", millis()); }
  • On SAMD, Serial is the USB CDC port; Serial1 is on D0/D1 if you need an external USB-TTL adapter for out-of-band logs.

3) Real step-debugging over SWD (breakpoints, watch, step)

Hardware you need (any one): Atmel-ICE, J-Link, Black Magic Probe, or CMSIS-DAP probe.
Connection (to the test pads on the underside):

  • SWDIO → SWDIO pad

  • SWCLK → SWCLK pad

  • RESET → RST pad

  • VTREF → 3V3 (board’s 3.3 V)

  • GND → GND
    (Use pogo pins or carefully solder thin wires; keep them short.)

Arduino IDE 2 steps:

  1. Tools → Programmer → your probe (e.g., Atmel-ICE).

  2. Click the bug icon (Debug). Choose the probe + Nano 33 IoT.

  3. Set breakpoints, use Continue/Step, Watch, Call Stack.

  4. If you ever lose the bootloader, Tools → Burn Bootloader (with the probe) restores it.

Tip: For rock-solid sessions, power the board via USB and connect SWD in parallel (don’t let the probe back-power it).

4) Catching crashes & hangs

Minimal HardFault trap (will blink and print if possible):

extern "C" void HardFault_Handler(void){ pinMode(LED_BUILTIN, OUTPUT); for(;;){ digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); delay(150); } }

Add timeouts to all loops waiting for hardware; never rely on infinite waits.

5) I²C/SPI/UART sanity (common culprits)

  • I²C pull-ups: 4.7 kΩ to 3.3 V typically. Slow down first: Wire.setClock(100000);

  • Bus lockups: Wire.setWireTimeout(25000, true); to auto-recover.

  • I²C scanner to verify addresses:

#include <Wire.h> void setup(){ Serial.begin(115200); Wire.begin(); } void loop(){ for (uint8_t a=1; a<127; a++){ Wire.beginTransmission(a); if(!Wire.endTransmission()) Serial.printf("0x%02X\n",a); } delay(2000); }
  • SPI: Check CS polarity and that only one device is selected at a time.

  • UART: Use Serial1 on D0/D1; match baud on both sides.

6) Wi-Fi (NINA-W102) troubleshooting

  • Update NINA firmware: Tools → WiFi101 / WiFiNINA Firmware Updater → “Test Connection” → “Update Firmware” to the version suggested by your WiFiNINA library.

  • Basic link test:

#include <WiFiNINA.h> void setup(){ Serial.begin(115200); WiFi.disconnect(); delay(200); Serial.print("FW: "); Serial.println(WiFi.firmwareVersion()); int s = WiFi.scanNetworks(); Serial.printf("Found %d networks\n", s); } void loop(){}
  • If WL_NO_MODULE, the NINA module isn’t answering → re-seat USB, update firmware, or check 3.3 V rail.

7) Timing & non-blocking pattern

Replace long delay() calls with the millis() pattern so your code stays responsive:

uint32_t tBlink=0; void loop(){ if (millis()-tBlink > 500){ tBlink = millis(); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } // …other work… }

8) Memory & strings on SAMD

  • SAMD21 has much more RAM than AVR, but still avoid huge dynamic String churn in tight loops. Prefer fixed buffers + snprintf.

  • Enable all compiler warnings (IDE Preferences) and fix them; many bugs surface there.

9) A quick “board health” checklist

  • Port shows up; double-tap reset enters bootloader if not

  • Serial prints at 115200 with a 1.5 s timeout

  • I²C devices detected by scanner; add/verify pull-ups

  • WiFiNINA firmware matches library; scan works

  • For tricky bugs, attach SWD probe and step through

评论

此博客中的热门博文

How To Connect Stm32 To PC?

What are the common HDL languages used in FPGA design?

How do you set up ADC (Analog-to-Digital Converter) in STM32?