GPS Logger not writing to SD Card

I am building a GPS Logger and use the following code below, but nothing writes to the SD Card, works in an SD Test, but not when using code below

// GPS Data logged into an SD Card
// Written by Adrianos Botis e-mail:adrianosbotis@gmail.com
//* SD card attached to SPI bus as follows:
//** MOSI - pin 11
//** MISO - pin 12
//** CLK - pin 13
//** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include<SPI.h>

// Choose two Arduino pins to use for softwarge serial
int RXPin = 2;
int TXPin = 3;
const int chipSelect = 4;
int GPSBaud = 9600;

TinyGPSPlus gps;

SoftwareSerial gpsSerial(RXPin, TXPin);

void setup()
{
// Start the software serial port at the GPS's default baud
gpsSerial.begin(GPSBaud);
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");

if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void loop()
{
String dataString = "";
String dataString2 = "";
String dataString3 = "";
String dataString4 = "";
// This sketch displays information every time a new sentence is correctly encoded.
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();

if (gps.location.isValid())
{
dataString = String(gps.altitude.meters(), 3);
dataString2 = String(gps.location.lat(), 6);
dataString3 = String(gps.location.lng(), 6);

File dataFile = SD.open("GpsDat.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.print("Altitude: ");
dataFile.println(dataString);
dataFile.print("Longtitude: ");
dataFile.println(dataString2);
dataFile.print("Latitude: ");
dataFile.print(dataString3);
dataFile.print("Time: ");
if (gps.time.hour() < 10) dataFile.print(F("0"));
dataFile.print(gps.time.hour());
dataFile.print(":");
if (gps.time.minute() < 10) dataFile.print(F("0"));
dataFile.print(gps.time.minute());
dataFile.print(":");
if (gps.time.second() < 10) dataFile.print(F("0"));
dataFile.print(gps.time.second());
dataFile.print(".");
if (gps.time.centisecond() < 10) dataFile.print(F("0"));
dataFile.println(gps.time.centisecond());
dataFile.println();
dataFile.close();

}
else
{
Serial.println("Failed to open file");
Serial.println();
}
}
else
{
Serial.println("Location: Not Available");
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.

  // If 5000 milliseconds pass and there are no characters coming in

// over the software serial port, show a "No GPS detected" error
if (millis() > 50000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while(true);
}
}

void displayInfo()
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
Serial.print("Number of Satellites: ");
Serial.println(gps.satellites.value(), 6);
}
else
{
Serial.println("Location: Not Available");
}

Serial.print("Date: ");
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.println(gps.date.year());
Serial.println();
Serial.print("Number of Satellites: ");
Serial.println(gps.satellites.value(), 6);
}
else
{
Serial.println("Not Available");
}

Serial.print("Time: ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(":");
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(".");
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.println(gps.time.centisecond());
}
else
{
Serial.println("Not Available");
}

Serial.println();
Serial.println();
delay(500);
}

What do the debug prints show?

Please remember to use code tags when posting code.

Maybe also a good idea to run the IDE's auto format tool on the code.

Why are you using Strings ?

To save the Longitude to the SD card file just do for example;

dataFile.print(gps.location.lat(),6);

In addition, the above will, for every sentence that arrives display a pile of stuff on the serial monitor and write a heap of stuff to SD card. On most GPSs there are about 10 sentences a second and only two of them contain location data, so most of the time you displaying and writing stuff to SD that has not changed.

Then there is the time one of these updates takes, the software serial buffer is only 32 bytes long which @ 9600baud is around 33mS, so if all the serial printing and SD writing takes longer than that you will miss sentences from the GPS, you might never get a fix ........... so you need to re-organise how your reading the GPS.

I suggest you look at ExFatDatalogger which is a low latency data logger.

You don't say which controller you are using. It might be a tight fit, RAM wise, if your are using an Uno. I had to switch to a Mega to fit the data logger and a 9-DOF sensor together.

just my 2d's worth

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