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 Boards → Arduino 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):
-
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:
-
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:
-
Tools → Programmer → your probe (e.g., Atmel-ICE).
-
Click the bug icon (Debug). Choose the probe + Nano 33 IoT.
-
Set breakpoints, use Continue/Step, Watch, Call Stack.
-
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):
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:
-
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:
-
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:
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
评论
发表评论