{"id":176,"date":"2016-01-02T18:33:56","date_gmt":"2016-01-02T18:33:56","guid":{"rendered":"http:\/\/www.adienicholls.co.uk\/blogs\/?p=176"},"modified":"2016-04-03T02:11:13","modified_gmt":"2016-04-03T01:11:13","slug":"raspberry-pi-real-time-clock-rtc","status":"publish","type":"post","link":"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-real-time-clock-rtc\/","title":{"rendered":"Raspberry Pi Real Time Clock (RTC)"},"content":{"rendered":"<h1>How to setup a Real Time Clock on a Raspberry Pi B v1.1<\/h1>\n<p>&nbsp;<\/p>\n<p>If you didn&#8217;t know, the Raspberry Pi doesn&#8217;t hold the time very well, this is because there is no on board Real Time Clock.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-211\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi--600x600.jpg\" alt=\"1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi-\" width=\"600\" height=\"600\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi--600x600.jpg 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi--150x150.jpg 150w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi--768x768.jpg 768w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/1pc-DS3231-Precision-RTC-Module-Memory-Module-for-Arduino-Raspberry-Pi-.jpg 1000w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>To keep the devices time accurate we need to add a module that holds the time whenever the RPi has no power running to it and then updates the RPi with the time saved on the battery powered module when the full power is restored and the system starts running again.<\/p>\n<p><!--more--><\/p>\n<h2>DS3231 vs DS1307<\/h2>\n<p>The Dallas DS3231 I2C RTC chip is allot more accurate than the commonly used DS1307 chip.\u00a0 It also comes on a pre made PCB with a battery and female header to attach to the Pi&#8217;s GPIO header<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-212\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/ds3231-raspberry-pi-board-real-time-clock-600x600.jpg\" alt=\"ds3231-raspberry-pi-board-real-time-clock\" width=\"600\" height=\"600\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/ds3231-raspberry-pi-board-real-time-clock-600x600.jpg 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/ds3231-raspberry-pi-board-real-time-clock-150x150.jpg 150w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/ds3231-raspberry-pi-board-real-time-clock-768x768.jpg 768w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/ds3231-raspberry-pi-board-real-time-clock.jpg 1000w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<h2>The Hardware<\/h2>\n<p style=\"text-align: left;\"><strong>Please be aware, the below code and options have been used to run on a <span style=\"text-decoration: underline;\"><a href=\"https:\/\/www.raspberrypi.org\/products\/raspberry-pi-2-model-b\/\" target=\"_blank\">Raspberry Pi B V1.1<\/a>.<\/span> Some of the commands\/Syntax maybe incorrect for other Raspberry Pi models.<\/strong><\/p>\n<p>For the build I purchased some DS3231 RTC chips prebuilt on mini PCB&#8217;s from <a href=\"http:\/\/www.ebay.co.uk\/itm\/2PCS-DIY-DS3231-Precision-RTC-Clock-Memory-Module-for-Arduino-Raspberry-Pi-\/141666242583\" target=\"_blank\">Ebay<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-203 aligncenter\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/DS3231-600x427.png\" alt=\"Ebay - DS3231\" width=\"600\" height=\"427\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/DS3231-600x427.png 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/DS3231-768x546.png 768w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/DS3231-1024x728.png 1024w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/DS3231.png 1205w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><strong><a href=\"http:\/\/www.raspberrypi-spy.co.uk\/2015\/05\/adding-a-ds3231-real-time-clock-to-the-raspberry-pi\/\" target=\"_blank\">Raspberry Pi Prerequisites:<\/a><\/strong><\/h2>\n<p>As always, ensure you have the latest OS by running the below update commands before installing new hardware:<\/p>\n<pre>sudo apt-get update<\/pre>\n<pre>sudo apt-get -y upgrade<\/pre>\n<h3><\/h3>\n<h3><a href=\"http:\/\/www.raspberrypi-spy.co.uk\/2014\/11\/enabling-the-i2c-interface-on-the-raspberry-pi\/\" target=\"_blank\">Enable the I2C interface on the Rpi:<\/a><\/h3>\n<pre>sudo raspi-config<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-204\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/raspi-config-600x255.png\" alt=\"raspi-config\" width=\"600\" height=\"255\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config-600x255.png 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config.png 641w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li>Select \u201c8 Advanced Options\u201d<\/li>\n<li>Select \u201cA7 I2C\u201d<\/li>\n<li>Select \u201cYes\u201d<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-205\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/Enable-I2C.png\" alt=\"Enable-I2C\" width=\"481\" height=\"323\" \/><\/p>\n<p>The RPi will confirm the request:<\/p>\n<ul>\n<li>Select &#8220;OK&#8221;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-206\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/I2C-reboot.png\" alt=\"I2C-reboot\" width=\"482\" height=\"321\" \/><\/p>\n<p>Load the I2C kernel module as default:<\/p>\n<ul>\n<li>Select &#8220;Yes&#8221;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-207\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/I2C-Default-Load.png\" alt=\"I2C-Default-Load\" width=\"482\" height=\"321\" \/><\/p>\n<p>The RPi will confirm the request:<\/p>\n<ul>\n<li>Select &#8220;OK&#8221;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-208\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/I2C-module-confirm.png\" alt=\"I2C-module-confirm\" width=\"482\" height=\"321\" \/><\/p>\n<p>The RPi will take you back to the main menu:<\/p>\n<ul>\n<li>Select &#8220;Finish&#8221;<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-209\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/raspi-config-finnish-600x256.png\" alt=\"raspi-config-finnish\" width=\"600\" height=\"256\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config-finnish-600x256.png 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config-finnish.png 641w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<h3><\/h3>\n<h3>Install I2C Utilities and Python Interface and update Modules file<\/h3>\n<p>To help debugging and allow the i2c interface to be used within Python we can install \u201cpython-smbus\u201d and \u201ci2c-tools\u201d :<\/p>\n<pre>sudo apt-get update\r\nsudo apt-get install -y python-smbus i2c-tools<\/pre>\n<p>Enter the following command to edit the Module file:<\/p>\n<pre>sudo nano \/etc\/modules<\/pre>\n<p>Ensure &#8216;i2c-dev&#8217; appears within the file<\/p>\n<pre># \/etc\/modules: kernel modules to load at boot time.\r\n#\r\n# This file contains the names of kernel modules that should be loaded\r\n# at boot time, one per line. Lines beginning with \"#\" are ignored.\r\n# Parameters can be specified after the module name.\r\n\r\nsnd-bcm2835\r\ni2c-dev<\/pre>\n<p>Once installed, reboot the RPi:<\/p>\n<pre>sudo reboot<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Connecting the PCB to the Raspberry Pi<\/strong><\/h2>\n<p>The DS3231 module is an I2C device that is attached to IO pins 1-3-5-7 on the Raspberry Pi B+<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-210\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/DS1231-Pin.png\" alt=\"DS1231 RPI B+ Pin\" width=\"588\" height=\"183\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>Testing the connection and software<\/h2>\n<p>Once you have run through the prerequisites and connected the DS3231 to the Pins 1-3-5-7\u00a0 run the following command to confirm the device is now connected:<\/p>\n<pre>lsmod | grep i2c_<\/pre>\n<p>the output will list connected I2C devices , the DS3231 will show as a &#8216;i2c_bcm2708&#8243;, should be something like:<\/p>\n<pre><span style=\"color: #999999;\">pi@rpi2 ~ $ lsmod | grep i2c_<\/span>\r\ni2c_bcm2708 5014 0<\/pre>\n<p>Another way to test that everything up to now is working, once the final reboot has taken place, enter the command:<\/p>\n<pre>sudo i2cdetect -y 1<\/pre>\n<p>The output shout be something like the below:<\/p>\n<pre><span style=\"color: #999999;\">pi@rpi2 ~ $ sudo i2cdetect -y 1<\/span>\r\n\u00a0\u00a0\u00a0\u00a0 0\u00a0 1\u00a0 2\u00a0 3\u00a0 4\u00a0 5\u00a0 6\u00a0 7\u00a0 8\u00a0 9\u00a0 a\u00a0 b\u00a0 c\u00a0 d\u00a0 e\u00a0 f\r\n00:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -- -- -- -- -- -- -- -- -- -- -- -- --\r\n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --\r\n70: -- -- -- -- -- -- -- --<\/pre>\n<h2><\/h2>\n<h2>Configuring the RPi to get time from the DS3231<\/h2>\n<p>So, with the hardware connected, prerequisites carried out and its the test shows a device connected we can do set the RPi to take the time from thee DS3231 hardware clock on boot.<\/p>\n<p>To do this we edit the rc.local file:<\/p>\n<pre>sudo nano \/etc\/rc.local<\/pre>\n<p>Add the following two lines before the exit 0 line :<\/p>\n<pre>echo ds1307 0x68 &gt; \/sys\/class\/i2c-adapter\/i2c-1\/new_device\r\nhwclock -s<\/pre>\n<p>The outcome should be like this:<\/p>\n<pre><span style=\"color: #999999;\">pi@rpi2 ~ $ sudo nano \/etc\/rc.local<\/span>\r\n<span style=\"color: #999999;\">\u00a0 GNU nano 2.2.6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 File: \/etc\/rc.local\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Modified<\/span>\r\n\r\n<span style=\"color: #999999;\">#<\/span>\r\n<span style=\"color: #999999;\"># By default this script does nothing.<\/span>\r\n\r\n<span style=\"color: #999999;\"># Print the IP address<\/span>\r\n<span style=\"color: #999999;\">_IP=$(hostname -I) || true<\/span>\r\n<span style=\"color: #999999;\">if [ \"$_IP\" ]; then<\/span>\r\n<span style=\"color: #999999;\">\u00a0 printf \"My IP address is %s\\n\" \"$_IP\"<\/span>\r\n<span style=\"color: #999999;\">fi<\/span>\r\n\r\necho ds1307 0x68 &gt; \/sys\/class\/i2c-adapter\/i2c-1\/new_device\r\nhwclock -s\r\n\r\n<span style=\"color: #999999;\">exit 0<\/span><\/pre>\n<p>To exit and save the document use the key combination:<\/p>\n<ul>\n<li>CTRL X<\/li>\n<li>Y for Yes<\/li>\n<li>Enter<\/li>\n<\/ul>\n<p>Reboot the RPi:<\/p>\n<pre>sudo reboot<\/pre>\n<p>If you were to re run the i2cdetect -y 1 command again you would get the following output, this is expected:<\/p>\n<pre>pi@rpi2 ~ $ sudo i2cdetect -y 1\r\n\u00a0\u00a0\u00a0\u00a0 0\u00a0 1\u00a0 2\u00a0 3\u00a0 4\u00a0 5\u00a0 6\u00a0 7\u00a0 8\u00a0 9\u00a0 a\u00a0 b\u00a0 c\u00a0 d\u00a0 e\u00a0 f\r\n00:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -- -- -- -- -- -- -- -- -- -- -- -- --\r\n10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\r\n60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --\r\n70: -- -- -- -- -- -- -- --<\/pre>\n<p>&nbsp;<\/p>\n<h2>Time Zones and Daylight Saving Time<\/h2>\n<p>To check or update your time preferences follow the below commands:<\/p>\n<p>Load the Rasp-Config:<\/p>\n<pre>sudo raspi-config<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-204\" src=\"http:\/\/www.adienicholls.co.uk\/blogs\/wp-content\/uploads\/2015\/12\/raspi-config-600x255.png\" alt=\"raspi-config\" width=\"600\" height=\"255\" srcset=\"http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config-600x255.png 600w, http:\/\/www.adienicholls.co.uk\/blog\/wp-content\/uploads\/2015\/12\/raspi-config.png 641w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li>Select \u201c4 Internationalisation Options \u201d<\/li>\n<li>Select \u201cChange Timezone\u201d<\/li>\n<li>Select \u201cYes\u201d<\/li>\n<li>Run through the wizard and select your own preferences<\/li>\n<li>Select Finish from the main config menu when done<\/li>\n<\/ul>\n<h2><\/h2>\n<h2>Reading The Date And Time<\/h2>\n<p>To read the currently set time and date from the RPi use the following command:<\/p>\n<pre>date<\/pre>\n<p>The output will be like below:<\/p>\n<pre><span style=\"color: #999999;\">pi@rpi2 ~ $ date<\/span>\r\nSat Jan\u00a0 2 17:57:25 GMT 2016<\/pre>\n<p>To set this to anything other, user the following commands:<\/p>\n<pre>sudo date -s \"2 Jan 2016 18:10:00\"<\/pre>\n<p>To commit this newly set time to the new RTC hardware clock use the following command:<\/p>\n<pre>sudo hwclock -w<\/pre>\n<p>To read the newly set hardware clock time and make sure its the same as the internal clock use the following commands to show both times at once:<\/p>\n<pre>date; sudo hwclock -r<\/pre>\n<p>The output will be as below (internal clock on the top and RTC on the bottom)<\/p>\n<pre><span style=\"color: #999999;\">pi@rpi2 ~ $ date; sudo hwclock -r<\/span>\r\nSat Jan\u00a0 2 18:14:02 GMT 2016\r\nSat 02 Jan 2016 18:14:02 GMT\u00a0 -0.126111 seconds<\/pre>\n<h2><\/h2>\n<h2>The Final Test<\/h2>\n<p>The only real way to test if the whole time keeping thing has worked is to unplug the RPi from the power and network, then plug the RPi upto a keyboard, monitor and power to log in locally and see what the time says, all being well the time will be accurate as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to setup a Real Time Clock on a Raspberry Pi B v1.1 &nbsp; If you didn&#8217;t know, the Raspberry Pi doesn&#8217;t hold the time very well, this is because there is no on board Real Time Clock. To keep the devices time accurate we need to add a module that holds the time whenever &hellip; <a href=\"http:\/\/www.adienicholls.co.uk\/blog\/raspberry-pi-real-time-clock-rtc\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi Real Time Clock (RTC)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":212,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,88],"tags":[],"_links":{"self":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/176"}],"collection":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":9,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":226,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/posts\/176\/revisions\/226"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/media\/212"}],"wp:attachment":[{"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.adienicholls.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}