Lesson plan

Clocks, RTCs and time lesson plan

Keep real time: millis() rollover tested in seconds, the DS1307, DS3231 and DS1302 clocks, time zones and the US and EU daylight saving rules, NTP on an ESP32-C3, alarms and schedules, and a sunrise alarm clock.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • millis() and the clock chips that keep the calendar.
  • Time zones, daylight saving and the network.
  • Let the clock chip watch the time.
  • A sunrise alarm clock that wakes you with light.

Materials for the whole unit

  • Addressable RGB LEDs
  • Arduino Uno R3
  • Character LCD with I2C backpack
  • ESP32-C3-DevKitM-1
  • Four-digit display, TM1637
  • Full-size breadboard
  • Half breadboard
  • I2C EEPROM, 24LC256
  • LED x 3
  • Logic analyzer, four channels
  • Piezo speaker
  • Potentiometer
  • Pushbutton
  • Real-time clock, DS1302
  • Real-time clock, DS1307
  • Real-time clock, DS3231
  • Resistor x 3

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 2 h 30 min

Counting time

millis() and the clock chips that keep the calendar.

LessonTimeWhat students do
millis() rollovermillis() is a 32-bit count that wraps to 0 after 49.7 days: a clock that starts five seconds before the wrap, a blinker that survives it with subtraction, and one that goes haywire.50 min
  1. Run it: five seconds to the wrap
  2. The wrap
  3. Yellow goes haywire
  4. Why subtraction is safe
  5. Fix the yellow lamp
  6. Testing what takes 49 days
DS3231 vs DS1307Inside a DS1307: a 32.768 kHz crystal, seven BCD registers, the clock-halt bit and a coin cell, and why the DS3231 drifts ten times less.50 min
  1. Run it: a clock on the bus
  2. Numbers in BCD
  3. The clock-halt bit
  4. How far it drifts
  5. A better crystal
  6. Choosing an RTC
The DS1302 on three wiresA DS1302 on its own three-wire bus: CE, a clock and one data line both ways, write-protect and halt bits, 31 bytes of battery-backed RAM, and setting the time.50 min
  1. Run it: a clock on a display
  2. Three wires, one of them both ways
  3. Halted and protected
  4. RAM that lasts
  5. Set the time
  6. When to use a DS1302

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Four-digit display, TM1637
  • Full-size breadboard
  • Half breadboard
  • I2C EEPROM, 24LC256
  • LED x 3
  • Piezo speaker
  • Potentiometer
  • Pushbutton
  • Real-time clock, DS1302
  • Real-time clock, DS1307
  • Resistor x 3

Check for understanding

Each student finishes "The DS1302 on three wires" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "When to use a DS1302".

Chapter 2. About 1 h 40 min

Wall clock time

Time zones, daylight saving and the network.

LessonTimeWhat students do
Time zones and daylight savingKeep UTC on the RTC and show local time: a zone offset, the US and EU daylight saving rules worked out for any year, and the hour that is skipped in spring and repeated in the fall.50 min
  1. Run it: UTC in, local time out
  2. The rules, worked out each year
  3. Spring forward
  4. Fall back
  5. Berlin
  6. Time zone rules for real products
NTP: time from the networkAn ESP32-C3 asks an NTP server for the time: Unix time in UTC, an offset added by the sketch, a resync interval, and the seconds of the minute running around a strip.50 min
  1. Run it: ask the network
  2. A simulated server
  3. NTP knows only UTC
  4. The seconds on a strip
  5. Another time zone
  6. NTP and an RTC together

Materials

  • Addressable RGB LEDs
  • Arduino Uno R3
  • ESP32-C3-DevKitM-1
  • Four-digit display, TM1637
  • Full-size breadboard
  • Half breadboard
  • Logic analyzer, four channels
  • Real-time clock, DS3231

Check for understanding

Each student finishes "NTP: time from the network" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "NTP and an RTC together".

Chapter 3. About 50 min

Alarms

Let the clock chip watch the time.

LessonTimeWhat students do
Alarms and schedulesTwo DS3231 hardware alarms, one at a set time and one every minute, flags that wait to be read, and a daily schedule table checked against the clock.50 min
  1. Run it: alarm set
  2. Two alarms, two flags
  3. Waking a sleeping board
  4. A schedule
  5. Add to the schedule
  6. Alarms and daylight saving

Materials

  • Arduino Uno R3
  • Four-digit display, TM1637
  • Half breadboard
  • Logic analyzer, four channels
  • Real-time clock, DS3231

Check for understanding

Each student finishes "Alarms and schedules" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Alarms and daylight saving".

Chapter 4. About 1 h 5 min

Boss project

A sunrise alarm clock that wakes you with light.

LessonTimeWhat students do
Boss: a sunrise alarm clockWake up to light: a DS3231 in UTC, New York time with daylight saving on a TM1637, a 16 LED ring that rises from deep red to warm white before the alarm, weekdays only, and a snooze.65 min
  1. Run it: before dawn
  2. The sun comes up
  3. Good morning
  4. Full sunlight
  5. Snooze
  6. Sleep in on weekends
  7. An earlier alarm
  8. Make it your clock

Materials

  • Addressable RGB LEDs
  • Arduino Uno R3
  • Four-digit display, TM1637
  • Half breadboard
  • Real-time clock, DS3231
  • Resistor

Check for understanding

Each student finishes "Boss: a sunrise alarm clock" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it your clock".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.