Real Time Clock Module and saving time with Arduino
We made it easy for you to use our Real Time Clock Module!
We updated the existing RTC library to support this new module with extended functionalities.
3 Arduino code examples are included in the library.
This tutorial will detail how to keep track of time without power supply using this RTC breakout board.
RTC module wired in the following way:
Included battery of the Real Time Clock module is placed in its slot.
Arduino IDE with our updated RTClibrary for Arduino and AVR (check out the Real Time Clock Module page to download the library).
Download and put the updated RTClib in your Arduino libraries folder. Start Arduino, and open the RTC BatterySet Arduino example from the RTClibrary like you would with any other example.
Locate this line in the setup():
RTC.adjust(DateTime(__DATE__, __TIME__, 6));,
And replace “6” with the current day of the week (Monday being n°1).
Select your correct board and COM port, and upload the code to your Arduino board!
Once done, open the Serial Monitor to check that the RTC has the current time set correctly.
Reading saved time:
Now open the other Arduino example, “BatteryRead”.
Disconnect the SDA/SCL wires, so that the RTC module can’t communicate with your Arduino board anymore.
Upload the BatteryRead code to your Arduino Board
If you open the Serial Monitor, you will now see completely inaccurate time. It is normal since the SDA/SCL lines are disconnected.
Now totally remove power supply from the Arduino board.
Then reconnect the SDA/SCL lines.
Open the Serial Monitor in Arduino, then reconnect the power supply to your Arduino board.
The MCP79401 chip stores timestamps of when it went on Battery and when it got VCC power restored.
We print those readings (note that they do not support seconds).
And then we print the current time, accurately saved on battery by the Real Time Clock module, which is now running back on VCC!
The RTC does not go on battery if VCC fails by default in order to save it. This enables battery mode.
DateTime fail = RTC.powerFailed(); DateTime restore = RTC.powerRestored();
These 2 functions read the 2 RTC timestamps, indicating the times at which the RTC went on battery mode (powerFailed), and at which it went back on VCC supply (powerRestored).
Please note that the MCP79401 does not save seconds for those timestamps, so these functions can only return the minutes at which power failed or was restored.
When power gets restored, a RTC flag has to be reset by software. Resetting it will erase the timestamps on the hardware side.
This is one pretty neat Breakout Board with the powerful MCP79401 RTC Real Time Clock chip. It accurately keeps track of time, and can do so for more than 5 years while running on battery!
Additionally, it can be used to know times at which power failures occur!
You can find additional information about the library we made for that Real Time Clock Module in the Library Wiki.
You can find it on the product page.
Check out the other tutorial for this Real Time Clock Module: using alarms!
This article was published by the Jaycon team. Learn more about how we can take your product design and hardware idea to the next level here.