{"id":417,"date":"2011-09-29T19:40:00","date_gmt":"2011-09-29T10:10:00","guid":{"rendered":"http:\/\/www.l8ter.com\/?p=417"},"modified":"2011-11-09T22:37:43","modified_gmt":"2011-11-09T13:07:43","slug":"arduino-love-electronics-rtc-%e2%80%93-ds3231-wiring-example-and-tutorial","status":"publish","type":"post","link":"http:\/\/www.l8ter.com\/?p=417","title":{"rendered":"Arduino Love electronics RTC \u2013 DS3231 wiring example and tutorial."},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"http:\/\/www.l8ter.com\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>As you may be aware from my previous post I recently was in the market for a more accurate replacement for the DS1307 RTC clock. I tried with a DS3234 but its SPI interface made it unsuitable in my project (running out of pins). All i was after was a simple breakout board for the DS3231 I found the <a href=\"http:\/\/www.adafruit.com\/products\/255\">chronodot<\/a> but its presoldered pins, lack of stock in\/transport costs to Australia (As per normal) and their overly complex setup (for my use anyway). I came across a UK based website called<a href=\"http:\/\/loveelectronics.co.uk\/\"> loveelectronics.co.uk<\/a>\u00a0. They have a perfect little <a href=\"https:\/\/www.loveelectronics.co.uk\/products\/137\/ds3231-real-time-clock-module\">DS3231 breakout <\/a>board.<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/ds3231-breakout-top-460x460.png\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-418\" title=\"ds3231-breakout-top-460x460\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/ds3231-breakout-top-460x460-288x288.png\" alt=\"\" width=\"288\" height=\"288\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/ds3231-breakout-top-460x460-288x288.png 288w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/ds3231-breakout-top-460x460-188x188.png 188w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/ds3231-breakout-top-460x460.png 460w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p>The bonus of this DS3231\u00a0board is its improved accuracy over the DS1307 but still using the I2C interface that the DS1307 uses. In short any code that works with DS1307 should work with DS3231 without any changes. One downside of this kit is that it\u00a0doesn&#8217;t\u00a0come with a coin battery but this is a minor issue. <strong>ENSURE YOU HAVE A BATTERY IN THE HOLDER (EVEN IF FLAT), OTHERWISE YOU GET STRANGE RESULTS.<\/strong><\/p>\n<p>So with that out of the way let me give you a quick tutorial on getting this chip running, first the cabling.<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz.png\"><img loading=\"lazy\" class=\"aligncenter size-large wp-image-419\" title=\"DS3231fritz\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz-489x494.png\" alt=\"\" width=\"489\" height=\"494\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz-489x494.png 489w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz-285x288.png 285w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz-594x600.png 594w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231fritz.png 785w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/a>This is the exact same setup as the DS1307, a small problem with the DS3231 board is its hard to see which contact is SDA, but I did this drawing to make it easier for you (and me to look back at). So once you have wired it up(and double checked ground and 5V {trust me do it}). So lets try the basic 1307 reading by the wire library code:<\/p>\n<p><pre class=\"brush: php\">#include &quot;Wire.h&quot;\r\n#define DS1307_ADDRESS 0x68\r\n\r\nvoid setup(){\r\n  Wire.begin();\r\n  Serial.begin(9600);\r\n}\r\n\r\nvoid loop(){\r\n  printDate();\r\n  delay(1000);\r\n}\r\n\r\nbyte bcdToDec(byte val)  {\r\n\/\/ Convert binary coded decimal to normal decimal numbers\r\n  return ( (val\/16*10) + (val%16) );\r\n}\r\n\r\nvoid printDate(){\r\n\r\n  \/\/ Reset the register pointer\r\n  Wire.beginTransmission(DS1307_ADDRESS);\r\n  Wire.send(0);\r\n  Wire.endTransmission();\r\n\r\n  Wire.requestFrom(DS1307_ADDRESS, 7);\r\n\r\n  int second = bcdToDec(Wire.receive());\r\n  int minute = bcdToDec(Wire.receive());\r\n  int hour = bcdToDec(Wire.receive() &amp; 0b111111); \/\/24 hour time\r\n  int weekDay = bcdToDec(Wire.receive()); \/\/0-6 -&gt; sunday - Saturday\r\n  int monthDay = bcdToDec(Wire.receive());\r\n  int month = bcdToDec(Wire.receive());\r\n  int year = bcdToDec(Wire.receive());\r\n\r\n  \/\/print the date EG   3\/1\/11 23:59:59\r\n  Serial.print(month);\r\n  Serial.print(&quot;\/&quot;);\r\n  Serial.print(monthDay);\r\n  Serial.print(&quot;\/&quot;);\r\n  Serial.print(year);\r\n  Serial.print(&quot; &quot;);\r\n  Serial.print(hour);\r\n  Serial.print(&quot;:&quot;);\r\n  Serial.print(minute);\r\n  Serial.print(&quot;:&quot;);\r\n  Serial.println(second);\r\n\r\n}<\/pre><\/p>\n<p>Once uploaded keep the Arduino program open, and go to Tools -&gt; Serial Monitor ensure the baud is at 9600. You should see something similar to:<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231basicout.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-422\" title=\"DS3231basicout\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231basicout.png\" alt=\"\" width=\"297\" height=\"381\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231basicout.png 297w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231basicout-224x288.png 224w\" sizes=\"(max-width: 297px) 100vw, 297px\" \/><\/a>So before I ran this code I removed power and battery so I reset the RTC to 0:0:0 0\/0\/00. So there we go its now ticking away. This is a great basic method but I wanted something a bit nicer and easier to work with.<\/p>\n<p>As my previous post I am once again going to use the RTC library changed by manicbubg forked from adafriut. Again this is a little bit further on than the original RTC library sent out in 2010. As you may have seen before this one library allows you to interface with a DS3234 and a DS1307, as i said before DS3231 works exactly the same as the DS1307 so its perfect for us.\u00a0You can<a href=\"https:\/\/github.com\/maniacbug\/RTClib\/archives\/master\">\u00a0jump onto the github service and download a .zip of the current release<\/a>\u00a0or you can use the copy that I&#8217;m writing this tutorial with (so it should work)\u00a0<a href=\"http:\/\/www.l8ter.com\/arduino\/maniacbug-RTClib.zip\">here<\/a>.\u00a0<strong>Extract<\/strong>\u00a0the folder inside the zip\u00a0<strong>into<\/strong>your\u00a0<strong>arduino\\libraries\\<\/strong>\u00a0and\u00a0<strong>rename<\/strong>\u00a0the folder\u00a0<strong>to<\/strong>\u00a0&#8220;<strong>ManicbugRTCLib<\/strong>&#8220;,\u00a0<strong>If you already have the generic RTClib installed it is best to move it out of the libraries folder<\/strong>(Otherwise it could call the original RTCLIB). Restart the Arduino software. With a new sketch paste in the following code,\u00a0Alternatively you can go file -&gt; examples\u00a0-&gt;\u00a0ManicbugRTCLib -&gt; DS1307 (Read after my code paste to see changes I have done to the standard example).<\/p>\n<p><pre class=\"brush: php\">\/\/ 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 &lt;SPI.h&gt;\r\n#include &lt;RTClib.h&gt;\r\n#include &lt;RTC_DS1307.h&gt;\r\n\r\nRTC_DS1307 RTC;\r\n\r\nvoid setup () {\r\n    Serial.begin(57600);\r\n    Wire.begin();\r\n    RTC.begin();\r\n    DateTime now = RTC.now();\r\n    DateTime compiled = DateTime(__DATE__, __TIME__);\r\n    if (now.unixtime() &lt; compiled.unixtime()) {\r\n    Serial.println(&quot;RTC is older than compile time! Updating&quot;);\r\n    \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\r\n    RTC.adjust(DateTime(__DATE__, __TIME__));\r\n  }\r\n}\r\n\r\nvoid loop () {\r\n    DateTime now = RTC.now();\r\n    \r\n    Serial.print(now.year(), DEC);\r\n    Serial.print(&#039;\/&#039;);\r\n    Serial.print(now.month(), DEC);\r\n    Serial.print(&#039;\/&#039;);\r\n    Serial.print(now.day(), DEC);\r\n    Serial.print(&#039; &#039;);\r\n    Serial.print(now.hour(), DEC);\r\n    Serial.print(&#039;:&#039;);\r\n    Serial.print(now.minute(), DEC);\r\n    Serial.print(&#039;:&#039;);\r\n    Serial.print(now.second(), DEC);\r\n    Serial.println();\r\n    \r\n    Serial.print(&quot; since midnight 1\/1\/1970 = &quot;);\r\n    Serial.print(now.unixtime());\r\n    Serial.print(&quot;s = &quot;);\r\n    Serial.print(now.unixtime() \/ 86400L);\r\n    Serial.println(&quot;d&quot;);\r\n    \r\n    \/\/ calculate a date which is 7 days and 30 seconds into the future\r\n    DateTime future (now.unixtime() + 7 * 86400L + 30);\r\n    \r\n    Serial.print(&quot; now + 7d + 30s: &quot;);\r\n    Serial.print(future.year(), DEC);\r\n    Serial.print(&#039;\/&#039;);\r\n    Serial.print(future.month(), DEC);\r\n    Serial.print(&#039;\/&#039;);\r\n    Serial.print(future.day(), DEC);\r\n    Serial.print(&#039; &#039;);\r\n    Serial.print(future.hour(), DEC);\r\n    Serial.print(&#039;:&#039;);\r\n    Serial.print(future.minute(), DEC);\r\n    Serial.print(&#039;:&#039;);\r\n    Serial.print(future.second(), DEC);\r\n    Serial.println();\r\n    \r\n    Serial.println();\r\n    delay(3000);\r\n}\r\n\/\/ vim:ci:sw=4 sts=4 ft=cpp<\/pre><\/p>\n<p>Originally the example has &#8220;if(! RTC.isrunning())&#8221; . However i never found a time where the RTC wasnt running, even when i took the battery out it started right away as soon as power was restored. The problem here the clock is never set to the current date and time (minus a few seconds). So I replaced that bit of code with;<\/p>\n<p><pre class=\"brush: php\">    DateTime now = RTC.now();\r\n    DateTime compiled = DateTime(__DATE__, __TIME__);\r\n    if (now.unixtime() &lt; compiled.unixtime()) {\r\n    Serial.println(&quot;RTC is older than compile time! Updating&quot;);\r\n    \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\r\n    RTC.adjust(DateTime(__DATE__, __TIME__));\r\n  }<\/pre><\/p>\n<p>This code gets the time off the DS3231 module and then the time the sketch was created then compares the two dates \u00a0If it finds the RTC&#8217;s date is earlier than the date the sketch was created it updates the time to the sketch compile date. This is how you can be a few seconds out from actual time due to the time it takes to flash the Arduino.<\/p>\n<p>Once you upload the above code keep the Arduino program open, and go to\u00a0<strong>Tools -&gt; Serial Monitor<\/strong>, ensure the baud down the\u00a0bottom\u00a0right is set to\u00a0<strong>57600<\/strong>. You should see an output like:<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231advout.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-425\" title=\"DS3231advout\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231advout.png\" alt=\"\" width=\"401\" height=\"454\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231advout.png 401w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/DS3231advout-254x288.png 254w\" sizes=\"(max-width: 401px) 100vw, 401px\" \/><\/a>So here you can see that the clock has been set as it saw that the previous time was earlier than todays time. I think I will continue to use this chip and breakout board, with the above library.<\/p>\n<p>I hope this blog post helps someone get started with an RTC, chuck me a message telling me what you use it for.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you may be aware from my previous post I recently was in the market for a more accurate replacement for the DS1307 RTC clock. I tried with a DS3234 but its SPI interface made it unsuitable in my project&#8230; <a class=\"more-link\" href=\"http:\/\/www.l8ter.com\/?p=417\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":430,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[46,16],"tags":[55,56,54,51,57],"_links":{"self":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/417"}],"collection":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=417"}],"version-history":[{"count":10,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":517,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/417\/revisions\/517"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/media\/430"}],"wp:attachment":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}