Hello everyone! I made a clock using RTC1307 module and as it doesn't automatically autocorrect time I have to correct the time . My current time and date is set to 12:00 AM 0/0/1970. I want to know how to set it to present time

/*************************************************************/
/Libraries/
#include <Adafruit_SH1106.h >
#include <Adafruit_GFX.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
/Global Var/
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
bool i;
int x=0;
tmElements_t KT;

void setup() {
display.begin(0x3C);
display.clearDisplay();

}

void loop() {
display.clearDisplay();
RTC.read(KT);
x=KT.Second%02u;
display.setTextSize(1);
display.setTextColor(WHITE);
display.setFont(&FreeSansBold9pt7b);
/Prints the Time/
display.setCursor(0,20);
Kotobuki(KT.Hour);
if(x==0)
display.print("");
if(x==1)
display.print(":");
display.setCursor(25,20);
display.print(KT.Minute);
if(x==0)
display.print("");
if(x==1)
display.print(":");
display.setCursor(50,20);
display.print(KT.Second);
display.setCursor(65,20);
if(i==true)
display.print(" AM");
if(i==false)
display.print(" PM");
/Prints the Date M/D/Y Format/
display.setCursor(0,40);
display.print(KT.Month);
display.print('/');
display.print(KT.Day);
display.print('/');
display.print(tmYearToCalendar(KT.Year));
display.display();
}

void Kotobuki(int Hour){
if(Hour==00){
Hour=12;
i=true;
display.print(Hour);
}
else if(Hour>00 && Hour<12){
i=true;
display.print(Hour);
}
else if(Hour==12){
i=false;
display.print(Hour);
}
else if(Hour>12){
Hour=Hour-12;
i=false;
display.print(Hour);
}
}

You might have the moderators move this post to a better suited forum.

Typically, I'd run another routine to set the RTC's time. I use ESP32's. I'd have the ESP32 get the time from the NNTP server, use that time to set the RTC and be done with that sketch. Then I'd move onto doing the real thing.

What will be your source for setting the time?

Do you have a battery installed on the RTC?

In case you do not have it here is how to manually set the time for the RTC you are using from the example sketches included with the library.

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

Sir, I have a battery on the RTC. I'm new to Arduino as well. This was my first Arduino project. Can you please tell me how to correct the time ? I have a battery in the RTC .

I'll try.

In the Arduino IDE go to File | Examples | DS1307RTC and select the Set Time sketch. Upload the Sketch to your microcontroller. After the upload, you should see some message on the monitor about success or failure in regards to setting the time.

Sir , there isn't a DS1307RTC folder in examples .

Somewhere there should be an example folder for the DS130RTC library you are using in your sketch.

The code posted in #2 sets the DSthiniges time, did you try that code?

I found it sir and I uploaded it just now but still, it's the same

Is it inserted the right polarity (not upside down)? Please post a wiring diagram of your circuit.

What was the message you got on the monitor when you tried?

It said done uploading but nothing happened and I tried the codes you gave and there was an error that one said

exit status 1

'tmElements_t' was not declared in this scope

Yes sir it is placed correctly. I did it the way the YouTube tutorial said

I'm sorry, but YouTube does not give me any confidence in what you did. But at least you checked it. Where is the wiring diagram I asked for?

If the code and the whole project really comes from a YouTube project, though, you should provide a link to it.

What message did the code in the examples that you found and ran give? Let's concentrate on the example code from your installed library, OK.

here's the youtube link

diagram link (I used a smaller breadboard though)

Take an image of your project and post it, please.

This is the code

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
bool parse=false;
bool config=false;

// get the date and time the compiler was run
if (getDate(DATE) && getTime(TIME)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}

Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(TIME);
Serial.print(", Date=");
Serial.println(DATE);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time="");
Serial.print(TIME);
Serial.print("", Date="");
Serial.print(DATE);
Serial.println(""");
}
}

void loop() {
}

bool getTime(const char *str)
{
int Hour, Min, Sec;

if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}

bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;

if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}

the error it gave was

avrdude: stk500_paged_load(): (a) protocol error, expect=0x10, resp=0x00

avrdude: stk500_cmd(): programmer is out of sync

avr_read(): error reading address 0x0000

read operation not supported for memory "flash"

avrdude: failed to read all of flash memory, rc=-2

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x00

1 Like

The TimeLib.h has a command to set the time see TimeLib.h

For simplicity I would put :

setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr
// (2010 or 10 sets year to 2010)

In your setup routine and put in the current time (maybe 15 seconds or so ahead) download and run.

The issue will be each time the Arduino reboots the time will be set to whatever you coded into the Setup routine.
You could add a button or manual connection so on startup it will only set the time if the manual connection is connected.

Thank you for being so cooperative and helpful with info.

You would be wise to learn how to put your code in code tags.

That is not a issue with your sketch but a possible issue with your microcontroller board or not selecting the correct board or a whole host of other things.

Can you post that image of your setup and let us know which board you are using?

You ask this, and now it looks like you never succeeded in loading a sketch? Hmmmm.