DS1302 Real Time Clock Module

Using sourceperl rpi.rtc library to read time

Needs corrections

Install Python3 and download the github repository

sudo python3 setup.py install
git clone https://github.com/sourceperl/rpi.rtc.git

Wire the rtc to the raspberry pi using the default mapping.

ChipRpi pin
VCC3.3v pin
GNDGND pin
CLKpin 11
DATApin 13
CE (RST)pin 15

*You can modify the the pin connections on the pyRPiRTC.py file by changing the pin =#

def __init__(self, clk_pin=11, data_pin=13, ce_pin=15)

Create a file to Read RTC chip date and time .

sudo nano DS1302_Time.py

Use functions from pyRPiRTC.py to read and set time

#!/usr/bin/env python3 
# read date and time from RTC chip, return ISO 8601 UTC string
# assume DS1302 contain UTC time and not local
import sys
import pyRPiRTC
rtc = pyRPiRTC.DS1302(clk_pin=11, data_pin=13, ce_pin=15)
try:
    # read date and time from RTC chip
    dt = rtc.read_datetime()
    print(dt.strftime('%Y-%m-%dT%H:%M:%SZ'))
except ValueError:
    sys.exit('error with RTC chip, check wiring')
finally:
   # clean close
    rtc.close()

Update Rpi system time from RTC chip

Typically call at RPi startup

sudo date -s `./ds1302_get_utc`

One line to check RTC chip time vs system time

Since RTC store only second and not millisecond a 1s delta can occur (or more after a few days)

# drift in second
echo $(($(date -u -d`ds1302_get_utc` +%s) - $(date -u +%s)))
# human readable
echo "RTC `ds1302_get_utc`"; echo "SYS `date --utc +%FT%TZ`";