Just a simple GPS logger using Arduino Uno, Seeed Studio SD card Shield, and Parallax 28500-RT PMB-648 GPS SiRF Internal Antenna. I hope the modified sketch is useful to someone like me who had difficulty finding a good example.
/*
This Sketch uses the TinyGPS library and SD card library to log GPS data and save it on a SD card.
Important: This version is intended for Arduino 1.0 IDE. It will
not compile in earlier versions. Be sure the following files are
present in the folder with this sketch:
TinyGPS.h
TinyGPS.cpp
keywords.txt
A revised version of the TinyGPS object library is included in the sketch folder
to avoid conflict with any earlier version you may have in the Arduino libraries
location.
*/
#include <SoftwareSerial.h>
#include "./TinyGPS.h" // Special version for 1.0
#include <SD.h> // Standard Arduino SD card library
File myFile;
TinyGPS gps;
SoftwareSerial nss(6, 255); // Yellow wire to pin 6. (Based on using
// Parallax 28500-RT PMB-648 GPS SiRF Internal Antenna)
void setup() {
Serial.begin(115200);
nss.begin(4800);
Serial.println("Reading GPS");
// Initialize SD card
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// End initialize SD Card
}
void loop() {
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 5000) { // Update every 5 seconds
if (feedgps())
newdata = true;
}
if (newdata) {
gpsdump(gps);
}
}
// Get and process GPS data
void gpsdump(TinyGPS &gps) {
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print(flat, 4);
Serial.print(", ");
Serial.println(flon, 4);
/// And write it to SD card
myFile = SD.open("tracking.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to tracking.txt...");
myFile.print(flat, 4);
myFile.print(", ");
myFile.println(flon, 4);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening tracking.txt");
}
///
}
// Feed data as it becomes available
bool feedgps() {
while (nss.available()) {
if (gps.encode(nss.read()))
return true;
}
return false;
}
I was excited to locate your code as I have been having a hard time getting my GPS and SD card to talk to each other.
I'm also new to Arduino and the code.
I have the latest Sparkfun GPS shield and SDcard shield. I have made adjustments in your code for the pin locations etc. Assuming that I have those things correct it still doesn't work.
I get to "initialization failed".
If I comment out the "return" it goes to "initialization done" and stops there.
Any suggestions or tips / Thanks
Below is your code with my changes
#include <SoftwareSerial.h>
#include < TinyGPS.cpp>
#include <TinyGPS.h>
#include <SD.h> // Standard Arduino SD card library
File myFile;
TinyGPS gps;
SoftwareSerial nss(3, 4);
///
int CS_pin = 8;
int pow_pin = 3;
///
void setup() {
Serial.begin(115200);
nss.begin(4800);
Serial.println("Reading GPS");
// Initialize SD card
Serial.print("Initializing SD card...");
pinMode(CS_pin, OUTPUT);
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);
if (!SD.begin(CS_pin)) {
Serial.println("initialization failed!");
//return;
}
Serial.println("initialization done.");
// End initialize SD Card
}
void loop() {
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 5000) { // Update every 5 seconds
if (feedgps())
newdata = true;
}
if (newdata) {
gpsdump(gps);
}
}
// Get and process GPS data
void gpsdump(TinyGPS &gps) {
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print(flat, 4);
Serial.print(", ");
Serial.println(flon, 4);
/// And write it to SD card
myFile = SD.open("LOG.csv, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to tracking.txt...");
myFile.print(flat, 4);
myFile.print(", ");
myFile.println(flon, 4);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening tracking.txt");
}
///
}
// Feed data as it becomes available
bool feedgps() {
while (nss.available()) {
if (gps.encode(nss.read()))
return true;
}
return false;
}
This is referring to the SD card initialization. Your SD card needs formatted to FAT32 if I remember correctly. Also there are a few test sketches that come with the SDFat library. I recommend getting the SDcard to work by itself with the test sketches then, return to trying the GPS logger sketch.
Hi,
Thanks for responding.
I have made a lot of ground since that post. Yes, I did separate the GPS from the SD card and was able to verify that I could write to the SDcard.
I also, pulled apart my two shields and looked over the soldering etc. In short, I finally got things working.
Right now I'm trying to figure out how to parse and write code that retrieves the GPS data and formats it in a way that is easily readable.
I keep looking through C & C++ books and references.
I really just want Longitude, Latitude, and Altitude in a simple straight line.