Digital Clock drift

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
matt42
Posts: 7
Joined: Sat Apr 25, 2020 4:10 am

Digital Clock drift

Post by matt42 » Tue Jul 05, 2022 11:13 am

I have created a simple digital clock with a D1 mini and 2 x 7 segment LED displays which shows 2 different time zones and works fine.

After noticing the D1 has a lot of time drift over 24 hours (10 or more mins), I changed my code to the below, checking NTP once every hour which has solved the drift problem.

My question is more of a check on whether I am solving this problem in (nearly) the most efficient way or is there other methods of keeping time with less drift? Is this level of drift pretty standard for a D1?
thanks for any comments / advice

Code: Select all

import tm1637
from machine import Pin
nsw = tm1637.TM1637(clk=Pin(12), dio=Pin(14))
qld = tm1637.TM1637(clk=Pin(04), dio=Pin(00))
import ntptime
import time
import utime

ntptime.settime()
nsw.brightness(0)
qld.brightness(0)

ntptime.host = "time.google.com"
x = 0
while True:
    nswclock_hour = utime.localtime(utime.mktime(utime.localtime()) + 11*3600)[3]
    nswclock_min = utime.localtime(utime.mktime(utime.localtime()) + 11*3600)[4]
    
    qldclock_hour = utime.localtime(utime.mktime(utime.localtime()) + 10*3600)[3]
    qldclock_min = utime.localtime(utime.mktime(utime.localtime()) + 10*3600)[4]
    
    if nswclock_min == 01 and x == 0:
        ntptime.settime()
        x += 1
        print('Time corrected at ', nswclock_hour)
    if nswclock_min == 02:
        x = 0
        
    nsw.numbers(nswclock_hour, nswclock_min)
    qld.numbers(qldclock_hour, qldclock_min)


tepalia02
Posts: 99
Joined: Mon Mar 21, 2022 5:13 am

Re: Digital Clock drift

Post by tepalia02 » Tue Jul 05, 2022 12:35 pm

Is the problem only occurring when you're using this particular board? Once I made an OLED clock using ESP8266 and Arduino IDE. I integrated the ESP8266 with nist.gov. The clock ran accurately. D1 mini has less memory than the generic ESP8266, right? Can it be a possible problem?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Digital Clock drift

Post by scruss » Tue Jul 05, 2022 1:09 pm

matt42 wrote:
Tue Jul 05, 2022 11:13 am
Is this level of drift pretty standard for a D1?
Absolutely: 10 minutes every day is pretty decent for an ESP8266's RTC. They really shouldn't be called "real time" anything, as they can be very unstable

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: Digital Clock drift

Post by KJM » Wed Jul 06, 2022 12:23 am

You should see what they get up to in deepsleep. I've seen free running neon tube oscillators keep better time.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Digital Clock drift

Post by pythoncoder » Wed Jul 06, 2022 8:40 am

They are notorious. Options (other than sucking it up) are to use such as an ESP32 or to add a DS3231 precision RTC to your ESP8266. The DS3231 is incredibly cheap and accurate.
Peter Hinch
Index to my micropython libraries.

matt42
Posts: 7
Joined: Sat Apr 25, 2020 4:10 am

Re: Digital Clock drift

Post by matt42 » Wed Jul 06, 2022 12:57 pm

Thanks all for the suggestions and comments, it helps. I ordered a DS3231 to play with so will see how that goes.

Post Reply