Page 1 of 4

DS1302 Real Time Clock Module (HCMODU0035)

Posted: Sun Sep 08, 2013 10:23 am
by admin
Image




The DS1320 RTC is a compact real time battery backed clock module capable of accurately keeping the current time and date. It includes a 24 hour clock consisting of hours, minutes & seconds and a calendar which stores the current year in 2 digits, month and date. The calendar is also capable of correcting for months with different amounts of days and leap years. Additionally there is a day of the week counter to keep track of the current date.

Besides keeping track of the time and date the module also includes 31 bytes of RAM that is free to be used by the end users application. This makes it useful as an additional memory storage area or to store temporary data in applications where the microcontroller is reset or placed into deep sleep modes.

The module includes a CR2032 button cell which maintains the clock and RAM when power is removed from the modules header.


EXCLUSIVE LIBRARY:
For Arduino users we have written an exclusive library (HCDS1302) that gives access to these features. See link at the bottom of this post.



FEATURES:
Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year with Leap-Year Compensation Valid Up to 2100
31 x 8 Battery-Backed General-Purpose RAM
Serial I/O for Minimum Pin Count
2.0V to 5.5V Full Operation
Uses Less than 300nA at 2.0V
Single-Byte or Multiple-Byte (Burst Mode)
Data Transfer for Read or Write of Clock or RAM Data
Simple 3-Wire Interface
TTL-Compatible (VCC = 5V)
Optional Industrial Temperature Range: -40°C to +85°C
DS1202 Compatible


PINOUT:
VCC……………..2 to 5.5V
GND………..…...GND
CLK/SCK……Clock
DAT/IO………..Data
RST/CE………..Chip enable

Note pin order may vary




Example sketches:

Set/Read DS1320 Example

Set and read back example:

This sketch demonstrates how to set and read back the DS1302s date
and time.

Please see Licence.txt in the library folder for terms of use.
  1. #include "HCDS1302.h"
  2.  
  3. // Define the pins connected to the DS1302
  4. #define CLK_PIN 4
  5. #define DAT_PIN 3
  6. #define RST_PIN 2
  7.  
  8. // Create an instance of the library
  9. HCDS1302 RTC(CLK_PIN, DAT_PIN, RST_PIN);
  10.  
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.  
  16.   // Set the DS1302s time to 16:30:00
  17.   RTC.setTime(16, 30, 0);
  18.  
  19.   // Set the DS1302s calendar to 7th Feb, 2024
  20.   RTC.setDate(24, 2, 7);
  21.  
  22.   // Set the DS1302s day of the week
  23.   RTC.dow(3);
  24. }
  25.  
  26.  
  27. void loop()
  28. {
  29.   // Read the current date & time
  30.   RTC.update();
  31.  
  32.   // Get the last read time
  33.   byte seconds = RTC.seconds();
  34.   byte minutes = RTC.minutes();
  35.   byte hours = RTC.hours();
  36.  
  37.   // Get the last read date
  38.   byte date = RTC.date();
  39.   byte month = RTC.month();
  40.   byte year = RTC.year();
  41.   byte dow = RTC.dow();
  42.  
  43.   // Print out the results
  44.   Serial.print("Hours: "); Serial.println(hours);
  45.   Serial.print("Minutes: "); Serial.println(minutes);
  46.   Serial.print("Seconds: "); Serial.println(seconds);
  47.   Serial.print("Year: "); Serial.println(year);
  48.   Serial.print("Month: "); Serial.println(month);
  49.   Serial.print("Date: "); Serial.println(date);
  50.   Serial.print("DOW: "); Serial.println(dow);
  51.   Serial.println();
  52.  
  53.   delay(1000);
  54. }

Formatted date and time example

Print a formatted date and time example:

This sketch demonstrates how to print out the current date and time
as a formatted string.

Please see Licence.txt in the library folder for terms of use.
  1. #include "HCDS1302.h"
  2.  
  3. // Define the pins connected to the DS1302
  4. #define CLK_PIN 4
  5. #define DAT_PIN 3
  6. #define RST_PIN 2
  7.  
  8. // Create an instance of the library
  9. HCDS1302 RTC(CLK_PIN, DAT_PIN, RST_PIN);
  10.  
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15. }
  16.  
  17.  
  18. void loop()
  19. {
  20.   // Create a string to print.
  21.   // YY will be replaced by the year
  22.   // MM will be replaced by the month
  23.   // DD will be replaced by the date
  24.   // hh will be replaced by the hour
  25.   // mm will be replaced by the minutes
  26.   // ss will be replaced by the seconds
  27.   char exampleString[] = "Date: DD/MM/20YY Time: hh:mm:ss";
  28.  
  29.   // Read the current date & time
  30.   RTC.update();
  31.  
  32.   // Insert date and time into the string
  33.   RTC.dateTimeString(exampleString);
  34.  
  35.   Serial.println(exampleString); Serial.println();
  36.  
  37.   delay(1000);
  38. }
  39.  

Write and read from RAM Example

Writing to and reading from the DS1302s RAM:

This sketch demonstrates how to write to and read back from the
DS1302s 31 bytes of RAM

Please see Licence.txt in the library folder for terms of use.
  1. #include "HCDS1302.h"
  2.  
  3. // Define the pins connected to the DS1302
  4. #define CLK_PIN 4
  5. #define DAT_PIN 3
  6. #define RST_PIN 2
  7.  
  8. // Create an instance of the library
  9. HCDS1302 RTC(CLK_PIN, DAT_PIN, RST_PIN);
  10.  
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.  
  16.   // Write 0xAA to the 31 bytes of RAM
  17.   for(byte address = 0; address <= 30; address++)
  18.     RTC.ram(address, 0xAA);
  19.  
  20.  
  21.   // Read back the RAM and print it out
  22.   for(byte address = 0; address <= 30; address++)
  23.   {
  24.     Serial.print(RTC.ram(address), HEX);
  25.     Serial.print(" ");
  26.   }
  27. }
  28.  
  29.  
  30. void loop()
  31. {
  32.  
  33. }


Downloads:
DS1302.pdf

ARDUINO HCDS1302 LIBRARY:
The HCDS1302 library can be downloaded from the software section of this forum here:

viewtopic.php?f=58&t=3095&p=8713


Diagrams, libraries, and example code are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Nov 28, 2013 1:34 pm
by jackbell16
Hi,
how should I connect this RTC to Arduino Uno ?

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Fri Nov 29, 2013 11:21 am
by andrew
Connection to an Uno is as follows:

MODULE ARDUINO
GND -> GND
VCC -> +5V
SCK -> D4
I/O -> D3
RST(CS) -> D2

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Dec 12, 2013 3:42 pm
by normski001
can you advise how i connect this to my HCARDU0036 rev 3 mega AT2560-16AU please
i am having a problem finding pin 2, 3, and 4

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Dec 12, 2013 7:05 pm
by andrew
You should be able to connect it as per Uno pinout above

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Dec 12, 2013 7:11 pm
by normski001
andrew wrote:You should be able to connect it as per Uno pinout above
i do not have d2, d3, d4 pins
here is my board
http://www.hobbycomponents.com/images/f ... pinout.jpg

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Dec 12, 2013 7:22 pm
by andrew
Ok I see the confusion D2, D3, & D4 refer to the digital pins that are just labeled 2,3, & 4 on your board where it says PWM

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Thu Dec 12, 2013 7:56 pm
by normski001
i have just plugged the screen in them the outer holes 2 and 3 seem connected but not 4 on the display shield board
the 5v and gnd is also connected to outer holes on board.
the board and holes seem to be 2, 3, 11, 12, 13 pins connected to the empty display shield holes so no idea how i get to 4 for the rtc

on the lcd display i have D0 to D7 where i can putpins and d4 does go to the pin 4 you said, could i fix it there any advice would be help full please

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Fri Dec 13, 2013 11:23 am
by andrew
As you have a Mega would it be easier for you to use some of the other spare digital pins? Digital pins numbered 14 to 53 are not covered by the shield and so all you would need to do is change the pin assignment in the sketch. For example, in the example sketch in the first post find the following lines...

#define SCK_PIN 4
#define IO_PIN 3
#define RST_PIN 2

...and change the numbers to what ever digital pin numbers you use on your mega. Is this a solution or do you need to connect to the shield for some reason?

Re: DS1302 Real Time Clock Module (HCMODU0035)

Posted: Fri Dec 13, 2013 1:41 pm
by normski001
here is what i have done, gnd and +5v from top and bottom of digital pin block right of board, sck to pin 22 i/o to pin 24 rst to pin 26
/* Define the DIO pins used for the RTC module */
#define SCK_PIN 22
#define IO_PIN 24
#define RST_PIN 26
when i load program above i get error
DS1302 rtc(RST_PIN, IO_PIN, SCK_PIN); this line goes yellow


below box goes orange and says DS1302 does not name a type
then lines in black box
sketch_dec13b:46: error: 'DS1302' does not name a type
sketch_dec13b.ino: in function 'void setup()':
sketch_dec13b:51: error: 'rtc' was not declared in the scope
sketch_dec13b.ino function 'void loop()':
sketch_dec13b:64: error: 'rtc' was not declared in the scope
sketch_dec13b:64: error:'MONDAY' was not declared in the scope