Clock rollover timer bug lab
A Nano timer built the wrong way fails after about fifty days when the clock number wraps round, while one built the right way keeps going. A small test starts the clock near the wrap so you see the bug in seconds instead of weeks.
Build it step by step
0 of 7 doneWire the two LEDs
D8 and D9 through 220 ohms to GND.
Upload and open the serial monitor at 9600
A start-up line prints.
Watch both LEDs for five seconds
They blink together.
Wait for the wrap message
It prints at about 5 seconds.
Watch the first LED
It misbehaves after the wrap.
Press the Nano reset button and watch again
The same thing happens each run.
Try fixing the first timer with subtraction
Both LEDs now keep blinking.
How do you build a clock rollover timer bug lab?
A Nano timer built the wrong way fails after about fifty days when the clock number wraps round, while one built the right way keeps going. A small test starts the clock near the wrap so you see the bug in seconds instead of weeks. You need Arduino Nano, LED 5 mm red × 2, Resistor 220 Ω × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 50 minutes.
- Level
- Beginner
- Time
- About 50 min
- Parts
- 5
Parts list
- Development BoardsBuyArduino Nano × 1
- SemiconductorsBuyLED 5 mm red × 2
- PassivesBuyResistor 220 Ω × 2
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| LED 1 long leg | Nano D8 through a 220 ohm resistor | The timer with the bug |
| LED 1 short leg | Nano GND | Return path |
| LED 2 long leg | Nano D9 through a 220 ohm resistor | The timer done correctly |
| LED 2 short leg | Nano GND | Return path |
Code
The function millis counts milliseconds in a number that holds up to about 4.29 billion, so it runs out after about 49.7 days and wraps back to zero. A timer written as "is now greater than the time I wanted?" fails at that moment, because the wanted time was computed just before the wrap and is a huge number, or has itself wrapped. A timer written as "has the gap since I last fired reached the interval?" keeps working, because unsigned subtraction wraps in the same way and gives the right gap. To see this without waiting seven weeks the sketch adds an offset to millis, so the wrap comes after 5 seconds. Watch the two LEDs. Both blink twice a second at first, and at about 5 seconds the first one misbehaves, flickering wildly or freezing, while the second carries on. Note the rule: always compare time differences, never absolute times, and keep time values as unsigned long.
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).
// millis() runs for about 49.7 days and then wraps back to zero.
// This test adds an offset so that the wrap arrives after only 5 seconds.
unsigned long fastClock() {
return millis() + (0xFFFFFFFFUL - 5000UL);
}
const uint8_t BAD_LED = 8;
const uint8_t GOOD_LED = 9;
const unsigned long INTERVAL = 500;
unsigned long nextBad = 0; // wrong way: remember when to fire next
unsigned long lastGood = 0; // right way: remember when we last fired
bool badState = false, goodState = false;
bool wrapReported = false;
void setup() {
pinMode(BAD_LED, OUTPUT);
pinMode(GOOD_LED, OUTPUT);
Serial.begin(9600);
Serial.println("Both LEDs should blink twice a second. Watch them after the wrap at about 5 s.");
nextBad = fastClock() + INTERVAL;
lastGood = fastClock();
}
void loop() {
unsigned long now = fastClock();
if (now >= nextBad) { // BUG: breaks when the number wraps
nextBad = now + INTERVAL;
badState = !badState;
digitalWrite(BAD_LED, badState);
}
if (now - lastGood >= INTERVAL) { // CORRECT: subtraction survives the wrap
lastGood += INTERVAL;
goodState = !goodState;
digitalWrite(GOOD_LED, goodState);
}
if (!wrapReported && now < 10000UL) {
wrapReported = true;
Serial.println("The clock number has just wrapped round to zero.");
}
}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.
Both LEDs should blink twice a second. Watch them after the wrap at about 5 s.
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Before 5 seconds | Both LEDs blink twice a second together |
| Wrap message | Prints at about 5 seconds |
| After the wrap | The first LED flickers or freezes |
| Second LED | Keeps its steady blink |
| After your fix | Both keep blinking |
If it does not work
Both LEDs misbehave
Check the second timer uses subtraction.
Neither LED blinks
Check the LED direction and pins.
No wrap message
Set 9600 baud and wait a little longer.
LEDs flicker from the start
Check INTERVAL is 500.
Upload fails
Choose the Old Bootloader option.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the clock rollover timer bug lab?
Arduino Nano, LED 5 mm red × 2, Resistor 220 Ω × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the clock rollover timer bug lab take to build?
About 50 minutes for a beginner maker, in 7 steps.
How do I know the clock rollover timer bug lab works?
Before 5 seconds: Both LEDs blink twice a second together. Wrap message: Prints at about 5 seconds. After the wrap: The first LED flickers or freezes.
Both LEDs misbehave: what should I check?
Check the second timer uses subtraction.
Neither LED blinks: what should I check?
Check the LED direction and pins.
No wrap message: what should I check?
Set 9600 baud and wait a little longer.
LEDs flicker from the start: what should I check?
Check INTERVAL is 500.
