Lessons and the learning game

Clocks, RTCs and time

Keep real time on a microcontroller: millis() rollover, three RTC chips, time zones and daylight saving, NTP, alarms, and a sunrise alarm clock.

Take the course at /learn/path/clocks-and-time. It is an intermediate course: it assumes the Uno course.

The lessons

Counting time. A now() that starts five seconds before millis() wraps from 4294967295 to 0: a blinker using now - last >= interval keeps its beat, one using now >= last + interval toggles over 23,000 times in a second. Then a DS1307's BCD registers, its clock-halt bit and its drift against the DS3231, and the DS1302's three-wire bus, write-protect bit and 31 bytes of RAM.

Wall clock time. A DS3231 that keeps UTC and a TM1637 that shows local time, with the daylight saving rules worked out for any year:

  • United States: from 2:00 local time on the second Sunday in March to 2:00 local daylight time on the first Sunday in November.
  • European Union: from 01:00 UTC on the last Sunday in March to 01:00 UTC on the last Sunday in October.

Then NTP on an ESP32-C3: Unix time in UTC, an offset added by the sketch (India's 5:30 included), and a resync interval.

Alarms. The DS3231's two alarms, their flags, and a daily schedule table.

Boss. A sunrise alarm clock: a 16 LED ring that rises from deep red to warm white before 6:30 on weekdays, local time with US daylight saving, and a nine-minute snooze typed on the serial monitor.

The benches

  • millis(): the RTOS bench's lamps on pins 13 and 12.
  • DS1307: the clock and memory board, at 0x68 on A4 and A5.
  • DS1302: the alarm clock, CLK on 5, DAT on 4, RST on 2, a TM1637 on 7 and 6.
  • Time zones and alarms: the desk clock, a DS3231 and a TM1637 on 4 and 5.
  • NTP: the gallery's ESP32-C3 rainbow strip, the strip on GPIO 5.
  • Boss: the New Year countdown, a DS3231, a TM1637 on 4 and 5 and a 16 LED ring on pin 6.

Each lesson sets its clock's start time so it opens just before the moment it is about.

Honest limits

  • NTP is simulated. The answer is the simulation's own clock, 2026-01-01 00:00:00 UTC when you press Run plus the seconds since, not today's date. On a real board the same sketch gets the real time.
  • The crystals do not drift in the simulator: the 20 ppm and 2 ppm figures are the datasheets', worked out, not measured.
  • A halted DS1307 here starts from 2000-01-01, where its registers start; a halted DS1302 here keeps the time it was set to.
  • The daylight saving rules are the ones in force in 2026. Laws change; real products use the tz database.
  • The DS3231's SQW alarm output is not wired on these benches; the sketches poll the alarm flags.

Data logging: SD, EEPROM and flash puts RTC timestamps into log files.