Gps.encode(); never returning true

Hi! I've recently been trying to use a gps component with the tinygps library. I tried to use its examples, but I have found that the conditional gps.encode() never returns true. I keep the gps outside in an open area, and it used to work with the Adafruit gps library. I am using the ultimate breakout v3 GPS, and I have my RX pin connected to 7 and my TX pin to 8. How can I fix this?

This is my full sketch(the simple_test example with a few changes):

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS 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).
*/

TinyGPS gps;
SoftwareSerial ss(8, 7);

void setup()
{
  Serial.begin(115200);
  ss.begin(4800);
  
  Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  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 (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  //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());
 // }
  
  gps.stats(&chars, &sentences, &failed);
  Serial.print(" CHARS=");
  Serial.print(chars);
  Serial.print(" SENTENCES=");
  Serial.print(sentences);
  Serial.print(" CSUM ERR=");
  Serial.print(failed);
  Serial.print(" ");
  Serial.println(ss.available());
  char c = ss.read();
  Serial.print(" ");
  Serial.println(ss.encode(c));
  if (chars == 0)
    Serial.println("** No characters received from GPS: check wiring **");
}

Are you absolutely certain that this Baud rate is correct? Post a link to the product page for the GPS module.

  ss.begin(4800);

I have my RX pin connected to 7 and my TX pin to 8

Please explain what that means. The SoftwareSerial connections are defined the other way around in the line below:

SoftwareSerial ss(8, 7);

Test the GPS outdoors, just printing the raw NMEA data. If it does not have a satellite fix, that will be immediately obvious.

This echo program will do:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 7); //GPS (RX, TX) check correct pin assignments and baud rate

void setup() {
  mySerial.begin(9600); //CHECK
  Serial.begin(115200); 
  Serial.println("GPS start");
}

void loop() {
  while (mySerial.available()) {
    Serial.write(mySerial.read());
  }
}

This program returns this(after about a minute of printing):
(picture that I removed as to not reveal my location)

That looks good. Which Baud rate and connections are you now using?

I am using the ones used in your echo program(reading the serial at 115200).

Then you had the wrong Baud rate in your original program.

1 Like

I am testing it again with the correct one. By the way, should I remove the image of the gps data that I sent? Would it reveal any information?

The GPS data tells us your current location, to within a few meters.

1 Like

Ok. It works! Thanks a lot!

If gps.encode() returns false it means there are no valid NMEA sentences to read or the baud rate is wrong.

Even if the GPS is indoors, in a cave and has no antenna, it should still produce NMEA sentences, but there will be no fix data.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.