DS3231 Real-time Clock - how to get fraction of a second?

I got one of the these modules to try and time some LEDS , it works fine and there are a few librarys around for it but all I seem to be able to get from them is Year, month, day, hour, minute and second but no fractions of a second. Does anyone have any experience with one of these ?

(i am finding too much lag with using millis(); so I thought an external clock would be the go.

Thanks, Andy

Look at the data sheet for the chip. The lowest resolution from the clock is seconds. The chip has a square wave output that can be up to 8kHz, so you may be able to use that and count for yourself.

andycomic: I got one of the these modules to try and time some LEDS

What are trying to do? Do you really need true wall/calendar time or just a timing interval? If you are trying to time between events, then things like using millis() or pulsein() are better.

Trying to use an RTC over i2c for something more precise than seconds is going to be difficult.

Depending on the accuracy needed, even if you use the sqw output, the challenge becomes syncing sqw output with the internal instance of time when the RTC bumps the second, especially since it would have to take into consideration the i2c overhead during initialization. It can be done but it isn't just a simple matter of counting sqw interrupts.

Then there is using the Alarm capability to generate an alarm every second which will assert the sqw signal, but then clearing it becomes the issue. You can save the millis() time in the ISR which can be used to sync up the RTC seconds with but if you try to clear the alarm in the ISR interrupts the Wire code will hang. But even if it doesn't hang, the i2c bus is so slow, that you would miss millis() timer ticks. So you would have to coordinate this with the foreground loop code.

--- bill

Thanks very much for your help, I will try to use an ISR to get it pretty close. I am not sure where the delay is coming in but my program is not hitting its timing cues at all when I run the led program trying to match music beats. When i turn the leds off (no led calls) the clock runs perfect. I guess there is a delay somewhere in the Adafruit library.

Much appreciated though you have put me on the right track.

(heres the test we did: https://www.youtube.com/watch?v=VABeFw4nbqY)

This looks more like something that would use time intervals rather than need accurate DateTime time of day. There are some libraries out there that can schedule based on timer interrupts. If you would describe what you are really needing or perhaps show some code we could help out a bit more.

--- bill

Hi Bill, It works good enough now using the RTC to find the nearest second and then using a delay or the Metro timer to count the partial second. I tried using varous timers but they all dragged after a while. Thanks for your help though. For this project I am happy enough close it up and move onto something else.


You do not want an ISR.

You just want to use millis() to determine your intervals. At the start of the interval, save your millis() value in a long, then you wait by polling millis(), subtracting your saved reference each time and comparing this to your desired delay figure. In between such successive polls, you are able to perform other basic tasks, given that nothing ever "busy waits".