DS1302 Real Time Clock

I have a sensor bundle for an Arduino Uno which includes a DS1302 Real Time Clock. The sketch that came with the kit does not compile. I have attempted to add a Time Structure to the sketch, but without success. I am curious to know if I need different library (.h) files for the DS1302 or ThreeWire.h to add to the sketch to make this work. Thank you.

I have the RtcDS1302.h library file, but the Arduino IDE is not recognizing this file. I have confirmed the location of this file in the library folder. This is the only file in the folder.

I am also interested in writing the date and time to an I2C SSD1306 128x64 OLED.

Any help would be appreciated.

I now have all of the Master Rtc library files from Makuna loaded in my libraries folder and attempted to use the code on the SunFounder page ( Real Time Clock Module (DS1302) — SunFounder Ultimate Sensor Kit documentation ) without success.

The IDI complier stops on the ThreeWire myWire(4, 5, 2); line and mentions something regarding the fact that myWire was not declared earlier in the script.

Any assistance would be helpful.

Can your UNO R3 upload other code?

If it can, this indicates that the UNO R3 is functioning normally.

Please check if you have installed the required libraries. You can refer to our tutorial link: DS1302 RTC Module Documentation.

Before uploading the DS1302 Real Time Clock code, make sure to use the Arduino Library Manager to search for “Rtc by Makuna” and install it, then upload the code and check its functionality.

If the upload fails, please provide us with the complete error message so we can assist you in analyzing and resolving the issue.

Hello,

Thanks for the reply.

I am compiling the attached clock.ini under IDI 2.3.6. I will be uploading this to a Mega 2560.

The snippet below is the libraries folder.

Below is the error message:

C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:45:11: error: expected initializer before ‘myWire’
ThreeWire myWire(6, 7, 5);
^~~~~~
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:46:26: error: ‘myWire’ was not declared in this scope
RtcDS1302 Rtc(myWire);
^~~~~~
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:46:26: note: suggested alternative: ‘Wire’
RtcDS1302 Rtc(myWire);
^~~~~~
Wire
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino: In function ‘void print_time()’:
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:56:26: error: conversion from ‘RtcDateTime’ to non-scalar type ‘Time’ requested
Time t = Rtc.GetDateTime();

C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:89:8: error: 'class U8X8_SSD1306_128X64_NONAME_HW_I2C' has no member named 'setCurstor'; did you mean 'setCursor'?
u8x8.setCurstor(2,2);
^~~~~~~~~~
setCursor
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino: In function 'void setup()':
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:131:12: error: expected unqualified-id before '.' token
ThreeWire.begin();
^
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:138:36: error: no matching function for call to 'Time::Time(int, int, int, int, int, int, int)'
Time t (2025, 8, 6, 16, 50, 10, 3); //initialize the time
^
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: Time::Time()
struct Time {
^~~~
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 0 arguments, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: constexpr Time::Time(const Time&)
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 1 argument, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: constexpr Time::Time(Time&&)
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 1 argument, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:140:20: error: no matching function for call to 'RtcDS1302<ThreeWire>::SetDateTime(Time&)'
Rtc.SetDateTime(t);
^
In file included from C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:14:0:
c:\Users\jbdie\Documents\Arduino\libraries\RtcDS1302/RtcDS1302.h:170:10: note: candidate: void RtcDS1302<T_WIRE_METHOD>::SetDateTime(const RtcDateTime&) [with T_WIRE_METHOD = ThreeWire]
void SetDateTime(const RtcDateTime& dt)
^~~~~~~~~~~
c:\Users\jbdie\Documents\Arduino\libraries\RtcDS1302/RtcDS1302.h:170:10: note: no known conversion for argument 1 from 'Time' to 'const RtcDateTime&'
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino: In function 'void loop()':
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:171:95: error: no matching function for call to 'Time::Time(int&, int&, int&, int&, int&, int&, int&)'
Time t (numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
^
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: Time::Time()
struct Time {
^~~~
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 0 arguments, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: constexpr Time::Time(const Time&)
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 1 argument, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate: constexpr Time::Time(Time&&)
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:34:8: note: candidate expects 1 argument, 7 provided
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:172:10: error: redeclaration of 'Time t'
Time t = rtc.getDateTime();
^
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:171:10: note: 'Time t' previously declared here
Time t (numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
^
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:172:14: error: 'rtc' was not declared in this scope
Time t = rtc.getDateTime();
^~~
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:172:14: note: suggested alternative: 'Rtc'
Time t = rtc.getDateTime();
^~~
Rtc
C:\Users\jbdie\Documents\Arduino\Real Time Clock\clock\clock.ino:182:27: error: conversion from 'RtcDateTime' to non-scalar type 'Time' requested
Time t = Rtc.GetDateTime();
~~~~~~~~~~~~~~~^~
exit status 1

Compilation error: expected initializer before 'myWire'

(Attachment clock.ino is missing)

Anything amiss on the line of code prior to the error line? E.g a missing semicolon?

Have you modified the code?
Please follow the tutorial we provided. The code in the tutorial has been verified. It uploads and runs correctly.

The code is as follows:

/*

This code initializes and sets up the RTC DS1302 module and prints the current date and

time on the serial monitor. It also checks if the RTC is running and has a valid date and time.

If not, it sets the RTC to the compile time.

Due to the time required for compilation and upload, there may be a time difference.

Board: Arduino Uno R3 (or R4)

Component: Real Time Clock Module (DS1302)

Library: https://github.com/Makuna/Rtc (Rtc by Makuna)

*/

#include <ThreeWire.h>

#include <RtcDS1302.h>

const int IO = 4; // DAT

const int SCLK = 5; // CLK

const int CE = 2; // RST

ThreeWire myWire(4, 5, 2); // IO, SCLK, CE

RtcDS1302 Rtc(myWire);

void setup() {

Serial.begin(9600);

Serial.print("compiled: ");

Serial.print(_DATE_);

Serial.println(_TIME_);

Rtc.Begin();

RtcDateTime compiled = RtcDateTime(_DATE_, _TIME_);

printDateTime(compiled);

Serial.println();

if (!Rtc.IsDateTimeValid()) {

// Common Causes:

//    1) first time you ran and the device wasn't running yet

//    2) the battery on the device is low or even missing

Serial.println("RTC lost confidence in the DateTime!");

Rtc.SetDateTime(compiled);

}

if (Rtc.GetIsWriteProtected()) {

Serial.println("RTC was write protected, enabling writing now");

Rtc.SetIsWriteProtected(false);

}

if (!Rtc.GetIsRunning()) {

Serial.println("RTC was not actively running, starting now");

Rtc.SetIsRunning(true);

}

RtcDateTime now = Rtc.GetDateTime();

if (now < compiled) {

Serial.println("RTC is older than compile time!  (Updating DateTime)");

Rtc.SetDateTime(compiled);

} else if (now > compiled) {

Serial.println("RTC is newer than compile time. (this is expected)");

} else if (now == compiled) {

Serial.println("RTC is the same as compile time! (not expected but all is fine)");

}

}

void loop() {

RtcDateTime now = Rtc.GetDateTime();

printDateTime(now);

Serial.println();

if (!now.IsValid()) {

// Common Causes:

//    1) the battery on the device is low or even missing and the power line was disconnected

Serial.println("RTC lost confidence in the DateTime!");

}

delay(5000); // five seconds

}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt) {

char datestring[20];

snprintf_P(datestring,

         countof(datestring),

         PSTR("%02u/%02u/%04u %02u:%02u:%02u"),

         dt.Month(),

         dt.Day(),

         dt.Year(),

         dt.Hour(),

         dt.Minute(),

         dt.Second());

Serial.print(datestring);

}

Before uploading the code, please install the library by using the Arduino Library Manager and searching for “Rtc by Makuna” to install it.