Strange Date and Time format

I’m using an Arduino Mega with the Keyes Data Logging Shield and trying to learn how to use the RTC feature. This is my code:

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

RTC_DS1307 rtc;
File arquivo;
String medicamento;
String dosagem;
int frequencia;
String horario;

void setup() {
	Serial.begin(57600);
	Wire.begin();
	rtc.begin();
}

void loop() {
	imprimirDataHora();
	delay(1000);
}

void imprimirDataHora() {
	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();
	Serial.println();
}

void verificaRTC() {
	if (!rtc.isrunning()) {
		Serial.println("RTC não está em execução");
		rtc.adjust(DateTime(__DATE__, __TIME__));
	}
}

But this is my output: 2165/165/165 165:165:85

What am I doing wrong?

Have you actually set the time once on this module?

Add this at the end of the setup() rtc.adjust(DateTime(__DATE__, __TIME__));, upload the code and let it run, then edit your code and comment out that line (only need to set up time once until the battery runs out)

What do you get?

What am I doing wrong?

Could be a number of things. The RTC may not be connected properly. It may not have the address that the library thinks it should have. It may never have been started. The battery could be dead.

It doesn't do much good to create a function to assign a time to the RTC is you never call it.

J-M-L:
Have you actually set the time once on this module?

Add this at the end of the setup() rtc.adjust(DateTime(__DATE__, __TIME__));, upload the code and let it run, then edit your code and comment out that line (only need to set up time once until the battery runs out)

What do you get?

I still get the same output: 2165/165/165 165:165:85

PaulS:
It doesn't do much good to create a function to assign a time to the RTC is you never call it.

I forgot that and now I called the verificaRTC() method, but still doesn't work.

Did you call verificaRTC or did you put the line (and only that line) in the setup as I suggested?

J-M-L:
Did you call verificaRTC or did you put the line (and only that line) in the setup as I suggested?

I did both, didn’t worked. Then commented the verificaRTC() call and used just the line you suggested at the end of setup() and didn’t worked either.

In case of doubt, here’s the related code:

#include "Arduino.h"
#include "SD.h"
#include "SPI.h"
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup() {
	SPI.begin();
	Serial.begin(57600);
	Wire.begin();
	rtc.begin();
	//verificaRTC();
	rtc.adjust(DateTime(__DATE__, __TIME__));
}

void loop() {
	imprimirDataHora();
	delay(1000);
}

void imprimirDataHora() {
	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();
	Serial.println();
}

void verificaRTC() {
	if (!rtc.isrunning()) {
		Serial.println("RTC não está em execução");
		rtc.adjust(DateTime(__DATE__, __TIME__));
	}
}

Did anyone find a solution to this date / time issue ? Have read many posts but yet to find a solution. thanks.

Which "RTClib.h" are you using?

https://forum.arduino.cc/index.php?topic=189283.0

Try removing the backup battery to see if that works. Apparently a high battery voltage or low Vcc voltage can cause problems. Something said the Vcc has to be at least 1.25 * Vbat.