Page 1 of 1

Digital Clock drift

Posted: Tue Jul 05, 2022 11:13 am
by matt42
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)


Re: Digital Clock drift

Posted: Tue Jul 05, 2022 12:35 pm
by tepalia02
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?

Re: Digital Clock drift

Posted: Tue Jul 05, 2022 1:09 pm
by scruss
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

Re: Digital Clock drift

Posted: Wed Jul 06, 2022 12:23 am
by KJM
You should see what they get up to in deepsleep. I've seen free running neon tube oscillators keep better time.

Re: Digital Clock drift

Posted: Wed Jul 06, 2022 8:40 am
by pythoncoder
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.

Re: Digital Clock drift

Posted: Wed Jul 06, 2022 12:57 pm
by matt42
Thanks all for the suggestions and comments, it helps. I ordered a DS3231 to play with so will see how that goes.