Micro SD drive failing after like 12 hours

Was so happy with my SD drive all working fine doing log entries once per minute
But doing longer test runs I find it always fails before 24 hours
Think the most I have really verified that it’s working (by checking led) is 2-3 hours
Always after running over night I see

a) I can see from access led it has stopped access to drive
b) When I check it out in my laptop drive the card has to be reformatted ( so all data/files gone )

I tried format with the SDFormatter program 4.0. No change
Tried comment out part of the code to save SRAM No change

At the moment I’m testing running with serial console open to see if I can detect exakt time of failure

One strange thing I noticed is I have 4.9 V on Arduino +5 (which feed the SD card) when USB serial is connected. When I run on just external power I have 3.62 V on +5 and 5.23 V on Vin.
Why is this so ? Will try a run and feed SD card from Vin … maybe 3.62 is not good enough for SD ??

BR Bosse

Memory usage:

Sketch uses 25 610 bytes (79%) of program storage space. Maximum is 32 256 bytes.
Global variables use 1 343 bytes (65%) of dynamic memory, leaving 705 bytes for local variables. Maximum is 2 048 bytes.

Program code:

//  For DHT11 temp
#include "DHT.h"

//    For RTC
#include <DS3232RTC.h>
#include <Time.h>
#include <Wire.h>
//  For SD card
#include <SPI.h>
#include <SD.h>

//  For GSM
//#include "SoftwareSerial.h"
//#include "Adafruit_FONA.h"

//  Pin defs
#define RELAYPIN 7
#define SDPIN 10
#define DHTPIN 8
#define FONA_RX            3
#define FONA_TX            4
#define FONA_RST           5

//  Local defs
#define LOOPTIME           60000


///////////////////    DHT11 code     ///////////////////
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);


///////////////////    RTC clock code     ///////////////////
void digitalClockDisplay(void)
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(month());
  Serial.print(' ');
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

///////////////////    GSM code     ///////////////////
char replybuffer[255];
//SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
//SoftwareSerial *fonaSerial = &fonaSS;
//Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
//uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);


///////////////////    My local variables                                     ///////////////////
unsigned long lcnt = 0;
int pstat = 0;
unsigned long hstart, hstop, hduration, shduration, mhduration, smhduration, totalon, stotalon;
int myyear, mymonth, myday, myhour, mymin, mysec;
float t, t0, mt = 0, mit = 38, targettemp = 2.8;
String TxtTime, mhdurationtime;
String report1 = "No report";
String report2 = "No report";
String MyErrorMess, mttime, mittime, tmptext, logfilen;
File LogFile1;

///////////////////    My local functions     ///////////////////

String add0(int x) {
  String atmp;
  atmp = String(x);
  if (x <= 9) {
    atmp = "0" + atmp;
  }
  return atmp;
}
String mytime2() {
  String amin, asec, ahour, aday, amon;
  SetMytime();
  asec = add0(mysec);
  amin = add0(mymin);
  ahour = add0(myhour);
  aday = add0(myday);
  amon = add0(mymonth);
  TxtTime = String(myyear) + amon + aday + ahour + amin + asec;
  return TxtTime;
}

void SetMytime () {
  myyear = year();
  mymonth = month();
  myday = day();
  myhour = hour();
  mymin = minute();
  mysec = second();
}

void MySDwrite(String mtt) {
  LogFile1 = SD.open(logfilen, FILE_WRITE);
  if (LogFile1) {
    LogFile1.println(mtt);
    LogFile1.close();
  }
  else {
    Serial.print(F("lfile Open FAIL"));
  }
}

void MySDread() {
  LogFile1 = SD.open(logfilen);
  while (LogFile1.available()) {
    Serial.write(LogFile1.read());
  }
  LogFile1.close();
}


///////////////////    SETUP    SETUP SETUP SETUP SETUP SETUP ///////////////////
void setup() {
  Serial.begin(115200);
  dht.begin();  // DHT11 startup

  //    Start RTC code
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet) {
    Serial.println(F("Unable to sync with the RTC"));

  }
  else {
    Serial.println(F("RTC has set the system time"));
  }
  //  End RTC code

  //  Start SD code
  //    SD init
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(SDPIN)) {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));
  //  End SD code

  //  Toggle logfile
  if ( ! SD.exists("nr2")) {
    LogFile1 = SD.open("nr2", FILE_WRITE);
    LogFile1.println("hej");
    LogFile1.close();
    logfilen = "logfile.txt";
  }
  else {
    SD.remove("nr2");
    logfilen = "logfile2.txt";
  }

  //  Start time in logfile
  tmptext = mytime2();
  tmptext = "Start time: " + tmptext;
  MySDwrite(tmptext);

}

///////////////////    LOOP    LOOP  LOOP  LOOP  LOOP  LOOP  LOOP  ///////////////////
void loop() {
  // Wait a few seconds between measurements.
  delay(LOOPTIME);
  String nowtime = mytime2();

  // Make sure to turn off heat before loop start
  if ( lcnt == 0 ) {
    pinMode(RELAYPIN, OUTPUT);
    digitalWrite(RELAYPIN, LOW);
  }

  //  Get temp
  t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(t0)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // read the input on analog pin 0:
  targettemp = analogRead(A0) * 30 / 1024;

  //    Error checking
  if ( t < 0.5 ) {
    MyErrorMess = "Below 0.5 " + mytime2() + "," + t;
    MySDwrite(MyErrorMess);
  }

  //    Turn ON heating
  if ( t <= targettemp - 2 ) {
    if ( pstat == 0 ) {
      Serial.println(F("turn on"));
      digitalWrite(RELAYPIN, HIGH);
      hstart = millis();
    }
    pstat = 1;
  }

  // Turn OFF heating
  /*if ( t > targettemp + 2 ) {
    if ( pstat == 1 ) {
      Serial.println(F("turn off"));
      digitalWrite(RELAYPIN, LOW);
      hstop = millis();
      hduration = hstop - hstart;
      totalon = hduration + totalon;
      if ( max(hduration, mhduration) > mhduration) {
        mhduration = hduration;
        mhdurationtime = nowtime;
      }
      shduration = hduration / 1000;
      stotalon = totalon / 1000;
      smhduration = mhduration / 1000;
      //report1 = "HEAT," + nowtime + "," + shduration + "," + stotalon + "," + smhduration + "," + mhdurationtime;
      //report1 = "Rep1kkkkkkkkkkk";
      //report2 = "TEMP," + String(t)  + "," + nowtime + "," + mt + "," + mttime + "," + mit + "," + mittime;
      //Serial.print(report1);
      //MySDwrite(report1);
      //Serial.print(report1);
    }
    pstat = 0;
  }*/

  // Print some loop status
  //MySDread();
  Serial.print(targettemp);
  Serial.print("  ");
  Serial.print(t);
  Serial.print("  ");
  digitalClockDisplay();

  //  Get min and max temp
  if ( min(t, mit) < mit) {
    //Serial.print("Themintime\n");
    mit = t;
    mittime = nowtime;
  }
  if ( max(t, mt) > mt) {
    mt = t;
    mttime = nowtime;
  }
  report2 = "TEMP," + String(t)  + "," + nowtime + "," + mt + "," + mttime + "," + mit + "," + mittime;
  //report2 = "kkkkkl";
  MySDwrite(report2);
  lcnt++;
}