Hello,
I am working on a project with an Arduino Pro (5V, 16MHz, ATMega328P) and a Data Logging Shield (Overview | Adafruit Data Logger Shield | Adafruit Learning System).
I am trying to get my RTC set up so that I can print time stamps in Unix time. I am using the RTClib library (GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library) to run the RTC. I used the example given in the library for the PCF8523 to initialize my RTC. At first, it was working fine, but now it is not, and I am not sure why. It no longer prints any time stamps, and keeps getting stuck in the setup of the code. I have tried using both my code and the example, and they both run into the same issue.
I added some basic serial.print statements to check to see where it is getting hung up. Apparently, it's just getting stuck at the "if (! rtc.initialized())" part. I know that I have initialized it correctly because when it was making it through the set up, it was printing the correct time.
Occasionally I will get it to make it through the set up, but I don't know what I am doing differently to get it to run, and it doesn't seem consistent. When it is working, I can hit the reset button on the Arduino and it will always work perfectly again, but when I close the serial port and re-upload the same code without touching the Arduino, it will get stuck and be unable to make it through the set up, even though it was just working a second ago.
Basically, I am not sure if I am doing something wrong or if there is something wrong with my connections. I have a separate file that records data on an SD card using the same shield, and that data is recorded fine, so I am led to believe that it is not a connection issue.
Any help or suggestions would be greatly appreciated!
My Code:
#include <RTClib.h>
RTC_PCF8523 rtc;
void setup() {
while(!Serial) {
delay(1);
}
Serial.begin(57600);
Serial.println("Initializing RTC");
if(! rtc.begin()){
Serial.println("Couldn't find RTC");
while(1);
}
// A test to debug
Serial.print("test1");
// Check to make sure that RTC has been initialized
if(! rtc.initialized()){
Serial.println("RTC is NOT running!");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// A test to debug
Serial.println("made it!");
}
void loop() {
DateTime now = rtc.now();
//Calculate and print unix time
Serial.println(now.unixtime());
delay(3000);
}