I have been trying for awhile to get GPS data from the iTead GPS Shield using an Arduino Mega 2560, looked at multiple guides, used multiple different examples, also tried getting the RAW data to the Serial 1 interface with no success.
Tried 2 different GPS shields and multiple antennas, and still nothing. Could someone please have a look at the setup and code and tell me what I might be doing wrong. I have attached a photo of the Arduino, Serial Monitor and the Sketch Code file.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial serialGps(7, 6);
void setup()
{
// Serial monitor
Serial.begin(9600);
// baud rate for GPS - 38400 is prefered, but 4800 can also be used
serialGps.begin(38400);
// Comments from TinyGPS simple_test example
Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
Serial.println();
}
void loop(){
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (serialGps.available())
{
char c = serialGps.read();
// Send raw data to the terminal window
Serial.write(c);
if (gps.encode(c))
newData = true;
}
}
// Send manipulate data to the terminal window.
/*
if (newData)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
}
*/
}
1.) If you're using a Mega, do NOT use SoftwareSerial.h, just use Serial1, Serial2, etc.
2.) The GPS's default baud is 9600
Try this:
#include <SPI.h>
#include <TinyGPS.h>
TinyGPS gps;
void setup()
{
// Serial monitor
Serial.begin(9600);
// baud rate for GPS - 38400 is prefered, but 4800 can also be used
Serial1.begin(9600);
// Comments from TinyGPS simple_test example
Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
Serial.println();
}
void loop(){
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial1.available())
{
char c = Serial1.read();
// Send raw data to the terminal window
Serial.write(c);
if (gps.encode(c))
newData = true;
}
}
// Send manipulate data to the terminal window.
/*
if (newData)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
}
*/
}
If this still doesn't work, swap the TX and RX pins and try again.
Software serial does work on a Mega but as has been suggested why use it when you have the better option of using the hardware serial ports.
If you had checked the Software Serial Referance Page you would have found that only the following pins can be used for RX in software serial;
"Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)."
So pin 7 for RX was the reason why it did not work, together with the serial baud needing to be 9600.