# Sd datalogger Error opening file after working for a bit

**URL:** <https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094>\
**Category:** Storage\
**Created:** [June 27, 2021, 3:36pm UTC](https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094 "2021-06-27T15:36:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dunkbaskest1](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@dunkbaskest1](https://forum.arduino.cc/u/dunkbaskest1)\
**Post date:** [June 27, 2021, 3:36pm UTC](https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094/1 "2021-06-27T15:36:56Z")

</div>

so my SD datalogger for temperature and humidity was working for a bit, but after some time, it always stops working and has an error opening file.

The code is almost the same as some other code I copied from online somewhere.

This is what the Serial Monitor says

card is initialized. Ready to go  
Time,Humidity,Temperature\_C,Heat\_index  
0:0:0, 65.00, 20.70, 15.52  
File Saved..  
14:55:01.795 -\> 0:0:10, 65.00, 20.80, 15.64  
14:55:01.795 -\> File Saved..  
14:55:11.848 -\> 0:0:20, 65.00, 20.70, 15.52  
14:55:11.848 -\> File Saved..  
14:55:21.880 -\> 0:0:30, 65.00, 20.60, 15.41  
14:55:21.914 -\> File Saved..  
14:55:31.946 -\> 0:0:40, 65.00, 20.60, 15.41  
14:55:31.946 -\> File Saved..  
14:55:41.989 -\> 0:0:50, 66.00, 20.50, 15.35  
14:55:42.023 -\> File Saved..  
14:55:51.969 -\> File closed..  
14:56:01.971 -\> error opening file  
14:56:12.004 -\> error opening file  
14:56:22.003 -\> error opening file  
14:56:32.013 -\> error opening file

**The code looks like this**

```cpp
#include <SD.h>
#include <SPI.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

long seconds=00;
long minutes=00;
long hours=00;

int CS_pin = 10;

DHT dht(DHTPIN, DHTTYPE);
File sd_file;

void setup() {
  Serial.begin(9600);
  pinMode(CS_pin, OUTPUT);
  dht.begin();
  // SD Card Initialization
  if (SD.begin()) {
    Serial.println("SD card is initialized. Ready to go");
  } 
  else {
    Serial.println("Failed");
    return;
  }

  sd_file = SD.open("data.txt", FILE_WRITE);

  if (sd_file) {
    Serial.print("Time");
    Serial.print(",");
    Serial.print("Humidity");
    Serial.print(",");
    Serial.print("Temperature_C");
    Serial.print(",");
    Serial.println("Heat_index");

    sd_file.print("Time");
    sd_file.print(",");
    sd_file.print("Humidity");
    sd_file.print(",");
    sd_file.print("Temperature_C");
    sd_file.print(",");
    sd_file.println("Heat_index");
  }
  sd_file.close(); //closing the file
} 

void loop() {
  sd_file = SD.open("data.txt", FILE_WRITE);
  if (sd_file) {
    senddata();
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening file");
  }
  delay(10000);
}

void senddata() {
  for(long seconds = 00; seconds < 60; seconds=seconds+10) {
    float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
    float hum = dht.readHumidity(); //Reading the humidity and storing in hum
    float heat_index = dht.computeHeatIndex(temp, hum);

    sd_file.print(hours);
    sd_file.print(":");
    sd_file.print(minutes);
    sd_file.print(":");
    sd_file.print(seconds);
    sd_file.print(", ");
    sd_file.print(hum);
    sd_file.print(", ");
    sd_file.print(temp);
    sd_file.print(", ");
    sd_file.println(heat_index);

    Serial.print(hours);
    Serial.print(":");
    Serial.print(minutes);
    Serial.print(":");
    Serial.print(seconds);
    Serial.print(", ");
    Serial.print(hum);
    Serial.print(", ");
    Serial.print(temp);
    Serial.print(", ");
    Serial.println(heat_index);

    if(seconds>=50) {
      minutes= minutes + 1;
    }

    if (minutes>59) {
      hours = hours + 1;
      minutes = 0;
    }

    sd_file.flush(); //saving the file
      Serial.println("File Saved..");

    delay(10000);
  }
  sd_file.close(); //closing the file
  Serial.println("File closed..");
}

```

\*\*This is what it looks like \*\* There is extra wires in there, because in the past I tried it with a RTC and a MQ135 co2 sensor, but It was having problems, so I have removed them to make it more simple. to try to find out what the problem is.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/6/4/16422ac82a68595a5d279244a953150839b52549.jpeg)

When I look at the Micro SD card it has some weird characters on there at the end like this

Time,Humidity,Temperature\_C,Heat\_index  
0:0:0, 65.00, 18.10, 12.66  
0:0:10, 65.00, 18.20, 12.77  
0:20, 65.00, 18.20, 12.77  
0:30, 65.00, 18.20, 12.77  
0:0:40, 65.00, 18.10, 12.66  
0:50, 65.00, 18.10, 12.66  
0:0:0, 65.00, 18.20, 12.77  
0:0:10, 65.00, 18.10, 12.66  
0:20, 65.00, 18.20, 12.77  
65.00, 18.20, 12.77  
0:40, 65.00, Bý¶"•—€\*Z×ü’  
Ø  
X¢uK«éÊ¿5WÖB®è•Ž5$$]¥®Iÿ“zù«.EPˆaaÎ— {Ò[W°Šº  
\Ÿo;o;Ÿp(`„A!_@:jhI-¾ÊÏ»°†D-•¤«ÔR/é(Õ¥%µ¨¡W@W@CKjQõU~Þ=(m zP<Úy®ëé­«=ç=ú_åç]Q¬!’t•º’þ'õù«.E(Äƒ0€®€®QCKj*?ïúR/éÔ•¤«D-°†p]Ï+5´¤ttZR‹®ëyHÔk]¥®$½¤ÿI@ƒ«¢.¬…é­«==úç]Ï+pC¢X%é*uýOê%]ŠòWaPˆaHçK ôÖÕžQÖB €ÁUû|Ûùùû|ÛR”¿êƒB<@:_ó¿ý®¯òóx´õ <ÚzPë«ü¼ÿëÑ?W@W@…x”¿êRâÑÖƒ ëAñh¯òó®ü¯GÿK Hç–Ô¢†U~Þõ^Òÿ¤}¾íü(`pp]Ï+¤5´tt‹ZRùy×WP\<Úz]¥®$€ÁU ¡¨kuµ'½«=é­^ëz  
‰Z`UêJÒþ'õ’üU—¢0:0:0, 65.00, 20.70, 15.52

Does anyone have any ideas about what could be causing this?

---

<div class="post-metadata">

**Author:** ![Nick\_Pyner](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nick\_Pyner](https://forum.arduino.cc/u/Nick_Pyner)\
**Post date:** [June 27, 2021, 4:17pm UTC](https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094/2 "2021-06-27T16:17:36Z")

</div>

Delete the file close command in setup, and get rid of the file open / file close commands in the loop.

---

<div class="post-metadata">

**Author:** ![drmpf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drmpf/32/1126438_2.png) [@drmpf](https://forum.arduino.cc/u/drmpf)\
**Post date:** [June 28, 2021, 2:11am UTC](https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094/3 "2021-06-28T02:11:07Z")

</div>

> [@dunkbaskest1](#):
>
> `sd_file = SD.open("data.txt", FILE_WRITE);`

This statement allocates memory on the heap to create/return the file in the 'standard' SD library.  
Which may/is the source of your problems.  
Bill Greiman's [SdFat](https://github.com/greiman/SdFat) library appears to be better in this regard.  
BUT in any case as noted above, no need to open/close in every loop. flush() should force updates to the SD so that when you remove power the last record has been written.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 26, 2021, 2:11am UTC](https://forum.arduino.cc/t/sd-datalogger-error-opening-file-after-working-for-a-bit/879094/4 "2021-10-26T02:11:51Z")

</div>

Closed on October 26, 2021, 2:11am UTC

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