I implemented RTC (DS3231) in my Arduino project the first time.
RTC is connected to SDA(A4) and SCL(A5) of Arduino Uno R3 board.
RTC library by NeiroN is in use.
The initial Date/Time set of RTC was done by the following code, similar to one from examples:
// Date and time functions using just software, based on millis() & timer
#include <Arduino.h>
#include <Wire.h> // this #include still required because the RTClib depends on it
#include "RTClib.h"
RTC_Millis rtc;
void setup () {
Serial.begin(115200);
// following line sets the RTC to the date & time this sketch was compiled
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
rtc.begin(DateTime(DATE, TIME));
rtc.adjust(DateTime(2018, 11, 12, 21, 03, 0));
}
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);
}
The output in Serial Monitor was as expected:
2018/11/12 21:03:06
2018/11/12 21:03:09
2018/11/12 21:03:12
...
My next sketch was uploaded to begin some evaluations of RTC.
Its code is the following:
//20Oct2018 - Modification to 16x2 LCD
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h> // this #include still required because the RTClib depends on it
#include "RTClib.h"
RTC_Millis rtc;
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.begin(16, 2);
}
void loop() {
// put your main code here, to run repeatedly:
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(2000);
}
And its results on Serial Monitor were very strange:
2106/2/6 6:28:16
2106/2/6 6:28:18
2106/2/6 6:28:20
2106/2/6 6:28:22
2106/2/6 6:28:24
2106/2/6 6:28:26
2106/2/6 6:28:28
...
Please give me some tip, what may be wrong.
Thanks.