Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

DS3234 Example Software

Summary and overview

This tech note is an update of similar content originally published in 2013. The software previously referenced by the original author has become unavailable. This tech note references four software packages, three of which are available from Github. The objective of the original tech note and this one is to provide software examples for interfacing the Maxim DS3234 Real Time Clock chip to Arduino.

The software examples and accompanying libraries enable setting the date and time and reading the temperature from the sensor built in the DS3234 that controls integrated temperature-compensated crystal oscillator. Alarm functionality is present in the fourth demo set, which is the most complex. You may want to delve into the various .h and .cpp libraries because the embedded notes will tell you what features have been implemented. In addition, check the Serial.begin(XXXX) method in each sketch to get the baud rate of the serial monitor and set your serial monitor to that rate to avoid receiving unrecognizable characters. A number of the examples use different rates. Additionally, the first three use Pin 8 as the chip select and the last family of examples uses Pin 10 (unless you change it).

I have tested all four software packages using the DeadOn DS3234 breakout board, available from SparkFun and others, on my Arduino Mega 2560 and 101. There is every reason to believe that the software packages will function correctly on the AVR devices described in the third reference.

The Arduino 101 uses much different technology than the Mega. I had no expectations that the software would even function at all on the 101 since all but one of the sketches were written well before the 101’s introduction. Example sketches ran variably on the 101. In one case it ran “better,” but it did not run all sketches.

It is important to recognize that the SPI hookup for the DS3234 device will vary based on the type of Arduino board you are using. A good reference for SPI operation and connection of the DS3234 can be found at the SPI reference page at the Arduino.cc site: https://www.arduino.cc/en/Reference/SPI.

There was a slight difference in configuration between the Mega and the 101 in addition to Pin choices. I operated the clock at 3.3 volts since the 101 had the feature to send and receive 3 volt signals. For the Mega I operated the clock at 5 volts because that is possible. I did not want to introduce the added complexity of a Texas Instruments TXB104 level shifter, although that would have been a good solution as well.

There is a high level explanation of the DS3234 here: http://tronixstuff.com/tag/ds3234/.

1. Sparkfun DS3234 test example

The first software package you might want to try is this one from SparkFun: https://github.com/sparkfun/DeadOn_RTC/blob/v1.1/Libraries/DeadOn_RTC/examples/set_and_read_time/set_and_read_time.ino. The output, which displays on the Arduino IDE’s serial monitor, is intended only to test your connections and to determine if your DS3234 is live. In other words, the date and time are not intended to be meaningful. However, you can change the parameters on line 8 to reflect the current time and date, and the output will be meaningful. The clock will be initially set with this value and update the time based on its internal crystal. (Note, I added 10 seconds to the “real time” and then compiled and uploaded. It is a good approximation of accurate time.)

This test example functioned properly on both the Mega and 101.

The next three software packages are likely not to be compatible with one another. In other words, it is a good idea to have only on installed in the Arduino IDE’s library, although I did not try having multiple at the same time. Some of the libraries share overlapping file names, but the content could be different. I kept an “archived” copy of each in a convenient folder and moved the various libraries in an out for my testing.

2. Basic clock examples

The second of the example libraries is from a forum participant who was an active in the past. Although the software is old, it is mature and has interesting examples: https://github.com/maniacbug/RTClib. The two examples worked perfectly with one minor exception as explained below.

DS3234.PDE

The operation of example ds3234.pde is explained by Mac Lane here: http://www.l8ter.com/?p=375. (Note: You will need to change the “pde” to “ino” if you are running Processing* on your computer.) I noticed a difference in operation between the Mega and the 101.

I was not successful in getting the date and time to set automatically for the Mega. The output always came up starting at the value at which the clock was initialized to 1.01.1980 by the accompanying software library. I think that was because my clock always started running, and the intent of the test on line 23 is to update the time and date if the clock is not running. I tested the update routine (line 23) by removing the “not” (!), and the clock updated based on the time and date of sketch compilation. It turns out that Marc Lane discovered the same issue for the DS3231 and offered a solution here: http://www.l8ter.com/?p=417. He replaced the test for clock not running with a test based on Unix time. Here is the replacing code, including the last two lines which are not changed but are included for navigation.

    DateTime now = RTC.now();
    DateTime compiled = DateTime(__DATE__, __TIME__);
    if (now.unixtime() < compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

In the case of the 101, the unmodified software functioned perfectly. The clock display showed the proper time when the display started on the serial monitor.

“* Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts.”

SoftRTC

SoftRTC initializes and updates the clock with the date/time that the sketch was compiled. It uses the Millis code near the end of RTClib.c and uses the Arduino millis() command.

This test example functioned properly on both the Mega and 101.

3. Fully featured clock examples done a different way and accessing internal memory to store and retrieve

The third of the example libraries is from Henning Karlsen’s Rinky Dinky Electronics and can be downloaded from the landing page: http://www.rinkydinkelectronics.com/library.php?id=71. All three of the examples operated without a flaw. There is a lot of very good program documentation in the author’s reference manual. The software makes use of an AVR library; therefore, it is not a surprise that each of the examples resulted in a compilation failure for the 101.

RAM Demo

Write and read from the internal battery backed-up RAM and display results on the serial monitor.

Serial Easy

Update the clock based on compilation date/time and output time and temperature of the crystal The clock is initialized to 01.01.2000, but there is a section of the sketch that is commented out that can update the clock to real time if it is activated.

Serial Hard

Update the clock based on compilation date/time and output day, date, time and temperature of the crystal. The clock is initialized to 10.01.2000, but there is a section of the sketch that is commented out that can update the clock to real time if it is activated.

4. Fully featured clock examples and alarm implementation

The fourth of the libraries is from Github and is courtesy of Petre Rodan who has recently updated the software: https://github.com/rodan/ds3234. The examples in the Rodan library all use Pin 10 as the DS3234 chip select pin. You can optionally change this to pin 8 to make your wiring consistent with the first three libraries. Also, you will want to read the README file the note in config.h, and the embedded notes in the various libraries.

The library contains three example sketches. All of the sketches ran properly in both the 101 and Mega. My 101 acted “balky” for the second sketch. On occasion I received a “port not found” error message after the sketch uploaded; however, either the output displayed anyway or I just re-downloaded.

Clock and temperature

The first example performs the basic clock and temperature functions. This example exposes more clock features than the previous examples in 1, 2 and 3 above and is, thus, more complex. This particular sketch has been recently updated. The author has instructed that this is the “time set” for the second and third example sketches, and it must be run before the other two, even if the real time clock has already been initialized by another sketch and has the correct time.

The “time set” string is input via the console. Note the uppercase T begins the string per this rule: TssmmhhWDDMMYYYY. Here is the author’s example: T431821107092016.

User-settable alarm control with continuous time and debug information output

The second example in this library has alarm control. When the alarm is activated, the SQW line is pulled low by the chip. (Note: The open drain SQW line is normally pulled high by a 10K ohm pull-up resistor on the DeadOn board.) According to Maxim literature, “The INT/SQW pin either generates an interrupt due to alarm condition or outputs a square-wave signal and the selection is controlled by the bit INTCN.” ITCN is located on bit 2 of the register at address 0E per the datasheet. The author has initialized this bit to 1 (see ds3234.cpp).

Just to emphasize, the clock is not initialized based on the compilation date/time and needs to be initialized prior to using this example.

Repeating alarm with continuous time and debug information output

The third example in this library has an alarm that repeats on a 5 minute interval and the pulls the SQW line low when the alarm is activated. I did not test the alarm function itself.

Just to emphasize, the clock is not initialized based on the compilation date/time and needs to be initialized prior to using this example

I found the three software libraries on Github and one other as a result of web searching. But you are encouraged to shop for others. There is also software on Marc Lane’s page, but I was not able to get that to work correctly, possibly because of the change in Arduino IDE standards.

Victor Dolcourt?