{"id":48,"date":"2012-11-06T11:09:27","date_gmt":"2012-11-06T11:09:27","guid":{"rendered":"http:\/\/ermine-electronics.co.uk\/?p=48"},"modified":"2019-11-27T09:37:16","modified_gmt":"2019-11-27T09:37:16","slug":"setting-arduino-rtc-openkontrolgateway","status":"publish","type":"post","link":"https:\/\/www.richardmudhar.com\/blog\/2012\/11\/setting-arduino-rtc-openkontrolgateway\/","title":{"rendered":"Setting the Real Time clock Arduino based OpenKontrolGateway"},"content":{"rendered":"<p>The OpenKontrolGateway a board like an Arduino Uno, but with a SD card interface, three RF board interfaces, an Ethernet board module slot and a DS1307 Real Time Clock with battery backup. It&#8217;s a nice collection of all the stuff you need to make a datalogger. If you want to retrofit a DS1307 RTC to an Arduino project <a href=\"https:\/\/www.seeedstudio.com\/Grove-RTC.html\">Seedstudio&#8217;s Grove RTC board<\/a> features the SMD version of of the chip and saves you the tiresome surface mount wrangling.<\/p>\n<figure id=\"attachment_113\" aria-describedby=\"caption-attachment-113\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-113 size-medium\" src=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116-300x224.jpg?resize=300%2C224\" alt=\"Ciseco OKG\" width=\"300\" height=\"224\" srcset=\"https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116.jpg?resize=300%2C224&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116.jpg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116.jpg?resize=624%2C468&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.richardmudhar.com\/blog\/wp-content\/uploads\/2013\/04\/DSCN2116.jpg?w=1680&amp;ssl=1 1680w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-113\" class=\"wp-caption-text\">Ciseco OKG<\/figcaption><\/figure>\n<p>The OKG bundle at \u00a325 gives you everything needed to box it up but I added the RTC kit and SRAM which added \u00a312 to the cost, since a data logger without an RTC is always going to be a pain, where you have to connect up a PC to set it. That&#8217;s OK at home but no fun at the farm.<\/p>\n<p>The RTC gave me a hard time right off the bat. There are a few gotchas.<\/p>\n<ul>\n<li>You have to set the damn thing,<\/li>\n<li>you need to do that to even see it started, since it can need initialisation from the SPI bus before the oscillator will start<\/li>\n<li>You need a 2032 coin cell present for it to be able to work too, so no testing that without and thinking you&#8217;ll fit that later \ud83d\ude09<\/li>\n<\/ul>\n<p>So if you &#8216;scope the crystal and think\u00a0 &#8216;ah, that&#8217;s why it isn&#8217;t working&#8217; then not so fast, you may need to initialise and set it before you see any action there. I found all of this out the hard way. This is what I used to set it, which I largely pinched from <a href=\"http:\/\/www.ladyada.net\/learn\/breakoutplus\/ds1307rtc.html\">LadyAda&#8217;s tutorial<\/a>. One of the nice things about that is it lets you set the clock to the time the sketch was compiled, which save a whole load of faff. What you must not do then is to press reset, or switch off and on again, else it will do that all over again, setting the RTC back to the time it was compiled. You must comment out the line<\/p>\n<pre>\u00a0\u00a0\u00a0 \/\/ RTC.adjust(DateTime(__DATE__, __TIME__));<\/pre>\n<p>and re-upload it. This listing has that line commented out, so you must uncomment it to set the clock, then recomment it.<\/p>\n<p><!--more--><\/p>\n<pre>\/\/ Date and time functions using a DS1307 RTC connected via I2C and Wire lib\r\n\r\n#include &lt;Wire.h&gt;\r\n#include \"RTClib.h\"\r\n\r\nRTC_DS1307 RTC;\r\n\r\nvoid setup () {\r\n\u00a0\u00a0\u00a0 Serial.begin(9600);\r\n\u00a0\u00a0\u00a0 Wire.begin();\r\n\u00a0\u00a0\u00a0 RTC.begin();\r\n\u00a0\u00a0\u00a0 \/\/RTC.adjust(DateTime(__DATE__, __TIME__));\r\n\r\n\u00a0 if (! RTC.isrunning()) {\r\n\u00a0\u00a0\u00a0 Serial.println(\"RTC is NOT running!\");\r\n\u00a0\u00a0\u00a0 \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\r\n\u00a0\u00a0\u00a0 \/\/ RTC.adjust(DateTime(__DATE__, __TIME__));\r\n\u00a0 }\r\n\r\n}\r\n\r\nvoid loop () {\r\n\u00a0\u00a0\u00a0 DateTime now = RTC.now();\r\n\r\n\u00a0\u00a0\u00a0 Serial.print(now.year(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print('\/');\r\n\u00a0\u00a0\u00a0 Serial.print(now.month(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print('\/');\r\n\u00a0\u00a0\u00a0 Serial.print(now.day(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(' ');\r\n\u00a0\u00a0\u00a0 Serial.print(now.hour(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(':');\r\n\u00a0\u00a0\u00a0 Serial.print(now.minute(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(':');\r\n\u00a0\u00a0\u00a0 Serial.print(now.second(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.println();\r\n\r\n\u00a0\u00a0\u00a0 Serial.print(\" since 1970 = \");\r\n\u00a0\u00a0\u00a0 Serial.print(now.unixtime());\r\n\u00a0\u00a0\u00a0 Serial.print(\"s = \");\r\n\u00a0\u00a0\u00a0 Serial.print(now.unixtime() \/ 86400L);\r\n\u00a0\u00a0\u00a0 Serial.println(\"d\");\r\n\r\n\u00a0\u00a0\u00a0 \/\/ calculate a date which is 7 days and 30 seconds into the future\r\n\u00a0\u00a0\u00a0 DateTime future (now.unixtime() + 7 * 86400L + 30);\r\n\r\n\u00a0\u00a0\u00a0 Serial.print(\" now + 7d + 30s: \");\r\n\u00a0\u00a0\u00a0 Serial.print(future.year(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print('\/');\r\n\u00a0\u00a0\u00a0 Serial.print(future.month(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print('\/');\r\n\u00a0\u00a0\u00a0 Serial.print(future.day(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(' ');\r\n\u00a0\u00a0\u00a0 Serial.print(future.hour(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(':');\r\n\u00a0\u00a0\u00a0 Serial.print(future.minute(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.print(':');\r\n\u00a0\u00a0\u00a0 Serial.print(future.second(), DEC);\r\n\u00a0\u00a0\u00a0 Serial.println();\r\n\r\n\u00a0\u00a0\u00a0 Serial.println();\r\n\u00a0\u00a0\u00a0 delay(3000);\r\n}<\/pre>\n<p>Having done that and got the RTC started, the\u00a0 next step is to log some data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The OpenKontrolGateway a board like an Arduino Uno, but with a SD card interface, three RF board interfaces, an Ethernet board module slot and a DS1307 Real Time Clock with battery backup. It&#8217;s a nice collection of all the stuff you need to make a datalogger. If you want to retrofit a DS1307 RTC to &hellip; <a href=\"https:\/\/www.richardmudhar.com\/blog\/2012\/11\/setting-arduino-rtc-openkontrolgateway\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting the Real Time clock Arduino based OpenKontrolGateway&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[27,43,44,49],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-sensors","tag-ds1307","tag-okg","tag-openkontrolgateway","tag-rtc"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5aOO7-M","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":3,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":3787,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions\/3787"}],"wp:attachment":[{"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richardmudhar.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}