TinyGPS
A Compact Arduino GPS/NMEA Parser
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 mandatory 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 millionths of a degree, so instead of 90°30’00″, get_position() returns a longitude value of 90,500,000, 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: TinyGPS13.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()
- Improved precision in latitude and longitude rendering. get_position() now returns angles in millionths of a degree.
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 [...]
April 27th, 2013 → 9:01 am
[...] TinyGPS is a library for Arduino that allows to use a GPS module relatively painlessly. [...]
May 16th, 2013 → 11:04 pm
[...] used the tinyGPS library to decode the NMEA GPS Data. Cooking-Hacks generously supplied both the GPS shield and SD Card [...]
July 1st, 2013 → 1:18 am
[...] using two really awesome libraries written by Mikal Hart, so make sure you have downloaded them! (TinyGPS and NewSoftSerial ) TinyGPS basically makes it easier for us to extract data like longitude and [...]
July 9th, 2013 → 12:59 pm
[...] TinyGPS | Arduiniana sentences – the number of valid $GPGGA and $GPRMC sentences processed; failed_checksum …. I am trying to input some proper NMEA commands but the response goes just like that. CHARS=0 …. 46 Trackbacks For This Post. เริ่มต้น สร้าง … [...]
July 9th, 2013 → 9:33 pm
[...] TinyGPS | Arduiniana sentences – the number of valid $GPGGA and $GPRMC sentences ….. 46 Trackbacks For This Post … and sending the bits of “Hello World” in 7N1 format. I spent … [...]
July 16th, 2013 → 12:38 pm
[...] click here to download it. You will also need to download the PWMServo library (version 2) and the TinyGPS library written by Mikal Hart. While you are at his site, thank him for his amazing work. If you [...]
July 22nd, 2013 → 12:45 pm
[...] for what I needed, and used interrupts, and other complex stuff Fortunately Time library used TinyGPS library, which is indeed very lightweight and straightforward. It also “requires” [...]
August 27th, 2013 → 4:45 am
[...] TinyGPS library [...]
September 1st, 2013 → 1:45 pm
[...] TinyGPS [...]
September 11th, 2013 → 7:49 am
[...] the next part, I am going to use one of the library available for Arduino, which is TinyGPS. You can download TinyGPS library here. This library ease your job to get all the information from [...]
October 7th, 2013 → 3:29 pm
[...] the Arduino developers for the great libraries, and to Mikal Hart in particular for his work on the TinyGPS and NewSoftSerial [...]
October 11th, 2013 → 3:42 am
[...] recently purchased one of those U-blox GPS modules from Ebay for $20 and after downloading the TinyGPS library for the Arduino, it works well once it has valid GPS data (I had to have mine close to the window) [...]
October 13th, 2013 → 2:50 pm
[...] a biblioteca TinyGPS (documentação e download da v13). Requer [...]
February 9th, 2014 → 5:43 am
[...] to the module and hooked it up to an ATmega328 running at 3.3V / 8MHz. I installed the Arduino tinygps library and uploaded one of the example sketches to the ATmega. I put my laptop, micro controller and GPS [...]
April 22nd, 2014 → 1:58 am
[...] This requires the TinyGPS and NewSoftSerial libraries from Mikal Hart: http://arduiniana.org/libraries/TinyGPS and http://arduiniana.org/libraries/newsoftserial/ [...]
May 14th, 2014 → 1:03 pm
[...] biblioteca tinyGPS sau [...]
May 29th, 2014 → 4:45 pm
[...] one extra sentence that is discarded by TinyGPS (a very cool library – check it out at http://arduiniana.org/libraries/tinygps/). I corrected that and believe the GPS delivers the optimum number of sentences at 19200 baud to [...]
June 25th, 2014 → 12:39 am
[...] fly, so thankfully there is an Arduino library to do this for us - TinyGPS. So head over to the library website, download and install the library before [...]
June 27th, 2014 → 8:46 pm
[...] fly, so thankfully there is an Arduino library to do this for us - TinyGPS. So head over to the library website, download and install the library before [...]
July 27th, 2014 → 11:26 am
[...] Eine recht praktische und kompakte Library zum Parsen von GPS/NMEA Daten: TinyGPS [...]
July 31st, 2014 → 10:07 pm
[...] Library to connect to the shield and I modified the code a little to stream incoming data into Mikal Hart’s GPS Parser Library, TinyGPS. Here is the crux of the [...]
August 24th, 2014 → 8:11 pm
[...] TinyGPS Library [...]
August 25th, 2014 → 1:45 am
[...] TinyGPS Library [...]
October 3rd, 2014 → 12:56 am
[...] comes a sample application using Spark Core and TinyGPS library. TinyGPS is a very powerful and fast NMEA GPS parser for Arduino and compatible. In this [...]
November 9th, 2014 → 2:54 pm
[...] the Arduino IDE, I shall directly skip on the coding part then. Our arduino sketch will require Tiny GPS library, which you can download it from here. Import the downloaded library to your Arduino IDE. [...]
December 27th, 2014 → 12:07 am
[...] tutorials are either too complex (i.e. include extra stuff like displays) or they don’t use TinyGPS or TinyGPS++ library. In the first step I’ve managed to get GPS working and displaying the [...]
March 1st, 2015 → 12:26 am
[...] first thing I had to do software wise was install the TinyGps library. This was hard to do as the creator’s website was iffy at the time. I ended up downloading it from github and renaming it to get rid of the dash [...]
April 22nd, 2015 → 9:06 am
[...] TinyGPS (ссылка на скачивание в середине страницы) [...]
April 22nd, 2015 → 10:44 pm
[...] requires the TinyGPS and NewSoftSerial libraries from Mikal Hart: http://arduiniana.org/libraries/TinyGPS and http://arduiniana.org/libraries/newsoftserial/ Example [...]
December 9th, 2016 → 12:41 pm
[...] ser parsed a datos de ubicación, velocidad etc. Esto lo logramos usando la TinyGPS library (http://arduiniana.org/libraries/tinygps/) que veremos en la Parte [...]
August 21st, 2017 → 2:59 am
[...] the base station. If a valid acknowledgement packet is received, the current GPS information (using TinyGPS library) is retrieved from L80 GPS module and stored. Some of you might have argue why not use the full [...]
August 30th, 2017 → 5:09 pm
[...] http://arduiniana.org/libraries/tinygps/ [...]
November 27th, 2017 → 10:22 am
[...] a biblioteca TinyGPS (documentação e download da v13). Requer [...]
December 4th, 2017 → 1:33 pm
[...] LCDUn módulo GPS en este caso el EM-411Breadboard, jumperwires y un potenciometro.Biblioteca TinyGPS.Enlace al tutorial completo.Califique esto Sample rating [...]
March 24th, 2018 → 5:26 am
[...] photo you can see the GPS shield, which has now turned up. A quick bit of poking with the lovely TinyGPS library has shown that it’s giving me location data, and as a bonus, there’s a function [...]
May 10th, 2022 → 8:03 pm
[...] Library Used : http://arduiniana.org/libraries/tinygps/ [...]