Hello...
I have an arduino mega ,a lcd 16*2 and a rtc ds3231 ,but I get wrong time and date.
For example the time and date here right now is: Friday 27/09/2019 , Time ---> 17:53
and I get : Saturday 01/01/2000 , Time ---> 00:21:20
--->> Before 2 months this project was working excellent,but now I get wrong time and wrong date....
The circuit is fine(gnd to gnd,vcc to vcc,SDA to SDA,SCL to SCL).
I reset arduino ,restart arduino ide ,but nothing ...
Code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
char buf1[10];
char buf2[10];
void setup()
{
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
lcd.begin(16, 2);
rtc.begin();
}
void Information_for_DATE_TIME()
{
DateTime now = rtc.now();
lcd.setCursor(4, 1);
sprintf(buf1, "%02d:%02d:%02d" , now.hour(), now.minute(), now.second());
lcd.print(buf1);
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 0);
sprintf(buf2, "%02d/%02d/%02d" , now.day(), now.month(), now.year());
lcd.print(buf2);
}
void loop()
{
Information_for_DATE_TIME();
}
Any advices?
Correction code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
char buf1[10];
char buf2[10];
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup()
{
lcd.begin(16, 2);
rtc.begin();
}
void Information_for_DATE_TIME()
{
DateTime now = rtc.now();
lcd.setCursor(4, 1);
sprintf(buf1, "%02d:%02d:%02d" , now.hour(), now.minute(), now.second());
lcd.print(buf1);
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 0);
sprintf(buf2, "%02d/%02d/%02d" , now.day(), now.month(), now.year());
lcd.print(buf2);
}
void loop()
{
Information_for_DATE_TIME();
}
Might need to change the battery on the RTC. But it should draw power from the Arduino. Any power outages? Just seems like the logical thing to look at first if it was working fine for a good while.
No.I changed the battery ,but nothing has changed
Did you reset the time on the RTC after changing the battery?
But the problem is still here
Please run this i2c scanner program and verify that the RTC is found on the bus
// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011
#include <Wire.h>
void setup() {
Serial.begin (115200);
// Leonardo: wait for serial port to connect
while (!Serial)
{
}
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup
void loop() {}
I got this:
I2C scanner. Scanning ...
Found address: 87 (0x57)
Found address: 104 (0x68)
Done.
Found 2 device(s).
Please post the code where you set the time, and the results.
What do yo mean that the problem is still there after you reset the time.
CODE:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
char buf1[10];
char buf2[10];
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup()
{
lcd.begin(16, 2);
rtc.begin();
}
void Information_for_DATE_TIME()
{
DateTime now = rtc.now();
lcd.setCursor(4, 1);
sprintf(buf1, "%02d:%02d:%02d" , now.hour(), now.minute(), now.second());
lcd.print(buf1);
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 0);
sprintf(buf2, "%02d/%02d/%02d" , now.day(), now.month(), now.year());
lcd.print(buf2);
}
void loop()
{
Information_for_DATE_TIME();
}
Results in lcd:
Sat 01/01/2000
00:10:20
The time here where I live is:
Fri 27/09/2019 20:40
I do not know what is wrong.....
Post the code used to set the time, and post the results.
I do not know what is wrong.....
The RTC is set to the wrong time.
Finally how can i fix it ?
There should be an example in the IDE under File/Examples/RTC for setting the correct time. Find it. Change times in it. And upload to your Arduino. Then upload your code above.
I do not know what is wrong.....
The code you posted in #11 does not compile. You are missing the instantiation of both the rtc and lcd objects.
Please focus on what you are posting and verify that the code compiles before posting.
You are right.I forgot it.