{"id":27399,"date":"2016-10-14T09:30:20","date_gmt":"2016-10-14T09:30:20","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=27399"},"modified":"2019-04-02T10:23:31","modified_gmt":"2019-04-02T10:23:31","slug":"guide-for-real-time-clock-rtc-module-with-arduino-ds1307-and-ds3231","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-for-real-time-clock-rtc-module-with-arduino-ds1307-and-ds3231\/","title":{"rendered":"Guide for Real Time Clock (RTC) Module with Arduino (DS1307 and DS3231)"},"content":{"rendered":"<p>This post is about how to use the DS1307 Real Time Clock (RTC) module with the Arduino. You can also follow this guide for other similar modules\u00a0like the DS3231 RTC.<!--more--><\/p>\n<h2>Introducing the Real Time Clock module<\/h2>\n<p>The real time clock module is the one in the figure below (front and back view).<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27401\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=515%2C247&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"rtc-front-and-back-view\" width=\"515\" height=\"247\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?w=515&amp;quality=100&amp;strip=all&amp;ssl=1 515w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=150%2C72&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=300%2C144&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=80%2C38&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=220%2C106&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=209%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 209w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=280%2C134&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?resize=496%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 496w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/p>\n<p>When you first use this module, you need to solder some header pins.<\/p>\n<p>As you can see in the picture above, the module has a backup battery installed. This allows the module to retain the time, even when it\u2019s not being powered up by the Arduino. This way, every time you turn on and off your module,\u00a0the time doesn&#8217;t reset.<\/p>\n<p>This module uses I2C communication. This means that it communicates with the Arduino using just 2 pins.<\/p>\n<h2>Where to buy?<\/h2>\n<p>The Real Time Clock is an affordable module. You can check the <a href=\"https:\/\/makeradvisor.com\/tools\/real-time-clock-module-ds1307\/\" target=\"_blank\" rel=\"noopener noreferrer\">DS1307 Real Time Clock module on Maker Advisor<\/a> and find the best price.<\/p>\n<h2>Pin Wiring<\/h2>\n<p>Wiring the RTC module is pretty straightforward!<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"283\"><strong>Pin<\/strong><\/td>\n<td width=\"283\"><strong>Wiring to Arduino Uno<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"283\">SCL<\/td>\n<td width=\"283\">A5<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">SDA<\/td>\n<td width=\"283\">A4<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">VCC<\/td>\n<td width=\"283\">5V<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">GND<\/td>\n<td width=\"283\">GND<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you\u2019re using other Arduino board rather than the uno, chek out what are their SCL and SDA pins.<\/p>\n<ul>\n<li>Nano: SDA (A4); SCL(A5)<\/li>\n<li>MEGA: SDA (20); SCL(21)<\/li>\n<li>Leonardo: SDA (20); SCL(21)<\/li>\n<\/ul>\n<h2>Example: Displaying date and time on the\u00a0serial monitor<\/h2>\n<p>This example displays date and time on the serial monitor.<\/p>\n<h3>Parts required<\/h3>\n<p>For this example you need the following parts (click the links below to find the best price at\u00a0<a href=\"https:\/\/makeradvisor.com\/tools\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maker Advisor<\/a>):<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/real-time-clock-module-ds1307\/\" target=\"_blank\" rel=\"noopener noreferrer\">DS1307 RTC module<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h3>Schematics<\/h3>\n<p>Connect your Real Time Clock module to your Arduino as in the schematics below.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-44003\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?resize=714%2C393&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"714\" height=\"393\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?w=1371&amp;quality=100&amp;strip=all&amp;ssl=1 1371w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?resize=768%2C423&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/rtc.png?resize=1024%2C565&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/a><\/p>\n<h3>Code<\/h3>\n<p>Working with the RTC requires\u00a0two important steps:<\/p>\n<ul>\n<li><span style=\"color: #ff0000;\"><strong>setting the current time<\/strong>, so that the RTC knows what time is it<\/span><\/li>\n<li><strong><span style=\"color: #ff0000;\">retaining\u00a0the time, <\/span><\/strong><span style=\"color: #ff0000;\">so that the RTC always gives the correct time, even when it is turned off<\/span><\/li>\n<\/ul>\n<h4>Set the current time in the Real Time Clock<\/h4>\n<p>For setting the current time you need to change the code provided.<\/p>\n<ul>\n<li>set your current time int\u00a0the function <em>setDS3231time()<\/em><\/li>\n<\/ul>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignnone wp-image-4841\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2016\/09\/set-the-time.png?resize=509%2C57&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"set-the-time\" width=\"509\" height=\"57\" \/><\/p>\n<p>The parameters for the function are highlighted in red: seconds, minutes, hours, day of the week, date, month and year (in this order). Sunday is the day 1 of the week and Saturday is 7. Don&#8217;t forget to uncomment that line of code.<\/p>\n<p>After setting the current time, you can upload the provided code with the required modifications.<\/p>\n<p>The code provided was\u00a0written by John Boxall from\u00a0tronixstuff. You can read his tutorial <a href=\"http:\/\/tronixstuff.com\/2014\/12\/01\/tutorial-using-ds1307-and-ds3231-real-time-clock-modules-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/\/ Written by John Boxall from http:\/\/tronixstuff.com\n\n#include &quot;Wire.h&quot;\n#define DS3231_I2C_ADDRESS 0x68\n\/\/ Convert normal decimal numbers to binary coded decimal\nbyte decToBcd(byte val){\n  return( (val\/10*16) + (val%10) );\n}\n\/\/ Convert binary coded decimal to normal decimal numbers\nbyte bcdToDec(byte val){\n  return( (val\/16*10) + (val%16) );\n}\nvoid setup(){\n  Wire.begin();\n  Serial.begin(9600);\n  \/\/ set the initial time here:\n  \/\/ DS3231 seconds, minutes, hours, day, date, month, year\n  setDS3231time(30,42,16,5,13,10,16);\n}\nvoid setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte\ndayOfMonth, byte month, byte year){\n  \/\/ sets time and date data to DS3231\n  Wire.beginTransmission(DS3231_I2C_ADDRESS);\n  Wire.write(0x0E); \/\/ select register\n  Wire.write(0b00011100); \/\/ write register bitmap, bit 7 is \/EOS\n  Wire.write(decToBcd(second)); \/\/ set seconds\n  Wire.write(decToBcd(minute)); \/\/ set minutes\n  Wire.write(decToBcd(hour)); \/\/ set hours\n  Wire.write(decToBcd(dayOfWeek)); \/\/ set day of week (1=Sunday, 7=Saturday)\n  Wire.write(decToBcd(dayOfMonth)); \/\/ set date (1 to 31)\n  Wire.write(decToBcd(month)); \/\/ set month\n  Wire.write(decToBcd(year)); \/\/ set year (0 to 99)\n  Wire.endTransmission();\n}\nvoid readDS3231time(byte *second,\nbyte *minute,\nbyte *hour,\nbyte *dayOfWeek,\nbyte *dayOfMonth,\nbyte *month,\nbyte *year){\n  Wire.beginTransmission(DS3231_I2C_ADDRESS);\n  Wire.write(0); \/\/ set DS3231 register pointer to 00h\n  Wire.endTransmission();\n  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);\n  \/\/ request seven bytes of data from DS3231 starting from register 00h\n  *second = bcdToDec(Wire.read() &amp; 0x7f);\n  *minute = bcdToDec(Wire.read());\n  *hour = bcdToDec(Wire.read() &amp; 0x3f);\n  *dayOfWeek = bcdToDec(Wire.read());\n  *dayOfMonth = bcdToDec(Wire.read());\n  *month = bcdToDec(Wire.read());\n  *year = bcdToDec(Wire.read());\n}\nvoid displayTime(){\n  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;\n  \/\/ retrieve data from DS3231\n  readDS3231time(&amp;second, &amp;minute, &amp;hour, &amp;dayOfWeek, &amp;dayOfMonth, &amp;month,\n  &amp;year);\n  \/\/ send it to the serial monitor\n  Serial.print(hour, DEC);\n  \/\/ convert the byte variable to a decimal number when displayed\n  Serial.print(&quot;:&quot;);\n  if (minute&lt;10){\n    Serial.print(&quot;0&quot;);\n  }\n  Serial.print(minute, DEC);\n  Serial.print(&quot;:&quot;);\n  if (second&lt;10){\n    Serial.print(&quot;0&quot;);\n  }\n  Serial.print(second, DEC);\n  Serial.print(&quot; &quot;);\n  Serial.print(dayOfMonth, DEC);\n  Serial.print(&quot;\/&quot;);\n  Serial.print(month, DEC);\n  Serial.print(&quot;\/&quot;);\n  Serial.print(year, DEC);\n  Serial.print(&quot; Day of week: &quot;);\n  switch(dayOfWeek){\n  case 1:\n    Serial.println(&quot;Sunday&quot;);\n    break;\n  case 2:\n    Serial.println(&quot;Monday&quot;);\n    break;\n  case 3:\n    Serial.println(&quot;Tuesday&quot;);\n    break;\n  case 4:\n    Serial.println(&quot;Wednesday&quot;);\n    break;\n  case 5:\n    Serial.println(&quot;Thursday&quot;);\n    break;\n  case 6:\n    Serial.println(&quot;Friday&quot;);\n    break;\n  case 7:\n    Serial.println(&quot;Saturday&quot;);\n    break;\n  }\n}\nvoid loop(){\n  displayTime(); \/\/ display the real-time clock data on the Serial Monitor,\n  delay(1000); \/\/ every second\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/rtc_example.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h4>Retain the time in the Real Time Clock<\/h4>\n<p>If you don&#8217;t want to reset the time everytime the RTC is turned off, you should do the following:<\/p>\n<ul>\n<li>after setting up the time, you should comment the function that sets the time and upload the code again.<\/li>\n<\/ul>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4840\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2016\/09\/comment-the-code-1.png?resize=456%2C56&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"comment-the-code\" width=\"456\" height=\"56\" \/><\/p>\n<p>This is a very important step to set up the time in your RTC. If you don\u2019t do this, everytime your RTC resets, it will display the time that you&#8217;ve set up previously and not the current time.<\/p>\n<h2>Demonstration<\/h2>\n<p>Open the serial monitor at a baud rate of 9600 and you&#8217;ll see the results.<\/p>\n<p>Here&#8217;s the Serial Monitor displaying the current date and time.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28415\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=326%2C314&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"demonstration\" width=\"326\" height=\"314\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?w=326&amp;quality=100&amp;strip=all&amp;ssl=1 326w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=150%2C144&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=300%2C289&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=80%2C77&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=220%2C212&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=104%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 104w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=156%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 156w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/10\/demonstration.png?resize=247%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 247w\" sizes=\"(max-width: 326px) 100vw, 326px\" \/><\/p>\n<h2>Wrapping up<\/h2>\n<p>I hope you&#8217;ve found this guide useful.<\/p>\n<p>The RTC module is really useful and you can use it as a clock, timer, etc..<\/p>\n<p>If you would like to display the date and time with the RTC module in the OLED display, check the following post:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-oled-display-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide for 0.96 inch OLED Display with Arduino<\/a><\/li>\n<\/ul>\n<p>What projects have you done or are you expecting to build with this module?<\/p>\n<p>Let me know by writing a comment down below.<\/p>\n<p>Thanks for reading,<\/p>\n<p>Rui and Sara<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about how to use the DS1307 Real Time Clock (RTC) module with the Arduino. You can also follow this guide for other similar modules\u00a0like the DS3231 RTC.<\/p>\n","protected":false},"author":1,"featured_media":27401,"comment_status":"open","ping_status":"closed","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":""},"categories":[267,2,302,269,264,10],"tags":[],"class_list":["post-27399","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-project","category-arduino","category-0-guide","category-guide-project","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/rtc-front-and-back-view.png?fit=515%2C247&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/27399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=27399"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/27399\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/27401"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=27399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=27399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=27399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}