Introduction: Real Time Clock - DS1302

This is a VERY simple tutorial on how to use the DS1302 Real Time Clock with an Arduino, or Versilino, board.  I will walk you through the wiring, code, and example.  All of which can be found at www.Virtuabotix.com.  This is my ifrst try at creating an instructable, so I welcome any feedback that anyone has, and I hope you enjoy it.  So let's get this started...

Step 1: The Background

The DS1302 trickle-charge timekeeping chip contains a real-time clock/calendar and 31 bytes of static RAM.  It communicates with a microprocessor via a simple serial interface.  The real-time clock/calendar provides seconds, minutes, hours, day, date, month, and year information.  Only three wires are required to communicate with the clock/RAM: CE, I/O (data line), and SCLK (serial clock). Data can be transferred to and from the clock/RAM 1 byte at a time or in a burst of up to 31 bytes. The DS1302 is designed to operate on very low power and retain data and clock information on less than 1μW.  The DS1302 has dual power pins, one for primary and another for backup.  in this example the Priamry power will be provided by the Arduino, and the back up by a CR2032 battery.

The 31 bytes of RAM is formatted as shown in the table below.  Notice that the numeric positions have their own bits to fill.  Specifically the 0-9 value of the 'ones' place have 4 bits, and the 0-9 value of the 'tens' place has 3 bits for the byte filled with the seconds value.  These values are combined to create the entire byte.

This product, as well as the manufacturers datasheet, can be found at https://www.virtuabotix.com/product/virtuabotix-ds1302-real-time-clock-rtc-module-arduino-versalino/.

Step 2: Parts Selection

The parts were chosen based on cost, availibility, and compatibility.  This instructable utilized the above parts:

Step 3: Wiring Setup

Step 4: Code

The original code was provided from the Arduino website, http://playground.arduino.cc/Main/DS1302, which was contributed by user "Kodal". That code was translated into a full library, and expounded upon by Virtuabotix, LLC. The full description of the library, and its functions, can be found at https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/. Simply go to the 'Code' Section to find the link to the library.


This Instructable will address two examples that utilize the library.

*NOTE: The library does NOT use the full functionality of the DS1302 chip, it does use the most common functions that are requested.

Step 5: Code (Example 1)

This example simply allows for printing out the data, and allows for direct user access to the individual elements. Thus printing each element at a time. This, of course, can be printed out in any format, this one just seemed the easiest. As always, this example can be turned into a function..

This example is included in the library that can be found at: The full description of the library, and its functions, can be found at https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/. Simply go to the 'Code' Section to find the link to the library. Once the library is imported, you can find the example in File->Examples.

Step 6: Code (Example 2)

This example utilizes two Superbright LEDs to indicate whether your RTC is actively updating. This is illustrated by wither a Green LED, for good; or a RED LED for not updating. This is simply accomplished by storing a read of the seconds and checking that against the current RTC time. If it has updated you get a Green LED illuminated, if not you get the Red LED.

This example is included in the library that can be found at: The full description of the library, and its functions, can be found at https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/. Simply go to the 'Code' Section to find the link to the library. Once the library is imported, you can find the example in File->Examples.

Step 7: Conclusion

This project was a vast learning experience.  There were a unimaginable amount of resources out there, but very few of them were actually helpful.  I hope this library, linked above, is helpful and I look forward to any input for future projects and feedback.  If you are attempting to use this Instructable and need any assistance contact me, and I will do my best to help.  Good Luck!!

Step 8: Demo

This video has a Demo of both Examples:

As before the examples and the library can be found at https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/....

Thanks for viewing my first Instructable...

Comments

author
coltontshudy made it!(author)2017-07-08

I'm having an issue with the DS1302 module where it reliable keeps the time when disconnected from the arduino after the arduino has been powered off with the module connected, but loses the time when the arduino is operating and the module's Vcc is unplugged from the arduino. Any possible solutions? I'm using the code provided in Step 5.

author
coltontshudy made it!(author)2017-07-08

Also, it seems that while the arduino is operating, if the Vcc and ground are disconnected, the clock still functions; it's only when the Vcc is reconnected that it resets the clock. Weird stuff. If anyone knows the solution to this, that would be greatly appreciated.

author
SoumyaP4 made it!(author)2016-03-15

Current Date / Time: 0/0/2000 0:0:0

this is my output....plz suggest how to remove this error.

author
Gelfling6 made it!(author)2016-07-09

At what voltage are you applying to VCC? despite most SPI/I2C devices running at +3.3V, I found the DS1302 runs only at 5V.. (tried 3.3 on an arduino, just read the same as You're getting.)

author
PazzodiBorgo made it!(author)2017-04-02

DataSheet says 2V-5,5V. However, the "setDS1302Time" only works with 5V, not with 3.3V. But then the DS1302 Chip gets very hot in a few seconds. All not producing much confidence. Maybe a crappy batch(gotmine also off EBay)

author
vijaycharless made it!(author)2016-10-05

can u pls say the value crystal oscillator

author
Gelfling6 made it!(author)2016-07-09

I've found the Virtuabotix library to work with just about ANY of the RTC modules found on EBay. Even one with 10K resistors across the RST/CE , I/O & CLK pins.)

I had to borrow the battery from one I got from eBay a few months back, (no-name board, just the basic DS1302 chip/crystal/battery.

author
cts_casemod made it!(author)2016-01-27

How accurate are these clocks for you guys? I have something like 10 seconds difference every day!

Changed to a DS3231 and have one working since August with 2 seconds delay.

author
txdurk made it!(author)2014-12-25

Hmm, not sure what I'm overlooking but example 1 compiles but never sets the time or gets it. 9 out of ten failures are usually cable related. The instructions say only three lines are needed (I presumed since it got power from the battery) but I see Vcc & Gnd wired up (I think). Which is it?

author
ghill2 made it!(author)2014-08-21

This is great so simple. Just thought I would add that to keep the time stored in the DS1302 comment out the line :

myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);

Took me a while to work that one out

author
Burgerjew21 made it!(author)2014-11-18

Life Saver!!!! Thanks for this... was beginning to tear my hair out with constantly reset time.

author
AlexLPD made it!(author)2014-11-17

Hi, I just came to your instructablem to thank you, the Library works great, is quite simple and easy to use, I have strugling with the original DS1302, so install this one and voila, in minutes I have a Functional sketch.

Please note, in the examples the funcion;

virtuabotixRTC myRTC(6, 7, 8); is use to set the pins of th RTC... they are in this order; SCLK, I/O, CE. (In case need to be changued)

One you use the function;

myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);

You alreade set the timer, so in next projects no need to set it again.

The only need to be done is to call the function;

myRTC.updateTime(); in the main loop. After this the myRTC.Hour,minute, second... all will retrive the ritgh date and time, since the clock set.

Just leave this here for anyone can use it.

Thanks again.

Kind Regards.

Alejandro Santiago.