{"id":251,"date":"2018-11-30T20:01:53","date_gmt":"2018-12-01T04:01:53","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=251"},"modified":"2019-06-16T17:30:33","modified_gmt":"2019-06-17T00:30:33","slug":"ds1302-real-time-clock-module","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/raspberrypi3bsensors\/ds1302-real-time-clock-module\/","title":{"rendered":"DS1302 Real Time Clock Module"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Using sourceperl rpi.rtc library to read time<\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">Needs corrections<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Python3 and download the github repository<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo python3 setup.py install\ngit clone https:\/\/github.com\/sourceperl\/rpi.rtc.git<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Wire the rtc to the raspberry pi using the default mapping.<\/h3>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>Chip<\/th><th>Rpi pin<\/th><\/tr><\/thead><tbody><tr><td>VCC<\/td><td>3.3v pin<\/td><\/tr><tr><td>GND<\/td><td>GND pin<\/td><\/tr><tr><td>CLK<\/td><td>pin 11<\/td><\/tr><tr><td>DATA<\/td><td>pin 13<\/td><\/tr><tr><td>CE (RST)<\/td><td>pin 15<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>*You can modify the the pin connections on the pyRPiRTC.py file by changing the pin =#<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def __init__(self, clk_pin=11, data_pin=13, ce_pin=15)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a file to&nbsp;<a id=\"user-content-read-rtc-chip-date-and-time\" class=\"anchor\" href=\"https:\/\/github.com\/sourceperl\/rpi.rtc#read-rtc-chip-date-and-time\" aria-hidden=\"true\"><\/a>Read RTC chip date and time .<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano DS1302_Time.py\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-rpi-date-and-time-utc-to-rtc-chip\" class=\"anchor\" href=\"https:\/\/github.com\/sourceperl\/rpi.rtc#rpi-date-and-time-utc-to-rtc-chip\" aria-hidden=\"true\"><\/a>Use functions from&nbsp;pyRPiRTC.py to read and set time<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env python3&nbsp;\n# read date and time from RTC chip, return ISO 8601 UTC string\n# assume DS1302 contain UTC time and not local\nimport sys\nimport pyRPiRTC\nrtc = pyRPiRTC.DS1302(clk_pin=11, data_pin=13, ce_pin=15)\ntry:\n    # read date and time from RTC chip\n    dt = rtc.read_datetime()\n    print(dt.strftime('%Y-%m-%dT%H:%M:%SZ'))\nexcept ValueError:\n    sys.exit('error with RTC chip, check wiring')\nfinally:\n   # clean close\n    rtc.close()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a id=\"user-content-update-rpi-system-time-from-rtc-chip\" class=\"anchor\" href=\"https:\/\/github.com\/sourceperl\/rpi.rtc#update-rpi-system-time-from-rtc-chip\" aria-hidden=\"true\"><\/a>Update Rpi system time from RTC chip<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Typically call at RPi startup<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><code>sudo date -s `.\/ds1302_get_utc`<br>\n<\/code><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><code><\/code><a id=\"user-content-one-line-to-check-rtc-chip-time-vs-system-time\" class=\"anchor\" href=\"https:\/\/github.com\/sourceperl\/rpi.rtc#one-line-to-check-rtc-chip-time-vs-system-time\" aria-hidden=\"true\"><\/a>One line to check RTC chip time vs system time<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Since RTC store only second and not millisecond a 1s delta can occur (or more after a few days)<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><code># drift in second<br>\necho $(($(date -u -d`ds1302_get_utc` +%s) - $(date -u +%s)))<br>\n# human readable<br>\necho \"RTC `ds1302_get_utc`\"; echo \"SYS `date --utc +%FT%TZ`\";<\/code><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>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. Chip Rpi pin VCC 3.3v pin GND GND pin CLK pin 11 DATA pin 13 CE (RST) pin 15 *You can &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/raspberrypi3bsensors\/ds1302-real-time-clock-module\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;DS1302 Real Time Clock Module&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":160,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-251","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/251\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/160"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}