Skip to content

Full Running Clock Using the TM1637

I'll create a detailed example of a running clock using the TM1637 display. This builds on some of the concepts shown in the clock-driver.py file you shared, but simplified for the TM1637.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tm1637
from machine import Pin, RTC
from utime import sleep, localtime

# Initialize the display
tm = tm1637.TM1637(clk=Pin(0), dio=Pin(1))

# Initialize the RTC (Real Time Clock)
rtc = RTC()

def set_initial_time():
    """
    Set an initial time if RTC isn't configured.
    In a real project, you'd sync this with a time server or user input.
    """
    # Format: (year, month, day, weekday, hours, minutes, seconds, subseconds)
    rtc.datetime((2024, 12, 28, 5, 12, 0, 0, 0))

def format_time(hours, minutes):
    """
    Convert 24-hour time to 12-hour format
    Returns hours and whether it's PM
    """
    is_pm = hours >= 12
    if hours > 12:
        hours -= 12
    elif hours == 0:
        hours = 12
    return hours, is_pm

def run_clock():
    # Set initial brightness (0-7)
    tm.brightness(2)

    # Track colon state for blinking
    colon_on = True

    while True:
        # Get current time from RTC
        year, month, day, weekday, hours, minutes, seconds, _ = rtc.datetime()

        # Convert to 12-hour format
        display_hours, is_pm = format_time(hours, minutes)

        # Toggle colon every second
        if seconds != prev_seconds:
            colon_on = not colon_on
            prev_seconds = seconds

        # Display the time
        tm.numbers(display_hours, minutes, colon=colon_on)

        # Brief pause to prevent display flicker
        sleep(0.1)

if __name__ == '__main__':
    set_initial_time()  # Only needed if RTC isn't set
    print("Starting clock...")
    run_clock()

Let's break down the key concepts students need to understand:

  1. Real-Time Clock (RTC)
  2. The RTC is a hardware component that keeps track of time
  3. It continues running even when the microcontroller is reset
  4. Time is stored as a tuple: (year, month, day, weekday, hours, minutes, seconds, subseconds)
  5. Students should understand why RTCs are important for accurate timekeeping

  6. Time Formats

  7. 24-hour vs 12-hour time conversion
  8. Why we need to handle special cases (midnight = 0 hours → 12, noon = 12 stays 12)
  9. The concept of AM/PM

  10. Display Multiplexing

  11. How LED displays show multiple digits (though the TM1637 handles this internally)
  12. Why we need a brief sleep to prevent display flicker
  13. How brightness control works with PWM (Pulse Width Modulation)

  14. State Management

  15. Tracking the colon state for blinking
  16. Maintaining previous second value to detect changes
  17. Why we use global variables in this context

  18. Program Structure

  19. Main loop design
  20. Function organization
  21. Error handling (not shown but important in real applications)

Common challenges students might encounter:

  1. Time Drift
  2. The RTC might drift slightly over time
  3. In real applications, you'd want to sync with an NTP server periodically

  4. Power Management

  5. Display brightness affects power consumption
  6. Consider dimming display in low light conditions
  7. Think about battery life in portable applications

  8. User Interface

  9. Adding buttons to set the time
  10. Handling time zone changes
  11. Adding features like alarms or timers

Extensions students could try:

  1. Add a temperature display that alternates with the time
  2. Implement automatic brightness control using a light sensor
  3. Add alarm functionality with a buzzer
  4. Create a menu system for setting the time
  5. Add a battery backup system

Would you like me to elaborate on any of these concepts or provide example code for any of the extensions?

For reference, this code builds on the concepts shown in your clock-driver.py file, but simplifies the implementation thanks to the TM1637's built-in controller. The course-description.md suggests this would fit well in the early stages of your course, particularly during the "Understanding" phase where students explore how timing functions and hardware work together.