Configure DS3234 RTC in OSBSS dataloggers

The DS3234 RTC breakout from SparkFun does not include a backup battery (CR1225). After inserting the battery, the RTC’s oscillator initializes and starts keeping time from 1/1/2000 00:00:00. The RTC’s time needs to be manually setup once. If you’ve already assembled an OSBSS datalogger, upload the following code to configure the current time in the RTC:

You need to install the DS3234 library for syncing time.

In the function RTC.setDateTime(), replace the numbers with the current day, month, year, hour, minute and second and upload the code. Make sure you select the correct board and processor in the IDE. In most OSBSS dataloggers, this should be Arduino Pro Mini (3.3V, 8MHz). After uploading, open the serial monitor and ensure the baud rate is 19200. The RTC starts keeping time from your configured date and time. Once the RTC is set, you can configure the launch time and upload the datalogger’s code without unplugging the FTDI cable. This is to ensure the RTC isn’t configured again to your previously set time once you plug the cable back in. The OSBSS datalogger will now start logging at the exact date and time.

If you want to sync the RTC with your PC’s exact time, set the minute variable in the function to a minute ahead in the future. After the code is set, press the serial monitor button about two seconds before the set minute. This takes care of that small delay before the serial monitor opens. You should now see your PC’s time and the RTC’s time to be in perfect sync.

Note: PC’s clocks are not accurate. They are known for their large drifts and should not be relied on for referring to the exact time. If you’re concerned about maintaining the exact date and time, check your PC’s clock time with a web server (which is usually synced with an atomic clock). In Windows, the clock time is synced with the internet time once a week. During this week however, the time drifts a lot, by almost a minute or more in some cases. There are some tweaks you can do to ensure that the time syncs more often, but a simple way would be to shut down your PC every night. The time is synced every time it turns on. The DS3234 RTC however is extremely accurate due to its unique temperature-compensated crystal, and it is able to maintain time with minimal or almost no drift throughout the year without needing to be re-synced. A 3.4 million second drift test done using several RTCs including the DS3231 (same IC as DS3234, but uses an I2C interface) shows that the RTC is more accurate than its advertised accuracy over an extended period of time.

  • Lambert

    Sorry to bother again, DS3234.h here is not library used in datalogger, which library it is?

    • OSBSS

      You will need the DS3234 library. Blog post updated with the link. We will eventually merge functions for setting time in our library.

      If you encounter other problems, do let us know; these tutorials are a work in progress and can always be improved.

  • Mark

    Have you encountered where the day remains 25 and the minute stays 85 in the serial monitor regardless of what’s in the code? I’ve tried the Sparkfun breakout and a generic, on the DHT logger and TRH logger circuits, and using both this RTC and the DHT RTC code but I keep getting this problem and it’s leaving me puzzled. I was able to get it working right finally in the TRH circuit, but now I can’t get past this weird issue on the DHT circuit.

    • OSBSS

      This means the RTC isn’t being detected by the Arduino. If it’s working fine on the TRH circuit but not on the DHT circuit, there’s something wrong in the code. The SPI pins are set differently in the DHT 22 tutorial – make sure you configure them properly in the code. Also make sure some pins aren’t being shorted out or that there’s a loose connection somewhere.

  • Rodolfo Jimenez

    Please help. I encounter error in the time that this logger give it stuck in 1:15, 1:30, 1:45 to 2:00 then go back to 1:15, 1:30…and repeatedly in this pattern. What went wrong? When I deploy the loggers 10 unit of them 6 months ago it works properly then I encounter this problem.

    • OSBSS

      The DS3234 library we developed has a function that accounts for daylight saving time in US. This happens in March and November every year where the time is moved ahead an hour and back again.

      You may have used that in code. If you don’t live in US or don’t plan on using daylight saving time, you can comment out the function RTC.checkDST(); from your code. For example, comment out line 105 here:

      You can set the clock back to the correct time and remove DST from code and you should be good!

      • Rodolfo Jimenez

        thank you for the reply

  • Rodolfo Jimenez

    How can i use this function “fetchAndSetTime();” ? I want to automatically sync the time of my PC.
    Thank you