Your "once a second" code breaks after 71 minutes
micros() returns an unsigned long, which is 32 bits on the ESP32. After 71 minutes 35 seconds it starts again from zero, and an interval check written as micros() >= last + interval fires on every pass through loop() for up to a second.
In short
micros()is 32 bits on the ESP32: it wraps after 232 µs, 71 min 35 s.millis()wraps after 49.7 days.- In a real run from power-on on an ESP32-S3,
if (micros() >= last + 1000000)fired 138,001 times in the second of the wrap, in about 0.46 s. The rest of the time it fired once a second. if (micros() - last >= 1000000)fired once a second before, during and after the wrap. Unsigned subtraction wraps too, and the difference comes out right.- The same happens on an Arduino Uno at 71 minutes, and with
millis()at 49.7 days (checked by fast-forwarding the ESP32 timer). - On the ESP32,
esp_timer_get_time()is 64-bit and won't wrap for about 292,000 years.
Once a second, then 138,000 times
This is a common way to run something once a second:
if (micros() >= last + 1000000) {
last = micros();
doSomething();
}
On an ESP32-S3 it works: once a second, every second, for 71 minutes. Then, in less than half a second, it runs 138,001 times. The chip does not crash and nothing else is wrong. The problem is in how the comparison is written.
Why 71 minutes
micros() counts microseconds since the board started. It returns an unsigned long, and on the ESP32 that is 32 bits: GCC for Xtensa defines LONG_TYPE_SIZE as 32, and the test sketch prints sizeof(unsigned long)=4.
The largest value a 32-bit unsigned number can hold is 232 − 1:
2^32 − 1 = 4,294,967,295 µs
2^32 µs = 4,294.967 s = 71 min 34.97 s ≈ 71 min 35 s
One microsecond after the maximum, the counter starts again from zero. There is no 33rd bit to carry into.
The Arduino reference rounds this to “approximately 70 minutes” on the micros() page and “about 71 minutes” on the millis() page. The exact figure is 71 minutes 35 seconds.
64 bits inside, 32 bits outside
Inside, the ESP32 keeps time in 64 bits. ESP-IDF's esp_timer_get_time() returns the time in microseconds since boot as an int64_t. The Arduino core simply casts it down to 32 bits:
// arduino-esp32 3.3.12, cores/esp32/esp32-hal-misc.c
unsigned long ARDUINO_ISR_ATTR micros() {
return (unsigned long)(esp_timer_get_time());
}
millis() is built the same way, from the same 64-bit timer:
(unsigned long)(esp_timer_get_time() / 1000ULL)
On the board this is visible directly: at the wrap, micros() drops to a small number while the 64-bit time keeps growing (see the log below).
Where the comparison breaks
The bad version waits until micros() reaches a moment in the future, last + 1000000. Just before the wrap, that future moment does not fit in 32 bits either. Unsigned arithmetic in C++ is performed modulo 2n, so the sum wraps to a small number:
last = 4,294,000,000
last + 1,000,000 = 4,295,000,000 − 2^32 = 32,704 (wrapped)
micros() = 4,294,500,000 >= 32,704 → true
micros() itself has not wrapped yet, so it is still huge, and the check is true. The code runs and sets last = micros(), which is again just below the wrap, so last + 1000000 wraps again and the check is true on the next pass too. This repeats on every pass through loop() until micros() wraps as well. After that the numbers are small again and the code returns to once a second.
The fix: subtract, then compare
Compute how much time has passed, and compare that with the interval:
if (micros() - last >= 1000000) {
last = micros();
doSomething();
}
The subtraction wraps too, and that is exactly why it works. last was taken just before the wrap, micros() is just after it, and the modulo-232 difference is still the real elapsed time:
last = 4,294,000,000
micros() = 32,704 (after the wrap)
micros() − last = 32,704 − 4,294,000,000 + 2^32 = 1,000,000 → exactly 1 s
This is the form used in Arduino's official BlinkWithoutDelay example:
if (currentMillis - previousMillis >= interval) {
The same example notes: “Generally, you should use "unsigned long" for variables that hold time.” Both parts matter: the time variables are unsigned long, the same type as the return value of micros() and millis(), so the subtraction wraps the same way the counter does.
Measured on the board
The test sketch runs both versions side by side in loop(), for micros() with a 1,000,000 µs interval and for millis() with a 1,000 ms interval. Once a second, timed by the 64-bit esp_timer_get_time(), it prints how many times each version fired during that second and how many passes loop() made.
ESP32-S3: real run from power-on
The board ran for 75 minutes from power-on, past the micros() wrap. No timer tricks.
- In 4,498 of 4,500 seconds both versions fired exactly once. The first, incomplete second had 0 for both.
- One second was the burst: the bad version fired 138,001 times, the good version 1 time.
loop()made 306,687 passes in that second, against a usual 307,253.micros()wrapped at 232 µs = 71 min 34.97 s after the timer started.
Three report lines around the wrap. Columns: 64-bit time (µs), micros(), millis(), loop passes, bad and good micros() versions, bad and good millis() versions:
4294509983,4294509984,4294509,307253,1,1,1,1
4295509982,542687,4295509,306687,138001,1,1,1
4296509982,1542687,4296509,307253,1,1,1,1
The 64-bit time goes on from 4,294,509,983 to 4,295,509,982. micros() goes from 4,294,509,984 to 542,687. The millis() versions were not affected in this run: their wrap is 49.7 days away.
The counts differ between runs because the length of the burst depends on timing, as explained next. The subtracting version fired once in every one of these seconds.
How long the burst lasts
The burst starts at the last normal firing before the wrap: after it, last + interval no longer fits in 32 bits, and the check is true on every pass. It stops when micros() wraps. So the burst lasts from that firing to the wrap: anywhere from 0 to 1 second, depending on where in the second the code happens to fire.
In the real run the last normal firing was at about 4,294.51 s, and the wrap at 4,294.967 s:
burst ≈ 4,294.967 − 4,294.51 ≈ 0.46 s
307,253 passes/s × 0.457 s ≈ 140,000
That agrees with the measured 138,001. The bad branch does a little more work on each pass than an idle pass, so slightly fewer passes fit in that time.
In the fast-forwarded runs, the last normal firing happened to land almost exactly one second before the wrap, so the burst took nearly the whole second: 305,927 of about 307,000 passes. Together the two cases show the range: the burst can be very short or nearly a full interval, but it does not go away.
millis() and the Arduino Uno
millis(): 49.7 days, same bug, same fix
millis() is also an unsigned long. It wraps after 232 ms = 49.71 days. The Arduino reference rounds this to “approximately 50 days” in one place and “about 49 days” in another.
Waiting 49.7 days for a test is not practical, so the ESP32 timer was fast-forwarded: in setup(), esp_timer_private_set() sets the 64-bit timer to 8 seconds before the wrap, and from there everything runs in real time. It is a test method: the function comes from ESP-IDF's private header esp_private/esp_timer_private.h.
| Run | Bad, millis() | Good, millis() |
|---|---|---|
| r1 | 301,424 | 1 |
| r2 | 301,424 | 1 |
| r3 | 301,342 | 1 |
millis() drops from 4,294,966,296 to 0. The same burst, and the same fix works. Since 232 ms is 1000 × 232 µs, micros() wraps at the same moment, and its bad version shows the same burst.
micros() fast-forwarded: three repeats
The same method was used to repeat the micros() wrap: timer set to 232 − 8,000,000 µs.
| Run | Bad, micros() | Good, micros() |
|---|---|---|
| r1 | 305,927 | 1 |
| r2 | 305,928 | 0* |
| r3 | 305,927 | 1 |
Arduino Uno: the same at 71 minutes
On the Uno, micros() also returns a 32-bit unsigned long (sizeof(unsigned long)=4). The Uno has no 64-bit timer, so the sketch builds one by adding up micros() differences. The board ran for 75 minutes from power-on, making about 59,460 passes through loop() per second.
micros()went from 4,294,001,044 to 33,744 between the 4,294 s and 4,295 s reports: the same 232 µs.- In the second of the wrap: bad 50,793, good 1.
- The other 4,498 seconds: once each.
The bug is not specific to the ESP32. It comes from the 32-bit counter and the way the comparison is written.
The 64-bit alternative
On the ESP32 you can also read the 64-bit timer directly:
int64_t now = esp_timer_get_time(); // microseconds since boot, 64 bits
It is a signed 64-bit value, so it overflows after 263 µs:
2^63 µs = 9.22 × 10^18 µs ≈ 292,271 years
For an interval check with micros() or millis(), the subtracting form is enough. Keep times in unsigned long, subtract, then compare.
Caveats
- The
micros()wrap on the ESP32-S3 and on the Uno was checked in real runs from power-on. Themillis()wrap was checked only with the fast-forwarded timer. - 71 min 35 s and 49.7 days are calculated from 232. The Arduino documentation gives rounded figures (“approximately 70 minutes”, “approximately 50 days”, “about 49 days”).
- The burst counts depend on how fast
loop()runs and on the phase of the last firing before the wrap. They are examples, not constants: a sketch with a slowerloop()makes fewer passes in the same time.
Test setup and code
| Board | ESP32-S3, USB-UART bridge CH343, 240 MHz |
| Core | arduino-esp32 3.3.12, board esp32:esp32:esp32s3, default settings |
| Comparison board | Arduino Uno, arduino:avr 1.8.8 |
| Method | bad (now >= last + interval) and good (now - last >= interval) versions side by side in loop(), for micros() (1,000,000 µs) and millis() (1,000 ms); once a second by the 64-bit timer, print how many times each fired |
| Real runs | 75 minutes from power-on, ESP32-S3 and Uno |
| Repeats | ESP32-S3, esp_timer_private_set() in setup() to 8 s before the wrap, 3 runs for micros() and 3 for millis() |
// JUMP_TO_US = 0 -> honest run from boot (wrap of micros() at ~71.6 min)
// JUMP_TO_US > 0 -> esp_timer is set to this value in setup() (fast repeats)
#define JUMP_TO_US 0ULL
#include "esp_timer.h"
#include "esp_private/esp_timer_private.h"
const unsigned long INTERVAL_US = 1000000; // 1 s
const unsigned long INTERVAL_MS = 1000; // 1 s
unsigned long lastBadUs = 0, lastGoodUs = 0, lastBadMs = 0, lastGoodMs = 0;
uint32_t badUs = 0, goodUs = 0, badMs = 0, goodMs = 0, loops = 0;
void loop() {
unsigned long now = micros();
// BAD: compare against a future timestamp
if (now >= lastBadUs + INTERVAL_US) { lastBadUs = now; badUs++; }
// GOOD: subtract, then compare
if (now - lastGoodUs >= INTERVAL_US) { lastGoodUs = now; goodUs++; }
unsigned long nowMs = millis();
if (nowMs >= lastBadMs + INTERVAL_MS) { lastBadMs = nowMs; badMs++; }
if (nowMs - lastGoodMs >= INTERVAL_MS) { lastGoodMs = nowMs; goodMs++; }
loops++;
// ... once a second by esp_timer_get_time(): print the counters and reset them
}
Also: wrapjump.ino and wrapjumpms.ino, the same sketch with JUMP_TO_US set to 8 s before the micros() and the millis() wrap; wrapuno.ino, the version for the Arduino Uno.
Sources
- arduino-esp32 3.3.12: cores/esp32/esp32-hal-misc.c
unsigned long ARDUINO_ISR_ATTR micros() { return (unsigned long)(esp_timer_get_time()); } - GCC: gcc/config/xtensa/xtensa.h
#define LONG_TYPE_SIZE 32 - Arduino Reference: micros() (source) “This number will overflow (go back to zero), after approximately 70 minutes.”
- Arduino Reference: millis() “approximately 50 days” · “about 49 days” · micros “in about 71 minutes”
- ESP-IDF v5.5: esp_timer.h and ESP Timer documentation (ESP32-S3) “Get time in microseconds since boot” ·
int64_t esp_timer_get_time(void); - cppreference: arithmetic operators, Overflows “Unsigned integer arithmetic is always performed modulo 2n where n is the number of bits in that particular integer.”
- Arduino examples: BlinkWithoutDelay.ino
if (currentMillis - previousMillis >= interval) {· “Generally, you should use "unsigned long" for variables that hold time”
Video
A short video on the same topic is on the ESPSSS YouTube channel (2:30, 2026-11-24). This page goes into more detail.
Also as a YouTube Short.