Quantcast
Viewing all articles
Browse latest Browse all 45

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, with this RTC breakout board.

 

 Image may be NSFW.
Clik here to view.
RTC

 

Hardware:

 

 

Arduino Pro Mini with FTDI-USB converter

RTC Real Time Clock Module

Jumper Wires

 

RTC module wired in the following way:

RTC               Arduino

VCC               VCC

MFP               unused

SCL               SCL

SDA               SDA

GND              GND

 

Included battery of the Real Time Clock module placed in its slot.

 

Software:

 

 

Arduino IDE with our updated RTClibrary for Arduino and AVR (check out the Real Time Clock Module page to download the library).

 

 

Setting time:

 

 

Image may be NSFW.
Clik here to view.
RTC

 

 

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.

 

Image may be NSFW.
Clik here to view.
RTC

 

 

Reading saved time:

 

 

Now open the other Arduino example, “BatteryRead”.

 

Image may be NSFW.
Clik here to view.
RTC

 

 

Disconnect the SDA/SCL wires, so that the RTC module can’t communicate with your Arduino board anymore.

 

Image may be NSFW.
Clik here to view.
RTC

 

 

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.

 

Image may be NSFW.
Clik here to view.
RTC

 

 

Open the Serial Monitor in Arduino, then reconnect the power supply to your Arduino board.

 

Image may be NSFW.
Clik here to view.
RTC

 

Image may be NSFW.
Clik here to view.
RTC

 

 

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!

 

 

 

 

Code explained:

 

 

Battery Set:

 

Initialization includes:

RTC.batteryEnable();

The RTC does not go on battery if VCC fails by default, in order to save it. This enables battery mode.

 

 

Battery Read:

 

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.

 

RTC.unflagBAT();

When power gets restored, a RTC flag has to be reset by software. Resetting it will erase the timestamps on the hardware side.

 

 

Conclusion:

 

 

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!


Viewing all articles
Browse latest Browse all 45

Trending Articles