Real Time Clock - DS1302
Intro: Real Time Clock - DS1302
STEP 1: The Background
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
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
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...
21 Comments
vijaycharless 7 years ago
can u pls say the value crystal oscillator
DaveW84 3 years ago
salvandongo 3 years ago
engr.kashif555 4 years ago
download flowcode v6 with fully cracked...
Stormy_Shock 5 years ago
thanks for this helpful information, but it will more helpful if you post active link. all the above link you posted are not found, please repost the link. thank you
Bluehawk99 6 years ago
I'm very new to all this but I need to ask a very basic question. What is used to interface the computer to the DS1302 chip in order to program it?
rafaelnfs 5 years ago
In thist case arduino Mega.
bohicarico 6 years ago
The links in the instructable no longer resolve. Does anyone have the script examples handy that they could elevate out to the cloud, like on box.com or example?
coltontshudy 6 years ago
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.
coltontshudy 6 years ago
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.
zonkerharris 6 years ago
It may be that you're doing something odd with the signals, which then causes the reset. I suggest that, if you are connecting and disconnecting the wires, that you always connect GROUND first, and disconnect GROUND last. If you are using headers, try to replace the ground pin with a longer (0.1" taller than the others) pin, so that this is reliable.
SoumyaP4 8 years ago
Current Date / Time: 0/0/2000 0:0:0
this is my output....plz suggest how to remove this error.
Gelfling6 7 years ago
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.)
PazzodiBorgo 7 years ago
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)
Gelfling6 7 years ago
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.
cts_casemod 8 years ago
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.
txdurk 9 years ago
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?
ghill2 9 years ago
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
Burgerjew21 9 years ago
Life Saver!!!! Thanks for this... was beginning to tear my hair out with constantly reset time.
AlexLPD 9 years ago
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.