Output to SD card repeating randomly

I'm using the EnviroDIY Mayfly board to measure voltage and temperature. The values are writing to a SD card for later retrieval. The problem I'm experiencing is what appears to be a random reset of the RTC to when I first uploaded the sketch and set the date/time. Here's an example where it jumped from April 16th @ 01:21 to April 12th @ 15:40 (the date/time I originally set the RTC to):

2021/4/16 1:21,3.40, 13,2
2021/4/16 1:26,3.40, 13,2
2021/4/12 15:40,3.46, 13,2
2021/4/12 15:40,3.46, 13,2
2021/4/12 15:40,3.46, 13,2
2021/4/12 15:40,3.46, 13,2
2021/4/12 15:40,3.40, 11,1
2021/4/12 15:45,3.40, 11,1
2021/4/12 15:50,3.41, 11,1
2021/4/12 15:55,3.40, 11,1

Notice how it repeats the same time stamp but not necessarily the same data. Not a battery issue since the clock is not being reset to some default value. Could this be an Interrupt problem?

I would think that there is a reset of the board happening, and your code is written to set the rtc on startup. Add some sort of print statement to setup() to check that you are restarting.

My crystal ball is hazy and you will need to provide your code, or a simplified example which demonstrates the problem.

Good thoughts. The data file is empty when I upload the sketch. When I return a few days later to retrieve the SD card, there are multiple occurrences of the clock going back in time and restarting. I attached a bigger file the output and another of the sketch. output.pdf (82.5 KB) Madison_Wetpond.ino (4.9 KB)

April 12th @ 15:40 (the date/time I originally set the RTC to)

Your posted code uses

DateTime dt(2021, 05, 4, 13, 20, 0, 0);  //Set the date and time everytime you download the program to initiate the clock

I'm not certain what initial day/time you are using, but clearly you reset the rtc every time you go through setup. Why your program is restarting is unknown.

How are you providing power? Is it stable? Often resets are caused by relays but I don't see anything like that.

void setup() {
  rtc.setDateTime(dt); //resetting rtc here!!
  pinMode(pinCS, OUTPUT);
  pinMode(8, OUTPUT);         // declare the Green ledPin as an OUTPUT
  digitalWrite(8, HIGH);      // turn the Green ledPin on
  // SD Card Initialization
  Serial.print("Initializing SD card...");

  if (!SD.begin(12)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");

Yes, you're correct. Everytime I set up the code, I have to tell the RTC what the date/time is. Then it writes a timestamp plus data to the SD every 5 minutes. I first thought my battery was going too low but I ran it again while plugged into my laptop at home and it did the same thing after a day or so. Now, I'm wondering if there isn't some sort of timing conflict between sensor measurements and writing to the SD. Could this have anything to do with not having an interrupt statement? Not sure how to test this.

That's good news in that you don't need a field test to see the issue.

You should accelerate things by doing things more frequently than every 5 minutes.

There are limited things other than power that can cause a reset in your program. First thing I would try is to run without the sd card and just log to Serial.

If that fails, then you will have to work through the different measurments to see which is causing the issue.

I do not understand your concern about interrupts. Please explain.

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