Problem with GPS data logger

Hello,

I’m trying to write gps latitude on a micro sd card. Separately, gps and sd card works fine, but together, after sd is initialized, gps latitude becomes zero, like 0,00000, and zero is written to sd card.
I use arduino uno, neo6mv2 gps and sd card shield.

My code

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

File myFile;

static const int RXPin = 10, TXPin = 11;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;
unsigned long lastUpdateTime = 0;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);
  Serial.print("Initializing SD card..."); 
  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("card initialized.");
       Serial.println(gps.location.lat(), 6);
  
}

void loop()
{
   

 while (ss.available() > 0)
    gps.encode(ss.read());
    
  if (millis() - lastUpdateTime >= 5000){
      lastUpdateTime = millis();
      
   
myFile = SD.open("test12.txt", FILE_WRITE); 
  
    Serial.print("Writing to test.txt...");
    
    myFile.println(gps.location.lat(), 6);
    // close the file:
    myFile.close();
    Serial.println("done.");
       }  
}

When I remove this part from sketch:

if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }

latitude is good again.
How to solve this problem?
thanks

 while (ss.available() > 0)
    gps.encode(ss.read());

The encode() method returns true or false, based on whether the character you passed it completes a sentence, or not. You should NOT be ignoring that value. You should NOT be writing to the SD card if you are not getting good data.

You SHOULD be printing the character to the serial port as well as passing it to the encode() method, so you have some idea whether the simple act of connecting the SD card (which uses more than just pin 8 ) to the Arduino has affected the GPS’s ability to deliver good data.

I can tell you, for a fact, that it has. If YOU did some research, and determined what kind of device your SD card reader/writer is (Hint: SPI) and which pins it needed to operate (Hint: 10, 11, 12, and 13), you’d have a clue as to what the problem is.

There are several ways to connect a GPS device. Like PaulS said, the pins you are using for the GPS are… wrong. Read this for all the alternatives. SoftwareSerial is the worst choice, even if you had used other pins.

And you might be interested in my NeoGPS library. It’s smaller, faster, more reliable and more accurate than all other GPS libraries. There are other advantages, related to the GPS quiet time and printing too much data. Even if you don’t use it, there are lots of tips on the Troubleshooting page and the Installation page.

Here is a NeoGPS/AltSoftSerial version of your sketch:

#include <NMEAGPS.h>
#include <SPI.h>
#include <SD.h>

File myFile;

// The serial connection to the GPS device.  PICK ONE:

// BEST: connect GPS to pins 0 & 1.
//             Requires disconnecting pin 0 to upload new sketch over USB.
//#define gpsPort Serial // just an alias for Serial

// 2nd BEST: connect GPS to pins 8 & 9 (on an UNO)
#include <AltSoftSerial.h>
AltSoftSerial gpsPort;

// 3rd BEST: NeoSWSerial on any two pins (except 0 & 1)
//static const int RXPin = 10, TXPin = 11;
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort(RXPin, TXPin);

// NOT RECOMMENDED: SoftwareSerial

static const uint32_t GPSBaud = 9600;

NMEAGPS gps; // The parser
gps_fix fix; // A structure with all the parsed values

unsigned long lastUpdateTime = 0;
unsigned char fixCount = 0;  // The number of fixes we have received.

void setup()
{
  Serial.begin(9600);
  gpsPort.begin(GPSBaud);

  Serial.print( F("Initializing SD card...") ); // F macro saves RAM!
  if (!SD.begin(8)) {
    Serial.println( F("initialization failed!") );
    return;
  }
  Serial.println( F("card initialized.") );

  // Open the file *once*
  myFile = SD.open("test12.txt", FILE_WRITE);

  // No GPS data has been processed yet!
  //Serial.println( fix.latitude(), 6 );
}

void loop()
{
  if (gps.available( gpsPort )) { // read and parse GPS characters

    //  A complete GPS fix is ready, once per second.
    fix = gps.read();
    fixCount++;

    // Have we received 5 fixes?  This is also 5 seconds.
    if (fixCount >= 5) {
      fixCount = 0; // reset

      Serial.print( F("Writing to test.txt... ") );

      // NOTE:  You may want to make sure the GPS really has a location:
      if (fix.valid.location) {
        Serial.print( fix.latitude(), 6 );
        myFile.println( fix.latitude(), 6 );
      } else {
        Serial.print( '?' );
        myFile.println( '?' );
      }

      // Make sure this location gets saved on the card.
      //   This slows things down a little, but it guarantees that
      //   the card will have the latest data when you power off
      //   the Arduino to remove the card.  Some people use a
      //   button press to close the file and stop logging.
      myFile.flush();
      Serial.println( F(" done.") );
    }
  }
}

Your original version uses 18850 bytes of program space and 1310 bytes of RAM.
The NeoGPS version uses 18286 bytes of program space and 1187 bytes of RAM.

If you’d like to try it, NeoGPS and AltSoftSerial are available from the Arduino IDE Library Manager, under the menu Sketch → Include Library → Manage Libraries.

Cheers,
/dev

Many thanks to both, now I understand better what's happening here :slight_smile: