TinyGPS
A Compact Arduino GPS/NMEA Parser
Note: TinyGPS 12 is now available. TinyGPS 12 provides several new features:
- satellites() – Number of satellites counter from $GPRMC
- hdop() – Horizontal Dilution of Precision from $GPRMC — the smaller this value is the more confidence you have in the accuracy of the location reading.
- course_to() – another Maarten Lamers’ borrowing that complements his distance_to
- cardinal() – a nice little static function by Matt Monson that converts a course to a compass direction like “SSE”.
Note: TinyGPS 11 is now Arduino 1.0 compatible.
TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want – position, date, time, altitude, speed and course – without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any floating point dependency and ignores all but a few key GPS fields.
Usage
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:
#define RXPIN 3
#define TXPIN 2
SoftwareSerial nss(RXPIN, TXPIN);
void loop()
{
while (nss.available())
{
int c = nss.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();
Statistics
The stats method provides a clue whether you are getting good data or not. It provides statistics that help with troubleshooting.
// statistics gps.stats(&chars, &sentences, &failed_checksum);
-
chars - the number of characters fed to the object
-
sentences - the number of valid $GPGGA and $GPRMC sentences processed
-
failed_checksum - the number of sentences that failed the checksum test
Integral values
Values returned by the core TinyGPS methods are integral. Angular latitude and longitude measurements, for example, are provided in units of 10-5 degrees, so instead of 90°30’00″, get_position() returns a longitude value of 9050000, or 90.5 degrees. But…
Using Floating Point
…for applications which are not resource constrained, it may be more convenient to use floating-point numbers. For these, TinyGPS offers several inline functions that return more easily-managed data. Don’t use these unless you can afford to link the floating-point libraries. Doing so may add 2000 or more bytes to the size of your application.
float flat, flon; // returns +/- latitude/longitude in degrees gps.f_get_position(&flat, &flon, &fix_age); float falt = gps.f_altitude(); // +/- altitude in meters float fc = gps.f_course(); // course in degrees float fk = gps.f_speed_knots(); // speed in knots float fmph = gps.f_speed_mph(); // speed in miles/hr float fmps = gps.f_speed_mps(); // speed in m/sec float fkmph = gps.f_speed_kmph(); // speed in km/hr
Date/time cracking
For more convenient access to date/time use this:
int year; byte month, day, hour, minutes, second, hundredths; unsigned long fix_age; gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &fix_age);
Establishing a fix
TinyGPS objects depend on an external source, i.e. its host program, to feed valid and up-to-date NMEA GPS data. This is the only way to make sure that TinyGPS’s notion of the “fix” is current. Three things must happen to get valid position and time/date:
- You must feed the object serial NMEA data.
- The NMEA sentences must pass the checksum test.
- The NMEA sentences must report valid data. If the $GPRMC sentence reports a validity of “V” (void) instead of “A” (active), or if the $GPGGA sentence reports fix type “0″ (no fix) then those sentences are discarded.
To test whether the TinyGPS object contains valid fix data, pass the address of an unsigned long variable for the “fix_age” parameter in the methods that support it. If the returned value is TinyGPS::GPS_INVALID_AGE, then you know the object has never received a valid fix. If not, then fix_age is the number of milliseconds since the last valid fix. If you are “feeding” the object regularly, fix_age should probably never get much over 1000. If fix_age starts getting large, that may be a sign that you once had a fix, but have lost it.
float flat, flon;
unsigned long fix_age; // returns +- latitude/longitude in degrees
gps.f_get_position(&flat, &flon, &fix_age);
if (fix_age == TinyGPS::GPS_INVALID_AGE)
Serial.println("No fix detected");
else if (fix_age > 5000)
Serial.println("Warning: possible stale data!");
else
Serial.println("Data is current.");
Interfacing with Serial GPS
To get valid and timely GPS fixes, you must provide a reliable NMEA sentence feed. If your NMEA data is coming from a serial GPS unit, connect it to Arduino’s hardware serial port, or, if using a “soft” serial port, make sure that you are using a reliable SoftSerial library. As of this writing (Arduino 0013), the SoftwareSerial library provided with the IDE is inadequate. It’s best to use my NewSoftSerial library, which builds upon the fine work ladyada did with the AFSoftSerial library.
Library Version
You can retrieve the version of the TinyGPS library by calling the static member library_version().
int ver = TinyGPS::library_version();
Resource Consumption
Linking the TinyGPS library to your application adds approximately 2500 bytes to its size, unless you are invoking any of the f_* methods. These require the floating point libraries, which might add another 600+ bytes.
Download
The latest version of TinyGPS is available here: TinyGPS12.zip
Change Log
- initial version
- << streaming, supports $GPGGA for altitude, floating point inline functions
- also extract lat/long/time from $GPGGA for compatibility with devices with no $GPRMC
- bug fixes
- API re-org, attach separate fix_age’s to date/time and position.
- Prefer encode() over operator<<. Encode() returns boolean indicating whether TinyGPS object has changed state.
- Changed examples to use NewSoftSerial in lieu of AFSoftSerial; rearranged the distribution package.
- Greater precision in latitude and longitude. Angles measured in 10-5 degrees instead of 10-4 as previously. Some constants redefined.
- Minor bug fix release: the fix_age parameter of get_datetime() was not being set correctly.
- Added Maarten Lamers’ distance_to() as a static function.
- Arduino 1.0 compatibility
- Added satellites(), hdop(), course_to(), and cardinal()
Acknowledgements
Many thanks to Arduino forum users mem and Brad Burleson for outstanding help in alpha testing this code. Thanks also to Maarten Lamers, who wrote the wiring library that originally gave me the idea of how to organize TinyGPS. Thanks also to Dan P. for suggesting that I increase the lat/long precision in version 8. Thanks to many people who suggested new useful features for TinyGPS, especially Matt Monson, who wrote some nice sample code to do so.
All input is appreciated.
Mikal Hart

March 2nd, 2009 → 1:47 pm
[...] http://www.sundial.org/arduino/?page_id=3 [...]
March 14th, 2009 → 11:57 am
[...] won’t. I answer a few questions on the Arduino microcontroller forum and post an update to a library I [...]
June 9th, 2009 → 3:08 pm
[...] this journey I also looked into using the TinyGPS library. While it does a nice job of handling the core GPS parsing, I still had the primary desire to log [...]
September 20th, 2009 → 9:05 am
[...] serielle Verbindung werden die Daten ans Display geschickt. Verwendet werden die Bibliotheken TinyGPS und [...]
January 20th, 2010 → 1:08 am
[...] Arduino developers for the great librarires, and to Mikal Hart in particular for his work on the TinyGPS and NewSoftSerial [...]
July 25th, 2010 → 6:03 am
[...] and sending the bits of “Hello World” in 7N1 format. I spent some time with the TinyGPS and NewSoftSerial libraries from Mikal Hart, and got the parsing working nicely and building an [...]
July 25th, 2010 → 11:33 am
[...] can have the LCD output latitude, longitude, or whatever. You’ll need the TinyGPS library from Arduiniana downloaded and installed for it to work. They suggest using NewSoftSerial, but I couldn’t get [...]
September 7th, 2010 → 7:40 pm
[...] EEPROM so they are persistent between power cycles. The sketch uses Mikal Hart's excellent TinyGPS library and includes code from the ArduPilot Projectfor [...]
September 11th, 2010 → 2:16 am
[...] uses a TinyGPS library for easier GPS shield access. So device can be used as normal GPS tracking device the only [...]
September 20th, 2010 → 12:12 am
[...] point you will need to install two libraries into the Arduino software – NewSoftSerial and TinyGPS. Extract the folders into the libraries folder within your arduino-001x [...]
October 7th, 2010 → 10:43 am
[...] sketch uses Mikal Hart’s excellent TinyGPS library and includes code from the ArduPilot Project for [...]
February 15th, 2011 → 9:06 am
[...] 2 [...]
March 2nd, 2011 → 7:52 am
[...] attached code should pretty much speak for itself. Using the TinyGPS library, your current position is taken and the direction to the final location is calculated. [...]
May 15th, 2011 → 10:43 pm
[...] pull-down resistor). You will need to install the SdFAT library, NewSoftSerial library, TinyGPS library and the SdFat library if not already [...]
June 11th, 2011 → 10:23 pm
[...] http://arduiniana.org/libraries/tinygps/ USBシリアルを使っているとTX,RXが使えませんが、 [...]
June 21st, 2011 → 12:20 am
[...] Mikal Hart’s tinyGPS library; [...]
July 1st, 2011 → 10:33 pm
[...] how to send raw GPS output to a NMEA 0183 message parser. For the following code example I used Mikal Hart’s TinyGps library. Since the library itself is not handling serial input, it was only necessary to make changes in [...]
September 20th, 2011 → 11:45 pm
[...] easy developing of the necessary Arduino application I used an existent dedicated library: TinyGPS (http://arduiniana.org/libraries/tinygps/), which help us to parse all the packets received on the serial port from the GpsBee module. Ok, so [...]
November 20th, 2011 → 4:37 pm
[...] and gyroscope package and slapped together a simple SD card interface. The libraries I used were TinyGPS and fat16lib (for SD card use). Weather_Balloon_Code and schematic in case you’d like to [...]
January 30th, 2012 → 4:57 pm
[...] forget the 10k ohm pull-down resistor). You will need to install NewSoftSerial library, TinyGPS library and the SdFat library if not already [...]
April 2nd, 2012 → 1:08 am
[...] [...]
April 7th, 2012 → 5:57 pm
[...] it’s pretty simple. It only does those functions, but with the help of the truly fantastic TinyGPS library, I have a destination function in the [...]
April 10th, 2012 → 2:59 am
[...] TinyGPS for Arduino [...]
April 29th, 2012 → 6:49 am
[...] tinygps [...]
May 4th, 2012 → 3:53 am
[...] ちなみに、データ処理部分は、TinyGPS Libraryなる便利なものがあったので、それを使ってみた。 [...]
May 21st, 2012 → 3:20 am
[...] Библиотека для работы с GPS/NMEA для Arduino (TinyGPS) [...]
June 5th, 2012 → 1:19 am
[...] Download TinyGPS her [...]
July 6th, 2012 → 12:58 am
[...] Working with Arduino and TinyGPS [...]
July 6th, 2012 → 12:59 am
[...] Working with Arduino and TinyGPS [...]
July 16th, 2012 → 9:33 pm
[...] used the tinyGPS library to decode the NMEA GPS Data. Cooking-Hacks generously supplied both the GPS shield and SD Card [...]
August 2nd, 2012 → 11:33 am
[...] the TinyGPS library: http://arduiniana.org/libraries/tinygps/ Share this: Written by andrewhodel Posted in Engineering, [...]
August 18th, 2012 → 4:28 am
[...] “TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want ñ position, date, time, altitude, speed and course ñ without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any floating point dependency and ignores all but a few key GPS fields.” – TinyGPS website [...]
August 18th, 2012 → 5:06 am
[...] “TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want ñ position, date, time, altitude, speed and course ñ without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any floating point dependency and ignores all but a few key GPS fields.” – TinyGPS website [...]
August 19th, 2012 → 12:36 pm
[...] “TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want ñ position, date, time, altitude, speed and course ñ without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any floating point dependency and ignores all but a few key GPS fields.” – TinyGPS website [...]
September 18th, 2012 → 2:17 pm
[...] a Base y testear el alcance de las antenas, el proceso lo he ejecutado en el Base. He utilizado la librería TinyGPS que aparentemente funciona bastante bien, y los programas usados han sido los [...]
October 6th, 2012 → 5:16 am
[...] (while adding more functionality) as we also need use the SD library for logging and also the TinyGPS library for GPS NMEA output [...]
October 17th, 2012 → 5:02 am
[...] project makes use of the TinyGPS library. It provides a useful parsing library with easy access to the data returned by the Serial [...]
October 17th, 2012 → 4:03 pm
[...] not hard to parse it yourself, but why go to the effort when there are libraries like TinyGPS that can do it for [...]
October 22nd, 2012 → 8:33 pm
[...] the next while tweaking things. During that time, we rewrote the code (attached below) to use the TinyGPS library and wired the switch up. For the switch, it was simply a matter of connecting the common pin on the [...]
November 2nd, 2012 → 12:42 pm
[...] Estava procurando uma biblioteca para interfacear com o modulo EM-411 mas não queria algo muito complexo, apenas algo que retornasse a distancia entre duas lat/long e o angulo entre elas. Então encontrei o projeto TinyGPS. [...]
November 5th, 2012 → 3:20 pm
[...] to interpret the information the GPS sends out ourselves as there’s a really helpful libraryTinyGPS that will do the hard work for [...]
December 15th, 2012 → 5:57 am
[...] Works with Arduino TinyGPS Library http://arduiniana.org/libraries/tinygps/ [...]
December 21st, 2012 → 5:29 am
[...] “TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want ñ position, date, time, altitude, speed and course ñ without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any floating point dependency and ignores all but a few key GPS fields.” – TinyGPS website [...]
February 12th, 2013 → 5:22 am
[...] tutorial video from Jeremy Blum to help us work out the GPS module, in which he referred us to the TinyGPS arduino library which we found lovely to use. With the help of Jeremy, and the example sketches with the library, [...]
February 28th, 2013 → 3:27 am
[...] TinyGPS Arduino library makes it very simple to make your GPS do something useful, much thanks… Hope [...]
April 18th, 2013 → 9:36 am
[...] is standard 9600 baud serial NMEA sentences. There’s an excellent GPS NMEA decoder library for Arduino here which works with this [...]