Real Time Clock - DS1302

177,161

70

15

Posted

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...

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

15 Comments

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?

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.

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.

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.

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

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

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.)

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)

can u pls say the value crystal oscillator

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.

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.