How to set the the correct time and date with a ds3231 rtc.

Hello,

I have followed the instructions on this website:

I opened the the example code (from the folder it said to download) and altered it a bit so I have this as my code:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Serial.begin(57600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    delay(3000);
}

However it is set for 1/1/2000 plus how long the battery has been working. How do I change this so it displays the correct time?

I am not great with code so please explain things as fully as you can. :wink:
Any help will be much appreciated!!!!!!!!!

Probably there are examples in the library that show how it is done. Have you checked them?

Brilliant thank you so much!!!!

Do you know how do I get the time to be printed like a clock on the serial monitor rather than a list of times?

what do you mean by clock, please provide an example

Note: IDE serial cannot do graphics...

hello,

I mean like a digital clock has one line for time and only the last number changes until it changes the minute and then hours.

I have also noticed the ds3231 doesn't keep the time with this code as the time stops and starts when you open and close the serial monitor. and the time keeps going when I detach the RTC. Do you know how to fix this? so I can keep the time accurately even if I unplug the unit or close the serial monitor.

I mean like a digital clock has one line for time and only the last number changes until it changes the minute and then hours.

Using the serial monitor, you can't. Use an LCD or a different serial port reader.

I have also noticed the ds3231 doesn't keep the time with this code as the time stops and starts when you open and close the serial monitor.

Because you reset the time every time the Arduino resets. Stop doing that!

and the time keeps going when I detach the RTC.

A sledge hammer to the Arduino will fix that. Of course, time continues. The only real surprise is that you are surprised.

Do you know how to fix this?

Fix what? You've described three things about your code/your PC that you don't like. None are at all difficult to change.

I mean like a digital clock has one line for time and only the last number changes until it changes the minute and then hours.

if (hour <10) Serial.print("0");
serial.print(hour);

your turn

serial.print(hour);

Hmmm...

yes my right shift isn't working as it used to :wink:

I have done some tests and found that the ds3231 doesn't keep time when the ground is unplugged, but if you unplug and then re-plug in all the other pins the ds3231 keeps the time and displays the correct time. any help?

any help?

You need help with not unplugging the ground line? Nope, sorry, can't help you with that.

Right ok. But do you know why the time resets when ground is disconnected? It's quite important because I can't upload the code from computer and then switch to battery power without the time resetting.

I can't upload the code from computer and then switch to battery power without the time resetting.

Why not? You don't upload code using the clock's ground wire.

robtillaart:
yes my right shift isn't working as it used to :wink:

Just divide by two instead.

Shit. Now, I've go to go find the windex.

But do you know why the time resets when ground is disconnected? It's quite important because I can't upload the code from computer and then switch to battery power without the time resetting.

You have to be very careful changing leads to or disconnecting DS1307/DS3231 modules when power is applied. The data sheets for both parts have this comment:

WARNING: Negative undershoots below -0.3V while the part is in battery-backed mode may cause loss of data.

Sorry what does negative undershoots mean? And tanks a lot btw