Problem using Sparkfun GPS and Arduino Mega

Hello.

I have been working for a few days with Arduino Mega and the GPS Logger Shield from Sparkfun. I tried to follow the guide there, but I don't manage to make it work. I connect the pins GPS-TX and GPS-RX to the digital pins 14 and 15 from Arduino Mega (manually). When I connect these pins, the 5V and GND pins too, the code always sends me 0. I mean, it prints Lat: 0, Long:0, Date: 0 and so on. I think the problem is in the code, so if someone could tell me whats wrong Id be very thankful

The code is the next:

#include <TinyGPS++.h>
TinyGPSPlus tinyGPS;

#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX A9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX A8 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial

#define gpsPort ssGPS 
#define SerialMonitor Serial

#define GPS_BAUD 9600 

void setup()
{
  SerialMonitor.begin(9600);
  gpsPort.begin(GPS_BAUD);
}

void loop()
{
  printGPSInfo();

  delay(1000);
}

void printGPSInfo()
{
  SerialMonitor.print("Lat: "); SerialMonitor.println(tinyGPS.location.lat(), 6);
  SerialMonitor.print("Long: "); SerialMonitor.println(tinyGPS.location.lng(), 6);
  SerialMonitor.print("Alt: "); SerialMonitor.println(tinyGPS.altitude.feet());
  SerialMonitor.print("Course: "); SerialMonitor.println(tinyGPS.course.deg());
  SerialMonitor.print("Speed: "); SerialMonitor.println(tinyGPS.speed.mph());
  SerialMonitor.print("Date: "); printDate();
  SerialMonitor.print("Time: "); printTime();
  SerialMonitor.print("Sats: "); SerialMonitor.println(tinyGPS.satellites.value());
  SerialMonitor.println();
}

// printDate() formats the date into dd/mm/yy.
void printDate()
{
  SerialMonitor.print(tinyGPS.date.day());
  SerialMonitor.print("/");
  SerialMonitor.print(tinyGPS.date.month());
  SerialMonitor.print("/");
  SerialMonitor.println(tinyGPS.date.year());
}

// printTime() formats the time into "hh:mm:ss", and prints leading 0's
// where they're called for.
void printTime()
{
  SerialMonitor.print(tinyGPS.time.hour());
  SerialMonitor.print(":");
  if (tinyGPS.time.minute() < 10) SerialMonitor.print('0');
  SerialMonitor.print(tinyGPS.time.minute());
  SerialMonitor.print(":");
  if (tinyGPS.time.second() < 10) SerialMonitor.print('0');
  SerialMonitor.println(tinyGPS.time.second());
}

1.) You might try switching RX and TX
2.) Some hobby GPS receivers can take a really long time to get their first fix. Make sure you are powering it off of a stable and quiet power supply, have it in direct view of the sky, and wait a few hours for the fix.

The TX ans RX pins in the code are not the pins you are connecting to.

Fakiko:
I have been working for a few days with Arduino Mega and the GPS Logger Shield from Sparkfun. I tried to follow the guide there, but I don't manage to make it work.

If Sparkfun publish an example bit of code for their product, you would expect it to work, when used as intended.

That shield is however designed for a UNO style Arduino, you would not expect it to work on a Mega as is.

Fakiko:
I connect the pins GPS-TX and GPS-RX to the digital pins 14 and 15 from Arduino Mega (manually).

It would be an idea to have those connections configured in the code, remember its a Mega you are using.

its also work pointing out that you are attempting to use software serial on one of the Megas hardware serial ports.

Thanks everybody for the answers.

Gerihatrick:
The TX ans RX pins in the code are not the pins you are connecting to.

As you said, the Tx and RX pins were not the same. Thats because I first connected them to A8 and A9 pins. But now I fixed it. Thanks!

srnet:
It would be an idea to have those connections configured in the code, remember its a Mega you are using.

its also work pointing out that you are attempting to use software serial on one of the Megas hardware serial ports.

Yes, thats why I changed the pins to A8 and A9. The guide is for Arduino Uno as you said, so I had to change those pins. However, it still doesn't work.

Fakiko:
I think the problem is in the code, so if someone could tell me whats wrong Id be very thankful

You can troubleshoot the code yourself.

Since your using the TnyGPS++ library, first load up one of the code examples in that library, they will be well tested and you can expect them to work if the GPS is hooked up correctly.

Then compare the libraries working example code with the code you posted, is anything missing ?

And where did your code come from ?

srnet:
You can troubleshoot the code yourself.

Since your using the TnyGPS++ library, first load up one of the code examples in that library, they will be well tested and you can expect them to work if the GPS is hooked up correctly.

Then compare the libraries working example code with the code you posted, is anything missing ?

And where did your code come from ?

I used the code from the sparkfun guide. I tried to use the code examples, but it still doesn't work. One of them shows the RX/TX information, and it show something like this:

The RX pin seems to read information from the GPS, but as you can see it doesn't show anything.

The code from the example is the following:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin =A8, TXPin = A9;
static const uint32_t GPSBaud = 4800;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);

  Serial.println(F("FullExample.ino"));
  Serial.println(F("An extensive example of many interesting TinyGPS++ features"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by Mikal Hart"));
  Serial.println();
  Serial.println(F("Sats HDOP  Latitude   Longitude   Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum"));
  Serial.println(F("           (deg)      (deg)       Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail"));
  Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
}

void loop()
{
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printInt(gps.location.age(), gps.location.isValid(), 5);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);

  unsigned long distanceKmToLondon =
    (unsigned long)TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      LONDON_LAT, 
      LONDON_LON) / 1000;
  printInt(distanceKmToLondon, gps.location.isValid(), 9);

  double courseToLondon =
    TinyGPSPlus::courseTo(
      gps.location.lat(),
      gps.location.lng(),
      LONDON_LAT, 
      LONDON_LON);

  printFloat(courseToLondon, gps.location.isValid(), 7, 2);

  const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);

  printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);

  printInt(gps.charsProcessed(), true, 6);
  printInt(gps.sentencesWithFix(), true, 10);
  printInt(gps.failedChecksum(), true, 9);
  Serial.println();
  
  smartDelay(1000);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
}

// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
  
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
}
static const uint32_t GPSBaud = 4800;

change that to:

static const int GPSBaud = 9600;

and report back

Okay I have just done what you said and now it worked!!!! I cant express how thankful I am. I compared this code from mine and I manage it to work!
My last question is, this code has Serial.begin(115200), while the GPS has 9600. Can I put the Serial.begin(9600) too? Or it has to be faster than the GPS?

Thanks !!

capital S Serial.begin(baud rate) sets up the Serial Monitor connection. it can be set to any of the values in the middle box of the three boxes at the bottom of Serial Monitor, as long as the value in the parentheses in Serial.begin( ); matches the value in the Serial Monitor baud rate box.

Okay, it is clear for me. Thanks again!!! Problem solved :slight_smile: