{"id":130,"date":"2020-09-16T19:39:21","date_gmt":"2020-09-16T19:39:21","guid":{"rendered":"https:\/\/edwinsmash.com\/?p=130"},"modified":"2020-09-16T19:39:21","modified_gmt":"2020-09-16T19:39:21","slug":"arduino-real-time-clock-module-ds3231","status":"publish","type":"post","link":"https:\/\/edwinsmash.com\/?p=130","title":{"rendered":"Arduino: Real-Time Clock Module (DS3231)"},"content":{"rendered":"\n<p>Real-Time Clock module running over I2C. You will need to give it a CR2032 battery so it retains the time when the power is removed from the Arduino.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>DEVICE<\/strong><\/td><td><strong>NANO<\/strong><\/td><\/tr><tr><td>GND<\/td><td>Ground<\/td><\/tr><tr><td>VCC<\/td><td>+5V\/+3.3V<\/td><\/tr><tr><td>SCL<\/td><td>A5<\/td><\/tr><tr><td>SDA<\/td><td>A4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You will need to install the &#8220;RTClib&#8221; library in the Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Set Time<\/h2>\n\n\n\n<p>Before use you need to set the time on the RTC. This script will pull the time from your computer and use it to configure the RTC.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h>\n#include \"RTClib.h\"\n\nRTC_DS3231 rtc;\n\nvoid setup() {\n  Wire.begin();\n  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n}\n\nvoid loop() {\n\n}<\/code><\/pre>\n\n\n\n<p>This has a couple problems: it uses whatever timezone your computer is using, and it creates a delay of however long it took your computer to compile and upload the sketch (in my case about six seconds.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Read Time<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h>\n#include \"RTClib.h\"\n\nRTC_DS3231 rtc;\n\nvoid setup() {\n  Wire.begin();\n  Serial.begin(115200);\n}\n\nvoid loop() {\n  \n    DateTime now = rtc.now();\n    \n    Serial.println(\"Current Date &amp; Time: \");\n    Serial.print(now.year(), DEC);\n    Serial.print('-');\n    Serial.print(now.month(), DEC);\n    Serial.print('-');\n    Serial.print(now.day(), DEC);\n    Serial.print(\" \");\n    Serial.print(now.hour(), DEC);\n    Serial.print(':');\n    Serial.print(now.minute(), DEC);\n    Serial.print(':');\n    Serial.print(now.second(), DEC);\n    Serial.println();\n    Serial.print(\"Unix Time: \");\n    Serial.println(now.unixtime());\n    Serial.println();\n    delay(1000);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Real-Time Clock module running over I2C. You will need to give it a CR2032 battery so it retains the time when the power is removed from the Arduino. DEVICE NANO GND Ground VCC +5V\/+3.3V SCL A5 SDA A4 You will &hellip; <a href=\"https:\/\/edwinsmash.com\/?p=130\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[8],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-arduino"],"_links":{"self":[{"href":"https:\/\/edwinsmash.com\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edwinsmash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edwinsmash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edwinsmash.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/edwinsmash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":0,"href":"https:\/\/edwinsmash.com\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/edwinsmash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edwinsmash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edwinsmash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}