I've programmed my Arduino Ethernet to receive data from a Skytraq Venus GPS receiver and separate it using TinyGPS, then store it on an SD card. With the SD card inserted, everything else except the time is totally wrong. Once I remove the SD card and reset, the longitude and latitude are still wrong but everything else is correct. I've tried supplying more power, replacing the receiver, antenna, the arduino itself, even switched the serial ports I use but I can't seem to find a solution.
Here's a link for the TinyGPS library and a link for the NewSoftSerial library. And for the schematics, I haven't drawn any yet but I'm using the Arduino Ethernet's SD port as my SD shield. My Skytraq GPS receiver's RX pin is connected to pin 2 on the arduino ethernet and the TX pin is connected to pin 3 on the ethernet. (Ethernet TX/RX has been set to pin 3 and 2 respectively.)
I'm using the Sparkfun Venus GPS with SMA connector from sparkfun.com. I would post the link but it says I need at least 10 reputation to post more than two links. Serial signals at 3.3V and GPS module is a 3.3V module. I use the same sketch with and without the SD Card. SD.begin is called just once and Serial.begin is also called once.
Here's my code:
#include <SoftwareSerial.h> //NewSoftwareSerial Library
#include <TinyGPS.h> //TinyGPS Library
#include <SPI.h> //This Library is used by the SD library.
//Must be defined before SD library to prevent errors.
#include <SD.h> //SD Card Library
//NB: To avoid SD Card Errors, use an SD Card whose size is at most 2GB
//and use FAT16 format. Arduino has problems with FAT32 and cards>2GB.
#define RXPIN 2
#define TXPIN 3
const int chipSelect = 4; //This selects the SD card port for read/write
#define GPSBAUD 9600
File logs; //Used to write to SD card later
TinyGPS gps;
//Initialize the Software Serial to recognize
//our UART pins (Read(RX) and Write(TX))
SoftwareSerial read_gps(RXPIN, TXPIN);
void getdata(TinyGPS &gps);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Start the serial port
read_gps.begin(GPSBAUD); //Start reading from the gps
pinMode(chipSelect, OUTPUT); //Make pin 4 an output pin
pinMode(10, OUTPUT); //Pin 10 must always be set to output to avoid errors
digitalWrite(10, HIGH); //Pin 10 must always be high to prevent errors.
Serial.println("");
Serial.println("Awaiting Lock");
Serial.println("");
//Initialize the SD Card
SD.begin();
Serial.println("Initializing SD Card. Please Wait.");
if(!SD.begin(4)){//If card not available or card not recognized
Serial.println("Failed.");
return;
}
Serial.println("Card Initialized");
}
//the loop function is used to read the data from the gps,
//check for valid data and then separate the data based on our specs.
void loop() {
// put your main code here, to run repeatedly:
while(read_gps.available()){//as data is received from the gps
int c = read_gps.read();
if(gps.encode(c)){//check if new valid data available
getgps(gps); //read the valid gps data and process
}
}
}
void getgps(TinyGPS &gps)
{
float latitude, longitude;
gps.f_get_position(&latitude, &longitude); //Feature of tinygps.
//Gets the latitude and longitude
Serial.print("Latitude: "); Serial.print(latitude);
Serial.print("\nLongitude: "); Serial.print(longitude);
int yr;
byte mnth, dy, hr, minu, sec, hundredths;
gps.crack_datetime(&yr, &mnth, &dy, &hr, &minu, &sec, &hundredths); //Gets the date and time and separates them
//into individual components.
Serial.print("\nDate: "); Serial.print(mnth, DEC); Serial.print("/");
Serial.print(dy, DEC); Serial.print("/"); Serial.print(yr);
Serial.print(" \nTime: "); Serial.print(hr, DEC); Serial.print(":");
Serial.print(minu, DEC); Serial.print(":"); Serial.print(sec, DEC);
Serial.print("."); Serial.println(hundredths, DEC);
Serial.print("Altitude(metres): "); Serial.println(gps.f_altitude());
Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
Serial.println();
logs = SD.open("datalogs.csv", FILE_WRITE); //open the datalog file on the sd card. If it doesn't exist on the card, create it.
if(logs){//Start writing data to SD Card
Serial.print("Writing to datalogs...");
logs.println("\nLatitude: "); logs.print(latitude);
logs.print("\nLongitude: "); logs.print(longitude);
logs.print("\nDate: "); logs.print(mnth, DEC); logs.print("/");
logs.print(dy, DEC); logs.print("/"); logs.print(yr);
logs.print(" \nTime: "); logs.print(hr, DEC); logs.print(":");
logs.print(minu, DEC); logs.print(":"); logs.print(sec, DEC);
logs.print("."); logs.println(hundredths, DEC);
logs.print("Altitude(metres): "); logs.println(gps.f_altitude());
logs.print("Speed(kmph): "); logs.println(gps.f_speed_kmph());
logs.print("\n");
logs.println();
logs.close(); //close the file
Serial.println("done.");
Serial.println("\n");
}
else{
Serial.println("Error opening file.");
}
unsigned long chars;
unsigned short sentences, failed_checksum;
gps.stats(&chars, &sentences, &failed_checksum);
}