RTC not writing correct data to SD

I cannot get the correct RTC data to print to the SD card.

I am using 5v promini, adafruit RTC(part#3295), adafruit SD(part#254). a reformatted 32gb SD card.

I know the RTC is working because the correct time information is displayed on the serial console when I run timestamp.ino from the RTC examples.

Time Data is written to the SD card but it does not update and it is not correct. The other data("count") updates and displays correctly

I am using the example code from www.arduinogetstarted.com/tutorials/arduino-log-data-with-timestamp-tp-sd-card. Everything compiles correctly but the RTC data printed to the SD card is not correct.

Any help appreciated.

Bill
DATALOGGER.ino (3.1 KB)

Please post examples of the incorrect data, and describe what you expected to see.

Attached is the file generated on the SD card.
The following line is one line of text which is the correct number of characters.
The first number"10" is the "count" which increments correctly each time the time is read.
The next number,"17" should be the month, the next number is the day and then time, hr, min. sec,.
10-17-33-2023-29-3-0: the RTC reports the correct date and time when viewed on timestamp.ino. The correct data should be 2-20-2023-11-30-22 or something close to the correct time.

Thanks for your help.

DATALOG.TXT (1.5 KB)

Please follow forum rules and post code/text inline, using code tags. Instructions here.

People using smart phones will often ignore posts that don't follow those rules.

By the way, it is a serious mistake to open the SD card data file, write a line or two of data, and then close it again. That vastly increases the current consumption of the project, the SD card error rate and can lead to brown outs and processor resets in some cases.

Open the file once in setup() and close it again when done writing data to the card. To protect data from unexpected shutdowns, every few minutes or hours issue a command like the following, to update the file pointers.

dataFile.flush();

To test correct RTC library function, replace the dataFile.print() statements with Serial.print() statements.

Thanks for advice, I changed to dataFile.print to serial and the following is what was printed.

target == HIGH
Count = 4
opening data fiLe
Writing to SD card
3
4-18-40-2023-29-3-0:
target == HIGH
Count = 5
opening data fiLe
Writing to SD card
3
5-18-40-2023-29-3-0:

Again the "count" is incrementing but time is not correct, the time when I took these readings was 2:05 PM EST.

My application records data at random time periods. I might get 1000 data writes one day and none of them are closer that 5 seconds. The system could stay idle for 3-4 days and then start recording again. IIs like counting people entering a building, real busy for a while and dead for hours. I have not figured the best approach but I will incorporate you comments.

THANKS.

Meanwhile, why isn't time updating?????

The link to the tutorial you followed does not work.

Test the RTC using the library example programs, and make sure you know how to use it, before starting any other programming.

Hi!

This leads to a PCF8523 based board, not a DS1307. Ok, it doesn´t explain why data is correct at Serial Monitor and wrong in the SD card, but can be an issue.

You should change the line:

RTC_DS1307 rtc;

in your code to:

RTC_PCF8523 rtc;

PS: if the other tip doesn´t solve the issue by itself, take a look also in the PCF8523 example of the RTClib library at Github, specially on these lines:

  // When the RTC was stopped and stays connected to the battery, it has
  // to be restarted by clearing the STOP bit. Let's do this to ensure
  // the RTC is running.
  rtc.start();

I changed RTC_DS1307 to RTC_PCF8523 and it looks like this fixed the problem. I still have some testing to do so keep this post open in case I run into another glitch.
Thank you for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.