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.
Any help will be much appreciated!!!!!!!!!
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 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?
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.
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.