{"id":375,"date":"2011-09-18T19:42:55","date_gmt":"2011-09-18T10:12:55","guid":{"rendered":"http:\/\/www.l8ter.com\/?p=375"},"modified":"2011-09-28T18:14:09","modified_gmt":"2011-09-28T08:44:09","slug":"arduino-deadon-rtc-ds3234-wiring-example-and-tutorial","status":"publish","type":"post","link":"http:\/\/www.l8ter.com\/?p=375","title":{"rendered":"Arduino DeadOn RTC &#8211; DS3234 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>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>I recently purchased a Arduino DeadOn RTC \u00a0DS3234 breakout board. (<a href=\"http:\/\/www.sparkfun.com\/products\/10160\">http:\/\/www.sparkfun.com\/products\/10160<\/a>). Initially i was\u00a0planning\u00a0to use this as a (more accurate) replacement for a DS1307, only to find out that the DS1307 uses I2C whilst the DS3234 uses SPI. This blog post is written to hopefully help others avoid \u00a0wasting an hour or so learning about this chip and how to get it\u00a0working quickly and easily\u00a0\u00a0with an Arduino Diecimila\/Duemilanove\/Uno.<\/p>\n<p>In basic terms the I2C only uses 2 conenctions (Clock, Data) to communicate over the analog input pin 3 and 4 (<a href=\"http:\/\/www.arduino.cc\/en\/Reference\/Wire\">Arduino help<\/a>). In contrast the SPI uses 4 connections to communicate (SS &#8211; pin 10, MOSI &#8211; pin 11, MISO &#8211; pin12, and SCK &#8211; Pin13). (<a href=\"http:\/\/www.arduino.cc\/en\/Reference\/SPI\">Read more at Arduino help<\/a>).<\/p>\n<p>Now we have a short introduction done lets get to the wiring image and getting the sample code (from sparkfun) to run;<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels.png\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-380\" title=\"RTCDS3234_labels\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels.png\" alt=\"\" width=\"797\" height=\"1056\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels.png 797w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels-217x288.png 217w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels-372x494.png 372w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/RTCDS3234_labels-452x600.png 452w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/a><\/p>\n<p>You may notice that SS\u00a0isn&#8217;t\u00a0connected\u00a0to 10 as SPI documentation stated, this is\u00a0because\u00a0sparkfuns example defines pin 8 as part of the sample, you may also notice that there is an additional pin SQW connected to GND, This is to allow the use of the RTC Libary that we will cover later on in the post. First here is the first code snippet of the sparkfun example:<br \/>\n<pre class=\"brush: php\">#include &lt;SPI.h&gt;\r\nconst int  cs=8; \/\/chip select \r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n  RTC_init();\r\n  \/\/day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)\r\n  SetTimeDate(11,12,13,14,15,16); \r\n}\r\n\r\nvoid loop() {\r\n  Serial.println(ReadTimeDate());\r\n  delay(1000);\r\n}\r\n\/\/=====================================\r\nint RTC_init(){ \r\n\t  pinMode(cs,OUTPUT); \/\/ chip select\r\n\t  \/\/ start the SPI library:\r\n\t  SPI.begin();\r\n\t  SPI.setBitOrder(MSBFIRST); \r\n\t  SPI.setDataMode(SPI_MODE1); \/\/ both mode 1 &amp; 3 should work \r\n\t  \/\/set control register \r\n\t  digitalWrite(cs, LOW);  \r\n\t  SPI.transfer(0x8E);\r\n\t  SPI.transfer(0x60); \/\/60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled\r\n\t  digitalWrite(cs, HIGH);\r\n\t  delay(10);\r\n}\r\n\/\/=====================================\r\nint SetTimeDate(int d, int mo, int y, int h, int mi, int s){ \r\n\tint TimeDate [7]={s,mi,h,0,d,mo,y};\r\n\tfor(int i=0; i&lt;=6;i++){\r\n\t\tif(i==3)\r\n\t\t\ti++;\r\n\t\tint b= TimeDate[i]\/10;\r\n\t\tint a= TimeDate[i]-b*10;\r\n\t\tif(i==2){\r\n\t\t\tif (b==2)\r\n\t\t\t\tb=B00000010;\r\n\t\t\telse if (b==1)\r\n\t\t\t\tb=B00000001;\r\n\t\t}\t\r\n\t\tTimeDate[i]= a+(b&lt;&lt;4);\r\n\t\t  \r\n\t\tdigitalWrite(cs, LOW);\r\n\t\tSPI.transfer(i+0x80); \r\n\t\tSPI.transfer(TimeDate[i]);        \r\n\t\tdigitalWrite(cs, HIGH);\r\n  }\r\n}\r\n\/\/=====================================\r\nString ReadTimeDate(){\r\n\tString temp;\r\n\tint TimeDate [7]; \/\/second,minute,hour,null,day,month,year\t\t\r\n\tfor(int i=0; i&lt;=6;i++){\r\n\t\tif(i==3)\r\n\t\t\ti++;\r\n\t\tdigitalWrite(cs, LOW);\r\n\t\tSPI.transfer(i+0x00); \r\n\t\tunsigned int n = SPI.transfer(0x00);        \r\n\t\tdigitalWrite(cs, HIGH);\r\n\t\tint a=n &amp; B00001111;    \r\n\t\tif(i==2){\t\r\n\t\t\tint b=(n &amp; B00110000)&gt;&gt;4; \/\/24 hour mode\r\n\t\t\tif(b==B00000010)\r\n\t\t\t\tb=20;        \r\n\t\t\telse if(b==B00000001)\r\n\t\t\t\tb=10;\r\n\t\t\tTimeDate[i]=a+b;\r\n\t\t}\r\n\t\telse if(i==4){\r\n\t\t\tint b=(n &amp; B00110000)&gt;&gt;4;\r\n\t\t\tTimeDate[i]=a+b*10;\r\n\t\t}\r\n\t\telse if(i==5){\r\n\t\t\tint b=(n &amp; B00010000)&gt;&gt;4;\r\n\t\t\tTimeDate[i]=a+b*10;\r\n\t\t}\r\n\t\telse if(i==6){\r\n\t\t\tint b=(n &amp; B11110000)&gt;&gt;4;\r\n\t\t\tTimeDate[i]=a+b*10;\r\n\t\t}\r\n\t\telse{\t\r\n\t\t\tint b=(n &amp; B01110000)&gt;&gt;4;\r\n\t\t\tTimeDate[i]=a+b*10;\t\r\n\t\t\t}\r\n\t}\r\n\ttemp.concat(TimeDate[4]);\r\n\ttemp.concat(&quot;\/&quot;) ;\r\n\ttemp.concat(TimeDate[5]);\r\n\ttemp.concat(&quot;\/&quot;) ;\r\n\ttemp.concat(TimeDate[6]);\r\n\ttemp.concat(&quot;     &quot;) ;\r\n\ttemp.concat(TimeDate[2]);\r\n\ttemp.concat(&quot;:&quot;) ;\r\n\ttemp.concat(TimeDate[1]);\r\n\ttemp.concat(&quot;:&quot;) ;\r\n\ttemp.concat(TimeDate[0]);\r\n  return(temp);\r\n}\r\n<\/pre><\/p>\n<p>Once uploaded keep the Arduino program open, and go to <strong>Tools -&gt; Serial Monitor<\/strong>, ensure the baud down the\u00a0bottom\u00a0right is set to <strong>9600<\/strong>. You should see an output like:<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/sparkfunoutput.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-383\" title=\"sparkfunoutput\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/sparkfunoutput.jpg\" alt=\"\" width=\"370\" height=\"421\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/sparkfunoutput.jpg 370w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/sparkfunoutput-253x288.jpg 253w\" sizes=\"(max-width: 370px) 100vw, 370px\" \/><\/a><\/p>\n<p>And so on, The code sets the time to 11\/12\/13 14:15:16 on boot, from there it increments second by second as you would expect. So success, if you have got this far your doing well. But lets get\u00a0something\u00a0that would be a little more use to us in our programs.<\/p>\n<p>Thankfully a user (<a href=\"https:\/\/github.com\/maniacbug\">manicbug<\/a>) made a mod to the standard RTC\u00a0library (As used with 1307 and chronodots), allowing you to utilise the same code and calls that you may have been using\/were using\/could use with a DS1307. You can<a href=\"https:\/\/github.com\/maniacbug\/RTClib\/archives\/master\"> jump onto the github service and download a .zip of the current release<\/a> or you can use the copy that I&#8217;m writing this tutorial with (so it should work) <a href=\"http:\/\/www.l8ter.com\/arduino\/maniacbug-RTClib.zip\">here<\/a>. <strong>Extract<\/strong> the folder inside the zip <strong>into<\/strong> your <strong>arduino\\libraries\\<\/strong> and <strong>rename<\/strong> the folder <strong>to<\/strong> &#8220;<strong>ManicbugRTCLib<\/strong>&#8220;, <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; DS3234.<br \/>\n<pre class=\"brush: php\">\/\/ Date and time functions using a DS1307 RTC connected via I2C and Wire lib\r\n\r\n#include &lt;SPI.h&gt;\r\n#include &lt;Wire.h&gt;\r\n#include &lt;RTClib.h&gt;\r\n#include &lt;RTC_DS3234.h&gt;\r\n\r\n\/\/ Avoid spurious warnings\r\n#undef PROGMEM\r\n#define PROGMEM __attribute__(( section(&quot;.progmem.data&quot;) ))\r\n#undef PSTR\r\n#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &amp;__c[0];}))\r\n\r\n\/\/ Create an RTC instance, using the chip select pin it&#039;s connected to\r\nRTC_DS3234 RTC(8);\r\n\r\nvoid setup () {\r\n    Serial.begin(57600);\r\n    Serial.println(&quot;RTClib\/examples\/ds3234\/&quot;);\r\n    SPI.begin();\r\n    RTC.begin();\r\n\r\n  if (! RTC.isrunning()) {\r\n    Serial.println(&quot;RTC is NOT running!&quot;);\r\n    Serial.print(&quot;Setting time to... &quot;);\r\n    Serial.print(__DATE__);\r\n    Serial.print(&#039; &#039;);\r\n    Serial.println(__TIME__);\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    const int len = 32;\r\n    static char buf[len];\r\n\r\n    DateTime now = RTC.now();\r\n\r\n    Serial.println(now.toString(buf,len));\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.println(future.toString(buf,len));\r\n    \r\n    Serial.println();\r\n    delay(3000);\r\n}\r\n\/\/ vim:cin:ai:sw=4 sts=4 ft=cpp<\/pre><\/p>\n<p>Once uploaded 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 <strong>57600<\/strong>. You should see an output like:<\/p>\n<p><a href=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/rtcoutput.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-385\" title=\"rtcoutput\" src=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/rtcoutput.jpg\" alt=\"\" width=\"489\" height=\"553\" srcset=\"http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/rtcoutput.jpg 489w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/rtcoutput-254x288.jpg 254w, http:\/\/www.l8ter.com\/wp-content\/uploads\/2011\/09\/rtcoutput-436x494.jpg 436w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/a><\/p>\n<p>You will notice that the time is now formatted correctly, still as 11\/12\/13 14:16:17 (+ time since previous program). This is because the RTC has continued to run since your last program, The easiest way to fix this is to unplug the Arduino pop the battery out of the RTC wait 5 seconds then put the battery back in and connect the Arduino and compile&amp; upload the program again (quickly if you want it super accurate). This will set the time to the same time as the sketch was compiled and uploaded, from here it should continue on and keep time.\u00a0Unfortunately\u00a0after all this the SPI of the DC3234 uses pins that are already in use, looks like its time to buy or build a chronodot that can utilise the unused analog pins and keep\u00a0similar\u00a0accuracy.<\/p>\n<p>I Hope this tutorial helps someone out there, let me know how you go and what you think. I&#8217;m no expert with this, but I&#8217;m happy to stumble through and help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; I recently purchased a Arduino DeadOn RTC \u00a0DS3234 breakout board. (http:\/\/www.sparkfun.com\/products\/10160). Initially i was\u00a0planning\u00a0to use this as a (more accurate) replacement for a DS1307, only to find out that the DS1307 uses I2C whilst the&#8230; <a class=\"more-link\" href=\"http:\/\/www.l8ter.com\/?p=375\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":377,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[46,16],"tags":[49,47,48,51,52,50],"_links":{"self":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/375"}],"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=375"}],"version-history":[{"count":11,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":415,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/415"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=\/wp\/v2\/media\/377"}],"wp:attachment":[{"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.l8ter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}