Reference
Last revision 2026/01/08
Library
To use, simply create an instance of an object like this:
#include "TinyGPS.h"
TinyGPS gps;
Feed the object serial NMEA data one character at a time using the encode() method. (TinyGPS does not handle retrieving serial data from a GPS unit.) When encode() returns “true”, a valid sentence has just changed the TinyGPS object’s internal state.
For example:
void loop()
{
while (Serial.available())
{
int c = Serial.read();
if (gps.encode(c))
{
// process new gps info here
}
}
}
You can then query the object to get various tidbits of data. To test whether the data returned is stale, examine the (optional) parameter “fix_age” which returns the number of milliseconds since the data was encoded.
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
// time in hhmmsscc, date in ddmmyy
gps.get_datetime(&date, &time, &fix_age);
// returns speed in 100ths of a knot
speed = gps.speed();
// course in 100ths of a degree
course = gps.course();
Related Document
Was this article helpful?
