Arduino millis() timing breaks after days: use unsigned long and subtract

Store millis() in an unsigned long and compare by subtraction. Written like that, the timer survives the roll-over.

What the message means

millis() counts up to a large number and then starts again from zero. A signed or smaller variable overflows sooner and breaks the comparison.

Fix it in this order

  1. Declare the variable as unsigned long.
  2. Use if (millis() – last >= interval) and then set last = millis().
  3. Do not write if (millis() > last + interval).
  4. Do not store millis() in an int, which wraps after about 32 seconds on an Uno.
  5. Use micros() for short times, and know that it rolls over much sooner.

Why it happens

millis() returns an unsigned long that wraps back to zero after about 49.7 days. Code that compares absolute times can fail at that point, and a variable that is too small, such as an int, wraps much sooner.

Store times in unsigned long variables and compare by subtracting, which stays correct across the wrap.

unsigned long last = 0;
if (millis() - last >= 1000UL) {   // works across the wrap
  last = millis();
}

Example sketch: change pins and values for your own wiring. It has not been bench-tested on your hardware.

If you see this, do this

You seeDo this
Timer stops after some secondsVariable is an int.
Timer fails after weeksUse subtraction.

delay and millis, integer division.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

millis() returns an unsigned 32-bit count of milliseconds since the board started. It reaches 4294967295 and then wraps to 0 after about 49.7 days (2 to the power 32 milliseconds). If the code stores time in an int, the limit is far sooner: an int on an Uno is 16 bits and overflows past 32767, which is about 32.8 seconds, so timing breaks almost at once.

Subtraction in unsigned arithmetic wraps the same way the counter does, so now - start stays correct across the wrap. Comparing now >= start + interval does not, because the sum itself can wrap and become a small number.

How to confirm the cause

  • Print sizeof(int) and sizeof(unsigned long). On an Uno they are 2 and 4. If your timing variables are declared int, they are too small.
  • Search the sketch for comparisons that add the interval to a stored time. Every one is a candidate for the wrap bug.
  • Search for variables that store the result of millis(). They should all be unsigned long.
  • Simulate the wrap on the bench by running the same code with a fake clock variable that starts near 4294967000, as in the example below. Correct code keeps its rhythm; broken code fires at once or stalls.

Worked example

A simulation with a fake clock that is 296 ms from the wrap. The interval is 1000 ms.

unsigned long start = 4294967000UL;      // 296 ms before the wrap
unsigned long now   = 200;                // clock after wrapping

// right: wraps like the counter does
unsigned long elapsed = now - start;      // 200 + 296 = 496
if (elapsed >= 1000) { /* not yet: 496 < 1000 */ }

// wrong: start + 1000 wraps to 704, so just after start
// (now = 4294967100) the test 4294967100 >= 704 is already true
if (now >= start + 1000) { /* fires at once */ }

In the wrong form the due time start + 1000 wraps to 704, which is a small number. Any reading taken before the wrap is a huge number, so the test passes immediately and the timed action fires at once instead of after one second. The subtraction form gives 496 ms elapsed at now = 200 and 1000 ms at now = 704, which is correct.

Common mistakes

  • Using int or long (signed) for stored times.
  • Storing a ‘next due’ time and comparing with the greater-than operator.
  • Resetting start to 0 instead of to the current millis() after each event.
  • Using delay() for long waits and mixing it with millis() timers in one sketch.
  • Testing for a few minutes only, so the bug appears weeks after deployment.

If this did not fix it

  • A different timer overflow, such as micros(), which wraps after about 71.6 minutes.
  • A board that resets (watchdog, brown-out) and restarts the clock without you noticing.
  • Floating point maths on times, which loses precision as the numbers grow.
  • Another library changing the timer prescaler and so the speed of millis().