Serial.write() blocking when transmit buffer fills on AVR Arduino
0 reputation · 24 Apr 2025, 07:36 UTC
Goal
Identify a reliable way to measure execution time of a tight loop on an AVR-based Arduino (Uno, Nano) without the measurement itself perturbing the result.
Constraints and uncertainty
On AVR cores, HardwareSerial.write() and print() block once the 64-byte software TX buffer is full, so any Serial.print()-based timing inside the loop inflates the measured interval once backpressure occurs. The built-in micros() offers roughly 4 µs resolution at 16 MHz, which limits the shortest measurable region. Direct port toggling observed with an oscilloscope or logic analyzer avoids serial distortion, but requires external hardware. No official Arduino profiling facility exists, leaving the choice of instrumentation to the developer.
Questions
- For a loop that runs in the 10–100 µs range, which combination of
micros()timestamping and pin-toggle verification gives the least perturbed measurement on a stock Uno? - How should the measured region be structured so the compiler does not optimize away the code under test?