Error opening file on sd card

Hello There!.I am still amateur to the arduino.so apologies in advance if u find any silly mistakes. I have been trying to store GPS data gathered through my ublox M8N module in 16 gb FAT32 SanDisk sd card. But an erratic error is occurring. Mostly my data.txt file would not be even created but even if it was created sometimes, anything could not be written onto it. pls help me out as i am perplexed why it is not working properly.All the physical connections and individual units are working properly,but when i latch all together, it becomes messy.
The message displayed on serial was-"error opening data.txt"

This is the code-

#include<SD.h>
#include<SPI.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
static const int RXPin = 3, TXPin = 2;
static const uint32_t GPSBaud = 9600;
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
TinyGPSPlus gps;
File myFile;
SoftwareSerial ss(RXPin, TXPin);
void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}
void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  Serial.println(F("Simple Test with TinyGPS++ and attached NEO-6M GPS module"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println();
  displaySensorDetails();
  pinMode(10, OUTPUT);
  if (SD.begin(4)) {
    Serial.println("Initialization Successful");
  }
  else {
    Serial.println("Initialization Failed");
  }
  if (SD.exists("data.txt")) { //Delete old data files to start fresh
    SD.remove("data.txt");
  }
myFile = SD.open("data.txt", FILE_WRITE);
 if (myFile) {
    Serial.print("Writing to data.txt...");
     myFile.print("GPS Testing");
    Serial.println("done.");
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening data.txt");
  }
}
void loop()
{
  sensors_event_t event1;
  myFile = SD.open("data.txt", FILE_WRITE);

  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayGpsInfo();
      if (myFile) {
     if (gps.location.isValid())
        myFile.print("Location: ");
        myFile.print(gps.location.lat(),6);
        myFile.print(",");
        myFile.print(gps.location.lng(), 6);
     if (gps.date.isValid())
         myFile.print("  Date/Time: ");
         myFile.print(gps.date.month());
         myFile.print("/");
         myFile.print(gps.date.day());
         myFile.print("/");
         myFile.print(gps.date.year());
     if (gps.time.isValid())
         if (gps.time.hour() < 10) myFile.print(F("0"));
         myFile.print(gps.time.hour());
         myFile.print("/");
         if (gps.time.minute() < 10) myFile.print(F("0"));
         myFile.print(gps.time.minute());
         myFile.print("/");
         if (gps.time.second() < 10) myFile.print(F("0"));
         myFile.print(gps.time.second());
         if (gps.time.centisecond() < 10) myFile.print(F("0"));
         myFile.println(gps.time.centisecond());

   myFile.print("X: "); myFile.print(event1.magnetic.x); myFile.print("  ");
   myFile.print("Y: "); myFile.print(event1.magnetic.y); myFile.print("  ");
   myFile.print("Z: "); myFile.print(event1.magnetic.z); myFile.print("  ");myFile.print("uT");
   myFile.close(); 
     }
}
void displayGpsInfo()
{
  // Prints the location if lat-lng information was recieved
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
    Serial.print(F(","));
    Serial.print(gps.altitude.meters(),6);
  }
  // prints invalid if no information was recieved in regards to location.
  else
  {
    Serial.print(F("INVALID"));
  }
Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
 Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
  Serial.println();
  if(mag.begin())
  {
    displayCompassInfo();
  }
}

void displayCompassInfo()
{
  sensors_event_t event; 
  mag.getEvent(&event);
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  float heading = atan2(event.magnetic.y, event.magnetic.x);
 float declinationAngle = -0.041;
  heading += declinationAngle;
  if(heading < 0)
    heading += 2*PI;
  if(heading > 2*PI)
    heading -= 2*PI;
  float headingDegrees = heading * 180/M_PI; 
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500);
}

Can you re-edit your post as your code didn't quite make inside the code tags.

I think your issue may be related to opening and closing your file inside the loop(). Try opening your file once at the end of setup() and then leaving it open for writing.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.
Please post an image of your project.

Open the SD card in setup() and leave it open for the duration of the program run. Use append() to open the file. Append will create the file if it does not exists and then store the thing or If the file exists open the file so that things can be added to the file.

let me try.

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