loading

I have found that the use of the RTC chip- DS3231 is extremely hard using the arduino. Just setting the time is pretty complex, not mentioning the code. Therefore I have found a great and easy to use library which really enhances the use of the DS3231 chip. Let's get right onto it.

Step 1: Connections

Of course, you will need to connect the chip first. It is very easy, do it according to the pictures or below:

VCC -> Arduino 5V

GND -> Arduino GND

SCL -> SCL or A5

SDA -> SDA or A4

As long as I know, there are dedicated SCL and SDA pins on the Arduino UNO and MEGA.

There are also two other pins which are the32K and SQW ones but we will not use them as we get the full functionality through the I2C interface.

Step 2: Library

We will use a library from Henning Karlsen which is great. Thanks a lot for that! There are some basic functions such as reading time and date and writing time and date. Download the library here:

http://www.rinkydinkelectronics.com/library.php?id=73

Step 3: The Code

I will just use an example sketch from the library which will include lots of comments for you to read, enjoy:

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved // web: http://www.RinkyDinkElectronics.com/ // // A quick demo of how to use my DS3231-library to // quickly send time and date information over a serial link // // To use the hardware I2C (TWI) interface of the Arduino you must connect // the pins as follows: // // Arduino Uno/2009: // ---------------------- // DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin // SCL pin -> Arduino Analog 5 or the dedicated SCL pin // // Arduino Leonardo: // ---------------------- // DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin // SCL pin -> Arduino Digital 3 or the dedicated SCL pin // // Arduino Mega: // ---------------------- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin // // Arduino Due: // ---------------------- // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin // // The internal pull-up resistors will be activated when using the // hardware I2C interfaces. // // You can connect the DS3231 to any available pin but if you use any // other than what is described above the library will fall back to // a software-based, TWI-like protocol which will require exclusive access // to the pins used, and you will also have to use appropriate, external // pull-up resistors on the data and clock signals. //

#include

// Init the DS3231 using the hardware interface DS3231 rtc(SDA, SCL);

void setup() { // Setup Serial connection Serial.begin(115200); // Uncomment the next line if you are using an Arduino Leonardo //while (!Serial) {} // Initialize the rtc object rtc.begin(); // The following lines can be uncommented to set the date and time //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 }

void loop() { // Send Day-of-Week Serial.print(rtc.getDOWStr()); Serial.print(" "); // Send date Serial.print(rtc.getDateStr()); Serial.print(" -- ");

// Send time Serial.println(rtc.getTimeStr()); // Wait one second before repeating :) delay (1000); }

Step 4: Done

There you go, this library is really easy to use. Have a good day!

Samuel

<p>wait no fixed it. Just needed to change the baud to 9600</p><p>thanks for reading</p>
<p>The character are...weird.</p><p>Yes I have the right board selected and I have the right port selected</p>
<p>Thanks for the instructions. It should be noted that &quot;rtc.setDate(1, 1, 2014)&quot; is &quot;rtc.setDate(Day, Month, Year)&quot;, when setting the date for the initial configuration.</p><p>When I set it for today's date (1, 14, 2017), it was going back to January 1, 1970. If you see that, simply update the date entry, and Verify/Upload to correct.</p>
<p>my serial cant pirnt it out</p>
<p>Serial.begin(115200);</p><p>You have to change the baud at the bottom right from the current/default 9600, to the one in the code; 155200. Click on the drop-down menu, and choose &quot;155,200 baud&quot; and it will start to work for you.</p>
<p>Hi, I am trying to use my DS3231 with ArduinoUno. Somehow the thing seems to &quot;freeze&quot; as soon as it interacts with the RTC. <br>I've tried using the code suggested above, and it stops right at &quot;Serial.print(rtc.getDOWStr());&quot;. Anything I put in loop () before that will be executed (turning LEDs on, or some serial.print). But nothing after happens. <br>I have double checked all the connections and I already replaced the battery of the DS3231 to make sure that is not empty. The red LED on the DS is on, so it has power.<br>Any suggestions how to fix this or pin down what the problem might be?<br>Thanks!<br></p>
<p>include what ?</p>
<p>#include &lt;DS3231.h&gt;</p><p>Before dng that make sure u hv downloaded the DS3231 library</p>
<p>I am looking to use the DS3231 library and display the time digits on a 4 digit 7-segment display. I don't want to use the string functions which the document shows the return format. I am thinking I want to use the rtc.getTime() function, however, there are no examples of what is returned and what data types should I be using for hour, minute, second.</p><p>Any help would be appreciated.</p>
<p>rtc.getTime() retourne un Time.... mais pourquoi ne veux-tu pas utiliser les strings function ?</p>
<p>Did you ever work this out? I'm trying to do the same thing.</p><p>thanks</p>
<p>Try this Code </p><p>// Date and time functions using a DS3231 RTC connected via I2C and Wire lib. Use a 4 digit common anode 7 segment display &amp; 2 shift registers (74HC595). </p><p>#include &lt;Wire.h&gt;</p><p>#include &quot;RTClib.h&quot;</p><p>RTC_DS3231 rtc;</p><p>char daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};</p><p>int unitHour, tenHour, unitMinute, tenMinute;</p><p>int clockPin = 8; // Pin 8 of Arduino connected in the pin 11 of 74HC595 (Clock)</p><p>int latchPin = 9; // Pin 9 of Arduino connected in the pin 12 of 74HC595 (Latch)</p><p>int dataPin = 10; // Pin 10 of Arduino connected in the pin 14 of 74HC595 (Data)</p><p>//Digits Matrix - 0 a 9</p><p>byte num[] = {</p><p> B01111110, // Zero</p><p> B00110000, // One</p><p> B01101101, // Two</p><p> B01111001, // Three</p><p> B00110011, // Four</p><p> B01011011, // Five</p><p> B01011111, // Six</p><p> B01110000, // Seven</p><p> B01111111, // Eight</p><p> B01111011, // Nine</p><p> };</p><p>void setup () {</p><p> Serial.begin(9600);</p><p> delay(3000); // wait for console opening</p><p> if (! rtc.begin()) {</p><p> Serial.println(&quot;Couldn't find RTC&quot;);</p><p> while (1);</p><p> pinMode(latchPin, OUTPUT); // Define the 3 digital pins as output</p><p> pinMode(clockPin, OUTPUT);</p><p> pinMode(dataPin, OUTPUT);</p><p> }</p><p> if (rtc.lostPower()) {</p><p> Serial.println(&quot;RTC lost power, lets set the time!&quot;);</p><p> // following line sets the RTC to the date &amp; time this sketch was compiled</p><p> rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));</p><p> // This line sets the RTC with an explicit date &amp; time, for example to set</p><p> // January 21, 2014 at 3am you would call:</p><p> // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));</p><p> }</p><p>}</p><p>void loop () {</p><p> DateTime now = rtc.now();</p><p> Serial.print(now.year(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(now.month(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(now.day(), DEC);</p><p> Serial.print(&quot; (&quot;);</p><p> Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);</p><p> Serial.print(&quot;) &quot;);</p><p> Serial.print(now.hour(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(now.minute(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(now.second(), DEC);</p><p> Serial.println();</p><p> unitHour = now.hour() % 10;</p><p> tenHour = now.hour() / 10;</p><p> unitMinute = now.minute() % 10;</p><p> tenMinute = now.minute() / 10;</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 8); //Set DISPLAY 1 (top view from left to right)</p><p> shiftOut(dataPin, clockPin, LSBFIRST, ~num[tenHour]); //Set the Hour (ten)</p><p> digitalWrite(latchPin, HIGH);</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 4); //Set DISPLAY 2</p><p> shiftOut(dataPin, clockPin, LSBFIRST, ~num[unitHour]); //Set the Hour (unit)</p><p> digitalWrite(latchPin, HIGH);</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 2); //Set DISPLAY 3</p><p> shiftOut(dataPin, clockPin, LSBFIRST, ~num[tenMinute]); //Set the Minute (ten)</p><p> digitalWrite(latchPin, HIGH);</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 1); //Set DISPLAY 4</p><p> shiftOut(dataPin, clockPin, LSBFIRST, ~num[unitMinute]); //Set the Minute (unit)</p><p> digitalWrite(latchPin, HIGH);</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 4); //Set LED of dots</p><p> shiftOut(dataPin, clockPin, LSBFIRST, ~B10000000); //Set LEDs of double dots</p><p> digitalWrite(latchPin, HIGH);</p><p> digitalWrite(latchPin, LOW);</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 1); //Set DISPLAY 4</p><p> shiftOut(dataPin, clockPin, LSBFIRST, 255); //Reset the DISPLAY 4 (to avoid some flicking)</p><p> digitalWrite(latchPin, HIGH);</p><p> Serial.print(&quot; since midnight 1/1/1970 = &quot;);</p><p> Serial.print(now.unixtime());</p><p> Serial.print(&quot;s = &quot;);</p><p> Serial.print(now.unixtime() / 86400L);</p><p> Serial.println(&quot;d&quot;);</p><p> // calculate a date which is 7 days and 30 seconds into the future</p><p> DateTime future (now + TimeSpan(7,12,30,6));</p><p> Serial.print(&quot; now + 7d + 30s: &quot;);</p><p> Serial.print(future.year(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(future.month(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(future.day(), DEC);</p><p> Serial.print(' ');</p><p> Serial.print(future.hour(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(future.minute(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(future.second(), DEC);</p><p> Serial.println();</p><p> Serial.println();</p><p> delay(3000);</p><p>}</p>
<p>HI</p><p> i used DS 3231 it work but i have a probl&egrave;m when u i want <br>to change the time (add one hour:summer hour),so i used the button with 2<br> states but dosn't work ,plz help</p>
<p>I tried but I get this (see pictures) </p><p>please help me </p>
<p>Change the Serial.begin () into serial.begin(9600).</p><p>Work fine for me.</p>
<p>thx it works :D</p>
<p>Works perfectly on arduino nano 3.0. Just follow the instructions on the web page or the pdf manual. The only mistake was to exchange SCL with SDA. Just correct and everything is fine. Thanks a lot!!! </p>
<p>hi, did you change the exchange the wiring for SCL and SDA or was it in the code?</p><p>I've been trying but it's not working for me. HELP!!! Also, i just want to make sure the SDA goes to A4 and SCL goes to A5 right??</p><p>Thanks</p>
<p>Yes, SDA goes to A4 and SCL goes to A5. I had placed them the wrong way and of course it did not operate until I fixed the mistake. My intention was just to assure that it works fine on arduino nano also.</p><p>Just make sure that you have the correct pinout for the version of the arduino you are using.</p><p>Regards,</p>
<p>hello my friend good day.</p><p>if you will send mi the code, because i have a proyect in my colleg and i has have dificult. </p><p>can you send mi the librery from arduino uno of DS3231 RTC </p><p>THANKS MI FREND....</p>
<p>Hi! <br><br>I'm getting an error on this line: </p><blockquote>// Init the DS3231 using the hardware interface<br>DS3231 rtc(SDA, SCL);</blockquote><p>no matching function for call to 'DS3231::DS3231(const uint8_t&amp;, const uint8_t&amp;)'</p>
<p>put this </p><p>#include &lt;DS3231.h&gt; and delete de #include line</p>
<p>Hi,</p><p>sorry, I get an error </p><p>libraries\DS3231\DS3231.cpp:83:49: error: 'uint8_t' was not declared in this scope<br><br> void DS3231::setTime(uint8_t hour, uint8_t min, uint8_t sec)</p>
<p>Thank you for the guide, it worked great!</p><p>i have a small question though, i want to turn on an led at the same time every day, looking at the RTC side of thing i have come up with the following, however correct me if i am wrong but unix time includes the date too? i just want the time every day</p><p>if (rtc.getUnixTime(rtc.getTime()) == 1459255200)</p><p>analogWrite(led, 64);</p>
<p><a href="http://forum.arduino.cc/index.php?topic=389915.0" rel="nofollow">http://forum.arduino.cc/index.php?topic=389915.0</a></p><p>for anyone interested</p>
<p>Very useful and easy to follow, thanks alot! :D </p>
<p>Muchas gracias!</p>
<p>why it shows error while compling in arduino ide?</p>
<p>You must have to set the include library, the line is not finished. Another thing that you may have to change is the serial conection baud rate, the code says &quot;115200&quot; I use &quot;9600&quot; and work fine. Thanks for this instructable!</p>
<p>Very helpful, thank you!</p>
<p>Hey </p><p>available at this code a Arlam or a simple if statement ?</p>
<p>is it a application or files? how do i run it?</p>
<p>what does the library do?</p>
<p>Good stuff Samuel!</p><p>What confusion I was causing myself when I set the RTC to January 25, 2016 using the US convention. For anyone not reading the DS3231 library PDF, you must use the European standard date format of: &quot;rtc.setDate(25, 1, 2016);&quot;</p><p>Expect *bad things* to happen if you use &quot;rtc.setDate(1, 25, 2016);&quot; !</p>
<p>My first use of the RTC. Works great. Thank you. Can you are anyone help with where and how to insert code into the sketch to convert the C deg to F deg? </p>
<p>void loop()</p><p>{</p><p> // Send current temperature</p><p><strong> float temp = ( ( rtc.getTemp() * ( 9/5 )) + 32 );</strong></p><p> Serial.print(&quot;Temperature: &quot;);</p><p> Serial.print(temp);</p><p> Serial.println(&quot; C&quot;);</p><p> delay (1000);</p><p>}</p>
HeshamA14, <br>Thank you for your response.<br>Your suggestion didn't work for me exactly as your code, but it put me on the right track. A flag was thrown on &quot;float temp&quot;. Another step in the learning process.<br><br>I declared two vars. int val=0; and int Ftemp; then inserted the following code just before delay(1000)<br><br> // Send current temp in F per HeshamA14 20160118<br> Ftemp = ((val*9/5)+32);<br> Serial.print(&quot;Temperatture: &quot;);<br> Serial.print(Ftemp);<br> Serial.println(&quot;F&quot;);<br><br> // Wait one second before repeating :)<br> delay (1000);<br><br>
<p>Thanks for this Samuel, you prob saved me hours. One more question: Why does the following code not work? <br><br>if (rtc.getTimeStr() /= &quot;12:04:20&quot;) {</p><p> digitalWrite(13,HIGH);</p><p>}</p>
<p>Howdy RubenS13, instead of /= you should be using == but for some reason it will still not work. I found that using the Unix time did do the trick, so something like:</p><p>if (rtc.getUnixTime(rtc.getTime()) == 946745100)</p><p>Hope this helps.</p>
<p>The Unix time has a small disadvantage, it also includes date. So I had to reset the date every day, not really full proof. Hence I ended up using a different library with alarm function:</p><p><a href="https://github.com/jarzebski/Arduino-DS3231" rel="nofollow">https://github.com/jarzebski/Arduino-DS3231</a></p><p>It actually has 2 alarms, as I am using it for my aquarium it is perfect to trigger an on and off cycle.</p>
<p>Are you trying to set 13 to high when the time does not equal 12:04:20? If so you would want to use !=</p><p>I am not familiar with a /= operator </p>
<p>Hey Sam,</p><p>I was wondering if you knew a way to add an hour to the DS3231 module via a button. I tried doing it via a button state == HIGH with rtc.hour = hour+1; but it didn't work. I took a look at the library, and it looks like there is a way to do it, I just can't figure out how. Any input is appreciated. </p><p><br>Thanks,</p><p><br>Tyler</p>
<p>I also added temperature.</p><p> Serial.print(&quot;\t Temp: &quot;);</p><p> Serial.println(rtc.getTemp());</p>
<p>Well written, worked great! I also was confused by the strange output characters until I read another comment about setting the Serial Monitor baud rate to 115200.</p>
Thanks for all these, I change el baud rate and works perfect just like zrmarin13 said. I have one question:<br>&iquest;How can I use the hour and the second of the RTC? I want to use it for simulate the sun light intensity while the time it's increasing
<p>Thanks ^^</p>
<p>why is the baud rate so high?</p>
<p>It's working! Thanks!</p>
<p>I just bought a couple of these RTC modules, my first RTC module ever(as well as first i2c module). <br>Your guide got me running in a matter of seconds :P so thanks for this simple but much needed guide :)</p><p>Although there is an &quot;error&quot; in your sketch, you just have &quot;#include&quot; instead of &quot;#include &lt;DS3231.h&gt;&quot;. Of course, anyone whos ever used an arduino before should be able to fix/figure it out in half a second. :)</p>
<p>Could not find any good info, here it is! thx :D<br></p>

About This Instructable

209,099views

102favorites

License:

Bio: Hello, My name is Samuel, I am a student. I love micro controllers like arduino, they are my favorite interest. I also do geocaching, a ... More »
More by samuel123abc:Real time clock using DS3231 (EASY) Controlling 120-240 VAC with a relay using arduino Nokia 5110 graphics tutorial 
Add instructable to: