Why isn't my DS3231 updating the time?

Hello, I'm having trouble just updating the time on the DS3231 RTC. I'm using the library DS3232_Simple and when I run the example to set the time or read the time it always sets it to "2002-00-176T00:00:03"

the code is:

#include <DS3231_Simple.h>

DS3231_Simple Clock;

void setup() {
  
  
  Serial.begin(9600);
  Clock.begin();
  
  Serial.println();
  Serial.println();
}

void loop() 
{ 
  // Create a variable to hold the data 
  DateTime MyTimestamp;
  
  // Load it with the date and time you want to set, for example
  //   Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
  MyTimestamp.Day    = 3;
  MyTimestamp.Month  = 10;
  MyTimestamp.Year   = 20; 
  MyTimestamp.Hour   = 14;
  MyTimestamp.Minute = 17;
  MyTimestamp.Second = 33;
  
  // Then write it to the clock
  Clock.write(MyTimestamp);
  
  // And use it, we will read it back for example...  
  Serial.print("The time has been set to: ");
  Clock.printTo(Serial);
  Serial.println();
  
  // Remember, once you set the time, the clock remembers it and keeps
  // running even if you reset or turn off your Arduino, as long as
  // the clock has battery power.
  
  Serial.print("End Of Program (RESET to run again)");
  while(1);
}

but the output is:

The time has been set to: 2002-00-176T00:00:03
End Of Program (RESET to run again)

I haven't done much else since I'm new to using the Arduino, please let me know if the issue is just that I missed a step or that I'm not doing something right.

You seem to set the time, print it and then halt.

You might want to set the time in the setup() function, and continuously print it in the loop() function.

You have created the variable in loop().
Every time loop() runs, it re-creates and re-initialises MyTimestamp.

do you have a good example code for this? I just used the example code they gave and didn't expect that this would be the cause of the issues

do I just move this out of the loop? I tried to move this line to the setup function but now every time the loop function calls the variable theres an error. Sorry I'm still new to this

Maybe something like this?

#include <DS3231_Simple.h>

DS3231_Simple Clock;


void setup() {
  Serial.begin(9600);

  Clock.begin();
  // Create a variable to hold the data 
  DateTime MyTimestamp;
  // Load it with the date and time you want to set, for example
  //   Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
  MyTimestamp.Day    = 3;
  MyTimestamp.Month  = 10;
  MyTimestamp.Year   = 20;
  MyTimestamp.Hour   = 14;
  MyTimestamp.Minute = 17; 
  MyTimestamp.Second = 33;
  // Then write it to the clock
  Clock.write(MyTimestamp);
} 

void loop() {
  // And use it, we will read it back for example...  
  Serial.print("The time has been set to: ");
  Clock.printTo(Serial);
  Serial.println();
}

[disclaimer] Untested code.

This needs to be moved to outside of and before setup()
Just to explain, if it is in setup(), then the MyTimestamp object may not be visible to the other functions

I tried it and I have this error message. I also reran the previous code and this also shows up. Any ideas?

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0xfc
avrdude: stk500_set_extended_parms(): protocol error, expect=0x14, resp=0x00
avrdude: stk500_initialize(): failed
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

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

That looks like an upload error.

Ok the error is gone but now it just prints the wrong date still. It's not updating to the date and time that I set

Post the sketch that you are using now

Ok thanks! I've edited the code but it still doesn't change the date and time

Here's the code

#include <DS3231_Simple.h>

DS3231_Simple Clock;
DateTime MyTimestamp;

void setup() {
  
  
  Serial.begin(9600);
  Clock.begin();
  
  Serial.println();
  Serial.println();
}

void loop() 
{ 
  // Create a variable to hold the data 
  
  // Load it with the date and time you want to set, for example
  //   Saturday the 3rd of October 2020 at 14:17 and 33 Seconds...
  MyTimestamp.Day    = 3;
  MyTimestamp.Month  = 10;
  MyTimestamp.Year   = 20; 
  MyTimestamp.Hour   = 14;
  MyTimestamp.Minute = 17;
  MyTimestamp.Second = 33;
  
  // Then write it to the clock
  Clock.write(MyTimestamp);
  
  // And use it, we will read it back for example...  
  Serial.print("The time has been set to: ");
  Clock.printTo(Serial);
  Serial.println();
  
  // Remember, once you set the time, the clock remembers it and keeps
  // running even if you reset or turn off your Arduino, as long as
  // the clock has battery power.
  
  Serial.print("End Of Program (RESET to run again)");
  while(1);
}

You are still not setting the time in setup() and you only print it once in loop() before entering an endless while loop. Is that what you meant to do ?

Well I'm just trying to reset the DS3231 to the right date and time. Once it's set I plan to use it on a different code. The original code I posted was just the example code from the library thats meant to set the time and date

OK. The code makes sense for setting the clock

Please post the sketch that you run afterwards to read the time

Yes it should work for setting the clock but in my case it doesn't at all and I can't tell what the issue is.

How do you know ?

Has the RTC module got a backup battery ?

Ah, I see your problem now.

You set the time and immediately read it back, but instead of seeing the time you just set, you see a default time string.

I would check the wiring at this stage.

[edit] Also, with the sketch I proposed, you do not see the seconds ticking at all?

The code includes posting the set date and time to the serial monitor so I can see that it does not change to the date and time I specified.

for the sketch I run after I have not completed it yet so I don't think it is useful to share since the issue is just with the sketch to set the time.

yes there is a backup battery