DS3231.h --- Library for DS3231 (or DS3232) real-time clock

I've written a header file/library/examples for the DS3231 clock: it's available here:

I wrote it for the DS3231, but the timekeeping registers are identical on the DS3232 so it'll work for that also.

This forum has been incredibly helpful to me over the years, and I hope this is a start on repayment. Drop me an email if you use it, and let me know if you find bugs so I can fix them right away.

-Eric Ayars

Great work.

Just a suggestion, why not integrate on the Time Library? There's some examples using DS1307 already, just have to port to work with your library.

Time Library Link:
http://www.arduino.cc/playground/Code/Time

I'm with some spare time so I made the library.
https://bitbucket.org/trunet/ds3231rtc

Fully compatible with Time Library and has getTemp method to retrieve temperature.

Just use the TimeRTC examples provided on Time and change DS1307RTC.h to DS3231RTC.h.

Thanks, Eric. This is cool, I'll be needing that chip in the future and I am glad to have your work!
:smiley:

Hi,

I was just curious if you are planning to upgrade this library to Arduino 1.0?

Regards
James

it just needs minor updates. I can work with this next week maybe.

As it turns out i ran into the same issue with the 1.0 ide and ended up updating it myself.
I ran the examples and they seem to work just fine.

i do not know if you already updated the code or not but here is the a copy of the files i updated.
The example codes remains unchanged but is now in an ino file.

http://soiling-detector.googlecode.com/files/DS3231updated.zip

Your orig lib and examples helped me understand how to get this IC up and running for my datalogger.
I have found a few of your sketches to be rather helpful.
Thank you

that's the way open source software works... thanks for your help and I'll update the code later.

jhub:
http://soiling-detector.googlecode.com/files/DS3231updated.zip

I set my clock with the example in the zip, and when I run the example test for some reason one time is exactly 6 hours off of the other. The logs look like:

2012 2 17 5 19 30 25 24h T=20.00 O+
Alarm 1: 5 DoW 19 22 30 enabled
Alarm 2: 17 Date 19 23 enabled
12/2/17day of the week :5
13:30:26

Any ideas where I can look for the bug? Or ideas what it may be?

It appears from my tests that getHour() is returning the proper hours, but getTime() is not, looking at the code and there didnt seem to be any glaring differences though, any local pros have an idea?

Found it, there is a bug on line 40 of DS3231.cpp where the hours are run through bdcToDec() twice. To fix this, on line 40 change:

tempBuffer = bcdToDec(Wire.read());

to

tempBuffer = Wire.read();

After this your getTime() calls should read correctly!

hm... how will i get leading zeros?

can anybody help?

Leading Zeros formatting. Enjoy 8)
05/23/2014

	// Display the time >  getTime() function
	Clock.getTime(year, month, date, DoW, hour, minute, second);

        if (month < 10)
          {
            Serial.print("0");
            Serial.print(month, DEC);
          }
        else
          {
            Serial.print(month, DEC);
          }

        Serial.print("/");

	if (date < 10)
          {
            Serial.print("0");
            Serial.print(date, DEC);
          }
       else
          {
            Serial.print(date, DEC);
          }
          
        Serial.print("/");
        
        Serial.print("2");
	if (Century) 
          {			// Won't need this until 2100 - or if clock gets reset.
		Serial.print("1");
	  }
        else
          {
		Serial.print("0");
	  }
        if (year < 10)             // used if the clock gets reset or if the year is 2100 - 2110
          {
            Serial.print("0");
          }
	Serial.println(year);
 
        Serial.print("day of the week : ");
	Serial.println(DoW, DEC);

        if (hour <10)
          {
            Serial.print("0");
            Serial.print(hour, DEC);
          }
	else
          {
            Serial.print(hour, DEC);
          }
          
        Serial.print(":");
	
        if (minute < 10)
          {
            Serial.print("0");
            Serial.print(minute, DEC);
          }
        else
          {
            Serial.print(minute, DEC);
          }
        Serial.print(":");
        if (second < 10)
        {
          Serial.print("0");
          Serial.println(second, DEC);
          
        }
	else
        {
          Serial.println(second, DEC);
        }

// simple way to format the output to include the leading zeros.
// with adjustments for formatting the year if clock gets reset or year 2100+
// NewAgeRyan85

I see that the last post was a little over a year ago. has this code been fully fleshed out? Or will I need to apply the above corrections to the code? And I am a bit confused as to which header file to download.

Thanks

can someone help us? is the code in post #12 updated?

Hi to all!
Tell me, please, where is the latest ds3231 library without any bugs?
Thanks!