Sending Data to SD Card Error

Hey guys. Newbie here.

I am writing data to an Micro SD Card from a GPS module and temp/hum sensor. I have a two LEDs to communicate if the data is being successfully written to micro SD card. The issue I am running into is that the data stops writing to the SD card randomly. I am able to log the data I want for like 10 seconds and then it errors out and my SD card becomes "undetectable".

Date,Time,Latitude,Longitude,Speed,Altitude,Number of Satellites,Temperature,Humidity
0,0,0.000000,0.000000,0.00,0.00,0,22.00,54.00
91218,22090200,40.196296,-84.500717,0.02,313.90,10,22.00,53.00
91218,22090300,40.196296,-84.500717,0.02,313.90,10,22.00,54.00
91218,22090400,40.196296,-84.500717,0.03,313.90,10,22.00,54.00
91218,22090500,40.196296,-84.500717,0.03,313.80,10,22.00,54.00
Is there an SD card present?

Is there a reason why it logs my data correctly for 4 occurencces and then stops??

Here's my code:

//DHT11 SETUP
#include <dht.h>
dht DHT;
#define DHT11_PIN 7

//GPS SETUP
#include <TinyGPS++.h>
TinyGPSPlus gps;

//SD CARD SETUP
#include <SPI.h>
#include <SD.h>
int CS_pin = 53;
File sd_file;

//LED SIGNAL
const int ledPin1 =  11;      // the number of the LED pin
const int ledPin2 =  12;      // the number of the LED pin

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT); //
  pinMode(ledPin2, OUTPUT);
  
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(CS_pin, OUTPUT);
  //dht.begin();
  // SD Card Initialization
  if (SD.begin())  {
    Serial.println("SD card is initialized.");
    digitalWrite(ledPin1, HIGH);
    delay(200);
    digitalWrite(ledPin1, LOW);
    delay(200);
    digitalWrite(ledPin1, HIGH);
    delay(200);
    digitalWrite(ledPin1, LOW);
    delay(200);
    digitalWrite(ledPin1, HIGH);
    delay(200);
    digitalWrite(ledPin1, LOW);
    delay(200);
    digitalWrite(ledPin2, LOW);
  } 
  else  {
    Serial.println("ERROR! SD Card Connection Failed");
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    return;
  }
  sd_file = SD.open("data.txt", FILE_WRITE);
  if (sd_file)  {
    Serial.println("Date,Time,Latitude,Longitude,Speed,Altitude,Number of Satellites,Temperature,Humidity");
    sd_file.println("Date,Time,Latitude,Longitude,Speed,Altitude,Number of Satellites,Temperature,Humidity");  
  }
  sd_file.close(); //closing the file
 }

void loop() // run over and over
{
  sd_file = SD.open("data.txt", FILE_WRITE);
  if (sd_file)  {
    digitalWrite(ledPin1, HIGH);
    senddata();
   }
  
  // if the file didn't open, print an error:
  else  {
    Serial.println("Is there an SD card present?");
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    delay(60000);
  }
}



void senddata() 
{
int chk = DHT.read11(DHT11_PIN);
  
while (Serial1.available() > 0)
  gps.encode(Serial1.read());

 if (DHT.temperature > 0 )
 {
  Serial.print(gps.date.value()); // Raw date in DDMMYY format (u32)
  Serial.print(",");
  Serial.print(gps.time.value()); // Raw time in HHMMSSCC format (u32)
  Serial.print(",");
  Serial.print(gps.location.lat(), 6); // Latitude in degrees (double)
  Serial.print(",");
  Serial.print(gps.location.lng(), 6); // Longitude in degrees (double)
  Serial.print(",");
  Serial.print(gps.speed.mps()); // Speed in meters per second (double)
  Serial.print(",");
  Serial.print(gps.altitude.meters());
  Serial.print(",");
  Serial.print(gps.satellites.value()); // Number of satellites in use (u32)
  Serial.print(",");
  Serial.print(DHT.temperature);
  Serial.print(",");
  Serial.println(DHT.humidity);

  sd_file.print(gps.date.value()); // Raw date in DDMMYY format (u32)
  sd_file.print(",");
  sd_file.print(gps.time.value()); // Raw time in HHMMSSCC format (u32)
  sd_file.print(",");
  sd_file.print(gps.location.lat(), 6); // Latitude in degrees (double)
  sd_file.print(",");
  sd_file.print(gps.location.lng(), 6); // Longitude in degrees (double)
  sd_file.print(",");
  sd_file.print(gps.speed.mps()); // Speed in meters per second (double)
  sd_file.print(",");
  sd_file.print(gps.altitude.meters());
  sd_file.print(",");
  sd_file.print(gps.satellites.value()); // Number of satellites in use (u32)
  sd_file.print(",");
  sd_file.print(DHT.temperature);
  sd_file.print(",");
  sd_file.println(DHT.humidity);

  sd_file.flush(); //saving the file

    //delay(2000);
  
  sd_file.close();   //closing the file 
 
  }
}

Which type of arduino? The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 11, 12, and 13 on typical Arduino boards... and your LEDS are on 11 and 12... bad choice...

Sorry for the late response. I must have missed my notification.

I am using a Arduino Mega. I will try adjusting those LED pins and see if that changes my results.

Just by reading online, other instances call an issue due to memory of the micro controller. Is this something that could be causing my problem?

I'd like mention this is my first time really working with Arduino so please be patient with me. Also, thanks for the help. It is greatly appreciated.

On the MEGA the SPI pins are not on 11 and 12 - so that would not be the issue then.

the way you read the GPS data is not correct - this is how you should structure it

  while (Serial1.available())
  {
    int c = Serial1.read();
    if (c != -1) 
      if (gps.encode(c)) {
      // ===== >> process new gps info here <<========
      }
  }

I would also recommend to use the SDFat library instead of SD

Thanks for the info. I am very new to programming and cannot say I understand the solution you are implying. Would this potential solution capture data from my gps module and the DHT sensor?

My goal is to write lines of data from the gps and DHT sensor every 10 seconds or so.

Even if you are new to programming you can probably see that I am not dealing with the DHT at all there. I’m just showing you how to make sure the GPS data needs to be handled to ensure you can do something with it. You could decide to read the dht once you got all the GPS data for example (so within the if) and save everything then