I want to build a clock, a precise one. For this purpose I got a Chronodot – extremely accurate and temperature compensated and battery backed real time clock module, based on the DS3231. The sample code on the website only shows how to read the time from the clock using Wire library. There is no info how to connect the thing to the Arduino. There is no code to show how to set the time. After reading the documentation and searching on-line I figured it out.
Chronodot uses I²C bus to talk to the outside world. Arduino accepts I²C connections on two analog intput pins. On most boards, SDA (data line) is on pin A4, and SCL (clock line) is on pin A5. Those are analog lines, not digital. So, connect pin A4 to Chronodot’s pin labeled SDA, and Arduino’s pin A5 to Chronodot’s pin SCL. Of course, +5V goes to VCC and GND to GND.
Based on the Time library and the DS3231 datasheet I wrote a library to talk to Chronodot, and a sketch with a Python script to set the time. Both are available in my git repository: Chronodot library, ChronodotSet sketch.
Load the ChronodotSet.pde into your Arduino and leave the unit connected to the PC. The set.py Python script will set the date and time over serial connection to either local PC time, time from a string, or time from NTP server (depends on Python serial library pySerial and NTP library for NTP functions). The time is rounded to the nearest second.
Without any options it sets the time to the local PC time and reads back the time reported by Arduino 5 times.
$ ./set.py -h
Usage: set.py [options]
Options:
-h, --help show this help message and exit
-d DEVICE, --device=DEVICE
-s STRING, --string=STRING
-n, --ntp
--noreadback
$ ./set.py
Setting time from local time
Time has been set to 2011-01-16 18:31:38.503983
Reading time back
2011-01-16 18:31:38
2011-01-16 18:31:39
2011-01-16 18:31:40
2011-01-16 18:31:41
2011-01-16 18:31:42
$ ./set.py --device /dev/tty.usbmodem411 --string "2010-02-28 23:59:57"
Setting time from string 2010-02-28 23:59:57
Time has been set to 2010-02-28 23:59:57
Reading time back
2010-02-28 23:59:57
2010-02-28 23:59:58
2010-02-28 23:59:59
2010-02-29 00:00:00
2010-02-29 00:00:01
$ ./set.py --ntp --noreadback
Setting time from NTP server
Time has been set to 2011-01-16 18:33:39
Have fun. I’m getting to work on 7-segment displays.