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
#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.
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?
