Hello, I have this code but when I ran it it puts me all the time "File not writing" and we can not see any error in the code

#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <SFE_BMP180.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>

SFE_BMP180 bmp180;
TinyGPS gps;
SoftwareSerial serialgps (4,3);
const int chipSelect = 10; //SD card CS pin connected to pin 4 of Arduino

int year;
byte month, day, hour, minute, second, hundredths;
unsigned long chars;
unsigned short sentences, failed_checksum;

void setup()
{
  // Setup Serial connection
  Serial.begin(9600);
  //Setup GPS
  serialgps.begin(9600);
  Serial.println("");
  Serial.println("GPS NEO6MV");
  Serial.println("Buscando seƱal...");
  Serial.println("");
  
  //CODI SD
  pinMode(chipSelect, OUTPUT);
  Initialize_SDcard();
  
  //CODI BMP180
  if (bmp180.begin())
    Serial.println("BMP180 iniciado correctamenten");
  else
  {
    Serial.println("Error al iniciar el BMP180");
    while(1); // bucle infinito
  }
}

void loop()
{
  //CODI GPS
  while(serialgps.available())
  {
    int c = serialgps.read();
    if (gps.encode(c))
    {
      float latitude, longitude;
      File dataFile = SD.open("Datos.txt", FILE_WRITE);
      if (dataFile)
      {
        gps.f_get_position(&latitude, &longitude);
        
        Serial.print("Latitud/Longitud: ");
        dataFile.print("Latitud/Longitud: ");
        Serial.print(latitude, 5);
        dataFile.print(latitude, 5);
        Serial.print(", ");
        dataFile.print(", ");
        Serial.println(longitude, 5);
        dataFile.println(longitude, 5);
        
        gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
        Serial.print("Fecha: "); Serial.print(day, DEC); Serial.print("/");
        Serial.print(month, DEC); Serial.print("/"); Serial.println(year); 
        
        Serial.print("Hora: "); Serial.print(hour, DEC); Serial.print(":");
        dataFile.print("Hora: "); dataFile.print(hour, DEC); dataFile.print(":");
        Serial.print(minute,DEC); Serial.print(":"); Serial.print(second,DEC);
        dataFile.print(minute,DEC); dataFile.print(":"); dataFile.print(second,DEC);
        Serial.print("."); Serial.println(hundredths,DEC);
        dataFile.print("."); dataFile.println(hundredths,DEC);

        Serial.print("Altitud (metros): "); Serial.println(gps.f_altitude());
        dataFile.print("Altitud (metros): "); dataFile.println(gps.f_altitude());
        
        Serial.print("Rumbo (grados): "); Serial.println(gps.f_course());
        dataFile.print("Rumbo (grados): "); dataFile.println(gps.f_course());

        Serial.print("Velocidad (kmph): "); Serial.println(gps.f_speed_kmph());
        dataFile.print("Velocidad (kmph): "); dataFile.println(gps.f_speed_kmph());

        Serial.print("Satelites: "); Serial.println(gps.satellites());
        dataFile.print("Satelites: "); dataFile.println(gps.satellites());

        Serial.println();
        dataFile.println();
        gps.stats(&chars, &sentences, &failed_checksum);
        dataFile.close(); //Close the file
      }
    }
  }

  char status;
  double T,P;

  status = bmp180.startTemperature();//Inicio de lectura de temperatura
  if (status != 0)
  {   
    delay(status); //Pausa para que finalice la lectura
    status = bmp180.getTemperature(T); //Obtener la temperatura
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Inicio lectura de presión
      if (status != 0)
      {        
        delay(status);//Pausa para que finalice la lectura        
        status = bmp180.getPressure(P,T); //Obtenemos la presión
        if (status != 0)
        {
          File dataFile = SD.open("Datos.txt", FILE_WRITE);
          if (dataFile)
          {
            Serial.print("Temperatura: ");
            dataFile.print("Temperatura: ");

            Serial.print(T,2);
            dataFile.print(T,2);

            Serial.print(" *C , ");
            dataFile.print(" *C , ");

            Serial.print("Presion: ");
            dataFile.print("Presion: ");

            Serial.print(P,2);
            dataFile.print(P,2);

            Serial.println(" mb");          
            dataFile.println(" mb");

            dataFile.close();
          }
          else
          {
            Serial.println("File not writing");
          }
        }      
      }      
    }   
  }
  delay(1000);     //Wait for 5 seconds before writing the next data 
}

void Initialize_SDcard()
{
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
   }
   Serial.println("Card present");
   // open the file. note that only one file can be open at a time,
   // so you have to close this one before opening another.
   File dataFile = SD.open("Datos.txt", FILE_WRITE);
   // if the file is available, write to it:
   if (dataFile)
   {
    dataFile.println("Datos:"); //Write the first row of the excel file
    dataFile.close();
    Serial.println("File writing");
   }
   else
   {
    Serial.println("File not writing");
   }
}

Show the full error just like you showed the full sketch (in a < CODE > block)

Your file "Datos.txt" could not be opened. Verify your wiring and the SD card reader has sufficient power.

Well actually if I run the code just with the BMP180 and the SD the code runs perfectly and save the information.

#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 bmp180;
const int chipSelect = 10; //SD card CS pin connected to pin 4 of Arduino
int num = 0;

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  Initialize_SDcard();
  //CODI BMP180
  if (bmp180.begin())
    Serial.println("BMP180 iniciado correctamenten");
  else
  {
    Serial.println("Error al iniciar el BMP180");
    while(1); // bucle infinito
  }
}

void loop()
{
  char status;
  double T,P;

  status = bmp180.startTemperature();//Inicio de lectura de temperatura
  if (status != 0)
  {   
    delay(status); //Pausa para que finalice la lectura
    status = bmp180.getTemperature(T); //Obtener la temperatura
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Inicio lectura de presión
      if (status != 0)
      {        
        delay(status);//Pausa para que finalice la lectura        
        status = bmp180.getPressure(P,T); //Obtenemos la presión
        if (status != 0)
        {
          File dataFile = SD.open("Datos.txt", FILE_WRITE);
          if (dataFile)
          {
            Serial.print("Temperatura: ");
            dataFile.print("Temperatura: ");

            Serial.print(T,2);
            dataFile.print(T,2);

            Serial.print(" *C , ");
            dataFile.print(" *C , ");

            Serial.print("Presion: ");
            dataFile.print("Presion: ");

            Serial.print(P,2);
            dataFile.print(P,2);

            Serial.println(" mb");          
            dataFile.println(" mb");

            dataFile.close();
          }
          else
          {
            Serial.println("File not writing");
          }
        }      
      }      
    }   
  }
  delay(1000);     //Wait for 5 seconds before writing the next data 
}

void Initialize_SDcard()
{
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
   }
   Serial.println("Card present");
   // open the file. note that only one file can be open at a time,
   // so you have to close this one before opening another.
   File dataFile = SD.open("Datos.txt", FILE_WRITE);
   // if the file is available, write to it:
   if (dataFile)
   {
    dataFile.println("Temperature,Humidity"); //Write the first row of the excel file
    dataFile.close();
    Serial.println("File writing");
   }
   else
   {
    Serial.println("File not writing");
   }
}

but when I add the GPS code is when all the code dies, so in theory the file "Datos.txt" is working well. I think the problem when I add the GPS code

May be it can be some library? idk

You have found the answer.

Whose theory? Not mine.

so the code doesn't work because the SD card hasn't sufficient powe when I add the GPS?

No, the device does not have sufficient power.

You have proven the SD code works.

ok thanks mate

Yes I did and all went good

1 Like

When you add devices, verify you have sufficient power to operate them at their maximum value, plus 10%. Your GPS is a power-hungry device.

ok I will prove it

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