Projects / Loop jitter histogram

Loop jitter histogram

Time how long each trip around a loop really takes with micros, sort the times into a histogram and print it as bars. See that a loop is not perfectly steady, and how a serial print inside the loop changes the spread.

Intermediate1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the button between D2 and GND

    It is read at the start of each run.

  2. NANO

    Upload and open the serial monitor at 115200

    A histogram prints every two seconds.

  3. NANO

    Leave the button alone

    Most passes fall in the first bins.

  4. NANO

    Note the quickest pass

    About 110 microseconds for an analog read.

  5. NANO

    Hold the button until the next run starts

    The mode line says with serial print inside.

  6. NANO

    Compare the two bar charts

    The print version is wider.

  7. NANO

    Try swapping the timed job

    For example a digital write.

In short

How do you build a loop jitter histogram?

Time how long each trip around a loop really takes with micros, sort the times into a histogram and print it as bars. See that a loop is not perfectly steady, and how a serial print inside the loop changes the spread. You need Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 60 minutes.

Level
Intermediate
Time
About 60 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Button leg 1Nano D2Button input
Button leg 2Nano GNDHold it to switch on the extra serial printing

Code

Even a loop that does exactly the same job every time does not take exactly the same time on every pass. The Nano's clock interrupt fires about once a millisecond and steals a few microseconds, and anything that waits, such as a full serial buffer, adds much more. The sketch times a thousand passes of a small job, an analog read, with micros, which counts in steps of 4 microseconds on a 16 megahertz Nano. It sorts each time into a bin showing how much slower than the quickest pass it was, and prints a bar chart. In the plain loop you should see nearly all the passes in the first bin or two and a few stragglers. Hold the button while the next run starts and the job also prints a character, which fills the serial buffer and creates a much wider spread and a tall last bin. Think about what this means for code that must act at an exact moment, such as reading a fast sensor or making a sound.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t BUTTON_PIN = 2;
const int BINS = 16;
const int PASSES = 1000;
unsigned int bins[BINS];

void work(bool withPrint) {
  analogRead(A0);                       // the job being timed, about 110 microseconds
  if (withPrint) Serial.print('.');     // extra work that can block when the buffer fills
}

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  bool withPrint = (digitalRead(BUTTON_PIN) == LOW);

  unsigned long base = 0xFFFFFFFFUL;    // quickest pass, from a short warm-up
  for (int i = 0; i < 50; i++) {
    unsigned long t = micros();
    work(false);
    unsigned long d = micros() - t;
    if (d < base) base = d;
  }

  memset(bins, 0, sizeof(bins));
  for (int i = 0; i < PASSES; i++) {
    unsigned long t = micros();
    work(withPrint);
    unsigned long d = micros() - t;
    int b = (d <= base) ? 0 : (int)((d - base) / 4);   // micros ticks in steps of 4 us
    if (b > BINS - 1) b = BINS - 1;                    // last bin collects all the outliers
    bins[b]++;
  }

  Serial.println();
  Serial.print("Mode: ");
  Serial.println(withPrint ? "with serial print inside" : "plain loop");
  Serial.print("Quickest pass: ");
  Serial.print(base);
  Serial.println(" us");
  for (int b = 0; b < BINS; b++) {
    Serial.print("+");
    Serial.print(b * 4);
    Serial.print(" us  ");
    Serial.print(bins[b]);
    Serial.print("  ");
    int len = (long)bins[b] * 40 / PASSES;
    if (bins[b] > 0 && len == 0) len = 1;              // show even rare bins
    for (int i = 0; i < len; i++) Serial.print('#');
    Serial.println();
  }
  delay(2000);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Mode: plain loop
Quickest pass: 112 us
+0 us  1000  ########################################
+4 us  0
+8 us  0
+12 us  0

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Plain loopNearly all passes in the first two bins
Quickest passAround 110 us for analogRead
Rare stragglersA few in the higher bins from the clock interrupt
Print modeA much wider spread and a tall last bin
Total of all binsAdds up to 1000

If it does not work

Nothing prints

Set 115200 baud in the serial monitor.

All passes in one bin

The job is very steady, try a longer job.

Text is garbled

Check the baud rate and that dots are not mixed in.

Mode never changes

Hold the button as a new run begins.

Upload fails

Choose the Old Bootloader option.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the loop jitter histogram?

Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the loop jitter histogram take to build?

About 60 minutes for a intermediate maker, in 7 steps.

How do I know the loop jitter histogram works?

Plain loop: Nearly all passes in the first two bins. Quickest pass: Around 110 us for analogRead. Rare stragglers: A few in the higher bins from the clock interrupt.

Nothing prints: what should I check?

Set 115200 baud in the serial monitor.

All passes in one bin: what should I check?

The job is very steady, try a longer job.

Text is garbled: what should I check?

Check the baud rate and that dots are not mixed in.

Mode never changes: what should I check?

Hold the button as a new run begins.