January 16

Jewelry Box: Keeping Time with the RTC Module

The first part of this project is to test the most problematic electronic: the RTC module. If it cannot maintain time, this part of the project could put everything else on hold.

Before I can start testing the module, I’m going to need the library for the Arduino IDE. There are a few of them out there, and some are specific to DS3231 clones which will not work with my DS3231 RTC module, but I found one that is referenced as a source for their git branch. I’m using the library from Rinky-Dink Electronics, which also provides a datasheet, but most importantly, a manual for their library.

The SainSmart Uno R3 (clone of the Arduino Uno R3) is what I’m using to test the DS3231 RTC module. Why the Uno R3 instead of the Pro Mini? Because the more I look into this project, the more I think I’m going to need the capabilities of the Uno R3 chipset, than the one provided by the Pro Mini. This is yet to be determined, but I’ll know soon enough, when I start to write my sketch.

 

Wiring

Wiring is the simplest thing. I use the red wire for power, and black wire for ground; plugged into 5V and GND, respectively.  SDA (Serial Data, yellow wire) is on pin A4 (Analong pin 4), and SCL (Serial Clock, green wire) is on pin A5 (Analog pin 5). This is all the DS3231 RTC module requires for power and communication.

 

Coding

Do You Have the Time?

Having obtained the DS3231 library, and extracted into my ~/sketchbook/libraries/ directory, I will open the Arduino IDE, and load the example it provides. With the “DS3231_Serial_Easy” sketch uploaded to the Uno R3, I can launch the Serial Monitor, and view the data being reported by the DS3231 RTC module. As it has yet to be set with the current time stamp, it could report “January 1, 1970 00:00:00” (epoch zero), or a different arbitrary date set by the manufacturer. Using the Serial Monitor, and setting the baud rate from the default 9600 to 115200 (set in the sketch), I can see the time stamp being produced by the DS3231 RTC module. In my case, it seems to have been set to “January 1, 2000 00:00:00” by the manufacturer.

Default configuration has the time set to January 1, 2000, 12:00AM

I updated the example code from “delay(1000)” to “delay(5000)”; 1000ms (1 second) to 5000ms (5 seconds). The next part is to update the time stamp, and that’s the tricky part. When uploading a sketch, it goes through a verification step before uploading. This can throw off the seconds, albeit a minuscule amount, so I’m not going to worry about having the exact time. If I wanted to add Internet access to this project, I could set the time from NTP servers, and not worry about manually setting the time.

Now Now, or the Now From Then?

As mentioned, I’m not going to have the exact time in seconds, but I can still get close. I’ll accomplish this by setting the time to the upcoming minute, with zero seconds, and upload the sketch once the minute arrives. This seems to put me off by no more than 3 seconds, and that’s acceptable, considering that I’ll be counting days. If this were a time-keeping project, requiring the exact time, I would be connecting to NTP servers. The code needed to set the time, which needs to be uncommented, is as follows:

// The following lines can be uncommented to set the date and time
//rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014

The first thing is Day Of Week (DOW), in ALL CAPS. Next is the current time, in 24-hour format (0-23), minutes, and seconds. Lastly, the date in day, month, year. If the date is not entered correctly, such as “month, day, year” format, it will not update the date. The code will need to be updated and uncommented for the Uno R3 to process it, and set the current time on the DS3231 RTC module.

 

// The following lines can be uncommented to set the date and time
rtc.setDOW(MONDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(10, 41, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(16, 1, 2017); // Set the date to January 1st, 2014

Time Traveling Without a TARDIS

The sketch was successfully compiled, and then uploaded. To verify that it went through successfully, I launch the Serial Monitor, and make sure it’s displaying the information I provided in the sketch.

Updated configuration has the time set to January 16, 2017, 10:41AM

SUCCESS! It is reporting the information that I provided in the sketch, and it is updating every 5 seconds, per “delay(5000)”. This shows that it will continue to update after having a date and time provided, but what matters is if it will continue to follow through, even if it is unplugged from the Uno R3, and removed from all external connections; will the coin battery do its job?

Only Time Will Tell

I removed the DS3231 RTC module from the breadboard, and left it aside for 2 hours. This time, I went back into the sketch to comment out the code used to set the time; using the default example, with my 5-second delay again. The sketch uploaded successfully, but now, the important part: did the battery keep the time going on correctly?

Time continues to be updated consistently

SUCCESS! This was a total success, and now, I can continue on with the other parts of this build, ensuring that I am able to piece it together with the other components.

Tags: , , ,
Copyright 2025. All rights reserved.

Posted 2017-01-16 by Draik in category "Arduino", "Jewelry Box