Sohil.Mehta
Posts: 57
Joined: Thu Feb 01, 2018 6:45 am

RTC time is drifting within one day

Wed Dec 19, 2018 8:58 am

I have built my own Compute Module IO board based on the schematics of CMIO Rev3.0 available online, and added some extra things over it like GPS/GNSS Module, IMU, RTC with a coin cell, etc. The processor that I am using is Compute Module 3 Lite. Since GPS is my only source of getting the actual time, I found this python code online that reads the gps time and writes it to system time:

Code: Select all

import os
import sys
import time
from gps import *

print 'Set System Clock to GPS UTC time'

try:
  gpsd = gps(mode=WATCH_ENABLE)
except:
  print 'ERROR: No GPS Present, time not set!!'
  sys.exit()

while True:
  #wait until the next GPSD time tick
  gpsd.next()
  if gpsd.utc != None and gpsd.utc != '':
    #gpsd.utc is formatted like"2015-04-01T17:32:04.000Z"
    #convert it to a form the date -u command will accept: "20140401 17:32:04"
    #use python slice notation [start:end] (where end desired end char + 1)
    #   gpsd.utc[0:4] is "2015"
    #   gpsd.utc[5:7] is "04"
    #   gpsd.utc[8:10] is "01"
    gpsutc = gpsd.utc[0:4] + gpsd.utc[5:7] + gpsd.utc[8:10] + ' ' + gpsd.utc[11:19]
    os.system('sudo date -u --set="%s"' % gpsutc)
    sys.exit()

The RTC I am using is DS1307 and it is backed by a coin cell. The RTC is on I2C-0:

Code: Select all

i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --         
Address is 0x68.


Contents of /boot/config.txt (last few lines):

Code: Select all

# Enable audio (loads snd_bcm2835)
dtparam=audio=on
dtparam=i2c_vc=on
dtparam=i2c1=on

dtoverlay=uart0,txd0_pin=36,rxd0_pin=37,pin_func=6 #alt2
dtoverlay=uart1,txd1_pin=14,rxd1_pin=15
dtoverlay=i2c0-rtc,ds1307
core_freq=250
enable_uart=1



Contents of /etc/modules:

Code: Select all

i2c-bcm2708
i2c-dev
rtc-ds1307

After I got the fix and wrote the correct time to the system clock, this is what I did to disable the fake hardware clock:

Code: Select all

sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclock

And then, ran sudo nano /lib/udev/hwclock-set and comment out these three lines:

Code: Select all

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi

And then, I did:

Code: Select all

sudo modprobe rtc-ds1307
sudo hwclock -w

to write the correct time to the RTC. Until yesterday, it was all fine. The actual time and the RTC and system time were correct as long as the GPS was on and the fix was present. Today, I disconnected the GPS to see if my RTC and system time is still correct. Turns out, there is a drift of 6 seconds already in a single day compared to the actual time. What should I do to make sure that the RTC and system time is always correct and there is no drift?

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RTC time is drifting within one day

Wed Dec 19, 2018 6:11 pm

Is your RTC battery backed? Is the battery dead?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

drgeoff
Posts: 9729
Joined: Wed Jan 25, 2012 6:39 pm

Re: RTC time is drifting within one day

Fri Dec 21, 2018 11:18 pm

Sohil.Mehta wrote:
Wed Dec 19, 2018 8:58 am
Today, I disconnected the GPS to see if my RTC and system time is still correct. Turns out, there is a drift of 6 seconds already in a single day compared to the actual time. What should I do to make sure that the RTC and system time is always correct and there is no drift?
I'm not 100% understand about what you are saying. If you mean your free-running RTC is off by 6 seconds in 24 hours, that is an error of 70 parts per million. That's not out of the question if you have not calibrated the DS1307 plus crystal combination.

Typically you require a trimmer capacitor to adjust the precise frequency of a crystal oscillator.

You can also periodically (eg once per hour) set the RTC time to the GPS time.

rzusman
Posts: 346
Joined: Fri Jan 01, 2016 10:27 pm

Re: RTC time is drifting within one day

Sun Dec 23, 2018 2:20 am

Frankly, the DS1307 accuracy stinks.

Spend the extra money and get the DS3231, which is temperature compensated, and is guaranteed accurate to a a minute or so/year.

http://www.reuk.co.uk/wordpress/accurat ... to-ds1307/

Return to “Compute Module”