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:

  1. You must feed the object serial NMEA data.
  2. The NMEA sentences must pass the checksum test.
  3. 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

  1. initial version
  2. << streaming, supports $GPGGA for altitude, floating point inline functions
  3. also extract lat/long/time from $GPGGA for compatibility with devices with no $GPRMC
  4. bug fixes
  5. API re-org, attach separate fix_age’s to date/time and position.
  6. Prefer encode() over operator<<. Encode() returns boolean indicating whether TinyGPS object has changed state.
  7. Changed examples to use NewSoftSerial in lieu of AFSoftSerial; rearranged the distribution package.
  8. Greater precision in latitude and longitude.  Angles measured in 10-5 degrees instead of 10-4 as previously.  Some constants redefined.
  9. Minor bug fix release: the fix_age parameter of get_datetime() was not being set correctly.
  10. Added Maarten Lamers’ distance_to() as a static function.
  11. Arduino 1.0 compatibility
  12. Added satellites(), hdop(), course_to(), and cardinal()
  13. 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

Page last updated on August 31, 2013 at 7:00 pm
702 Responses → “TinyGPS”

  1. Jade

    12 years ago

    Hello, Mikal. I want to try out your library without any GPS device. Could you give me some example command to feed the code for “simple_test”? I am trying to input some proper NMEA commands but the response goes just like that

    CHARS=0 SENTENCES=0 CSUM ERR=0
    CHARS=0 SENTENCES=0 CSUM ERR=0
    CHARS=0 SENTENCES=0 CSUM ERR=0
    CHARS=76 SENTENCES=0 CSUM ERR=0
    CHARS=76 SENTENCES=0 CSUM ERR=0

    So could you please tell me what am i doing wrong?

    I just want to test your “simple_test” sketch without any real gps device. Thanks


  2. radhoo

    12 years ago

    Here is another AVR compatible library for NMEA parsing. This one is highly optimized both in terms of used memory and speed. Available as open source on google code : https://code.google.com/p/avr-nmea-gps-library/


  3. Mikal

    12 years ago

    @Jade,

    Try the example called “static_test” instead. That one doesn’t require a GPS device.


  4. Jack

    12 years ago

    Hi Mikal I really like your work, and i want you please make GSM library and connect it with Tiny GPS , Which we can send GPS coordinates as google maps link by SMS to Cellphone in real time …
    It will be nice from you to do that :)
    Thanks


  5. Nilton

    12 years ago

    Help to SKYLAB SKM53 compatible please
    no date, no speed and no course.

    thanks


  6. tinyFan

    12 years ago

    Thanks for the great work on tinyGPS! This saved me much time with parsing and course/distance. I would make a suggestion to change the floating point methods to doubles instead of floats. Most newer GPS units have accuracy (CEP50) down to 2-3m. The long in get_position has enough precision to get to 0.185m (1/10,000th of minute). But, the float with arduino only goes to 4 positions right of decimal depending on your longitude location. So, you get max accuracy to 11.1m with decimal degrees in a float (1/10,000 of a degree). AVR-based arduinos will still be limited to this because double=float. But, the new Due and ARM-based arduino-like boards implement a true double. Using double would let the floating point side achieve the same accuracy while only costing only a few more bytes of mem.


  7. Mikal

    12 years ago

    @Jack,

    I love this message. Ok, I will look into it right away. :)


  8. Mikal

    12 years ago

    @tinyFan,

    Thanks for the great analysis and suggestion. I’ll make sure that the next release of TinyGPS uses double.


  9. Faaz

    12 years ago

    Firstly thank you for such a great help.
    Iam trying to link postcode to this code. the idea behind is i have a file(csv) of the postcodes of a specific area with their long/lat. I want to include those postcodes when the gps get a fix that specific long/lat.

    Postcode can be written in StringArrays, this might eliminate the use of sd card.
    If you can help, would be highly appreciable.
    thanks you


  10. Mikal

    12 years ago

    @Faaz,

    Does your postcode database show the boundaries of each postcode area, or just a single central point? How important is it to precisely map a given point to the correct (and not just a nearby) postcode?


  11. Göran Andersson

    12 years ago

    Hi Mikal, I am trying to make an NMEA to SeaTalk protocol converter for GPS specifically, and started using your excellent library. I understand the present version decodes GGA and RMC sentences. I need to get “Fix Quality” from the GGA sentence, how do I do that?

    I also need GSV and MSS sentences, so I kindly ask if you can consider to include those in the next release..
    Thanks!


  12. Mikal

    12 years ago

    @Göran,

    I’m working on a new version of TinyGPS now that will allow you to access an arbitrary field in an arbitrary sentence. Thanks for the prod. That will help me get it done fast.


  13. Phil Nixon

    12 years ago

    I have an Arduino UNO R3 and an Adafruit GPS shield (their latest version). I used their data logger example and everything works but the data is sent to the SD card as a continuous text file. I like the Tiny GPS code and I’ve been trying to get it to work but I cannot read a sentence. I’m using software serial and this shield sets up a serial port on pins 8,7 of the R3. I’m changing the pin designation to 8,7 in your test_with_gps_device and never get data. The receiver does have a lock indication and works great with Adafruit code. Any hints would be appreciated.


  14. Markoh

    12 years ago

    I’d like to echo tinyFan’s request. I’m just getting started with DGPS and Arduino and would like to take advantage of the higher precision position to be obtained using doubles.

    Thanks and keep up the great work!


  15. Dave Erickson

    12 years ago

    I have a wise clock 4 board w/ atmega 1284p, gpsBee and dual 32×16 sure LED boards. I’m having a problem getting data from the gpsbee. I believe it’s a pin assignment or baud rate problem. The static example runs fine but when I try the test_with_GPS_device I get not data from the gps. Any thoughts or help would be greatly appreciated I’m new arduino but been programing along time.
    Thanks
    Dave


  16. Chip Thomas

    12 years ago

    For using the Date/Time Cracking, where would I put the gps.crack_datetime(&year, &month, &day,&hour, &minute, &second, &hundredths, &fix_age); for being able to see that in the monitor?

    Thanks


  17. Chip

    12 years ago

    I tried to add your Date/Time Cracking code to the inverse geocache code and keep getting a invalid use of void expression. I was trying to have the date and time print to the monitor.

    Any suggestions?
    Thanks
    Chip


  18. raghav

    12 years ago

    Hey! mikal please leave me your email. im a complete newbie and am in desperate need of help.


  19. Mr.C

    12 years ago

    Hi..Is there any problem if I want to use only the RMC sentence,the other one been stopped for transmission by the GPS unit itself? Also, another question for you…I want to integrate the Tiny in a sketch containing also the Time library and a DS1307 (DS3231 most probably…).In fact, is about a clock build around GPS,but having a DS as backup for the “blackout” receptions of GPS…This clock drives a finite state machine, so I want to be “time-lost-proof”..But I want to know how to manipulate the variables declared in both libraries for time and date, to be able to load the correct time into DS..? Needless to say that I’m total newbie in C++,assembler beeing my “native” language and this only for Microchip…Best regards…


  20. Faaz

    12 years ago

    Yes it shows the boundaries from the lag/lat.

    Postcode Latitude Longitude Easting Northing

    AL10 0AA 51.764308 -0.226194 522507 208780
    AL10 0AB 51.773123 -0.223341 522680 209765
    AL10 0AD 51.773475 -0.218732 522997 209812

    it shows like this, now can i get the postcode from the fix i get from the gps? and i dont want big file just few post codes, less than 5 even.


  21. raghav

    12 years ago

    can you please update your code, suitable to the new software serial library. great work!


  22. Ken Kingsbury

    12 years ago

    Do you have a version of TinyGPS that compiles in Arduino? TinyGPS12 says it has lots of issues when verifying on Arduino 1.5.
    Thank you, Ken


  23. Mikal

    12 years ago

    Phil, are you sure any data is getting to TinyGPS? Can you prove that you’re calling tinygps.encode()? If not, there’s probably a wiring problem. If so, then I would guess that there is too much delay in calling encode and the serial buffer overflows.


  24. Mikal

    12 years ago

    Markoh,

    Coming soon! :)


  25. Mikal

    12 years ago

    Dave, did you change the pin assignments in the code to match your installation?


  26. Mikal

    12 years ago

    @Chip, I’m sorry I don’t understand your question. Can you give an example of the code that is failing?


  27. Mikal

    12 years ago

    Hi Raghav,

    Sorry, what we try to do is answer the questions in the forum here. Do you have a particular question?


  28. Mikal

    12 years ago

    @Ken Kingsbury–

    Working on version 13–should support 1.5x and support higher precision. Stay tuned.


  29. Mikal

    12 years ago

    @Mr.C,

    While TinyGPS does process both the RMC and GGA sentences, it does not depend on having both. That is, if your device is transmitting only RMC sentences, that’s ok. If you are just using the RMC sentence, you’ll lose fields like HDOP and Altitude, but it sounds like that’s ok since you’re just using the time/date components, right?

    I would just repeatedly encode() the serial stream and call crack_datetime() to extract the date and time. Make sense?


  30. Peter

    12 years ago

    Thanks for a very nice Library, this is very easy to understand and every think works so easy.
    but can you maybe tell me how to chance the updating time from the GPS, its only update every second, but can you gets the GPS updating every 0.1 second


  31. Vinnie

    12 years ago

    How can I change the timezone of my clock with TinyGPS ? I’m using the em-406a with 4 hours ahead.

    Thanks for your work.


  32. Mikal

    12 years ago

    @Peter,

    I suspect that most modules that update once per second by default cannot increase their update rate. 10 Hz modules probably have this capacity, but in any case, such a feature is well beyond the purview of TinyGPS. TinyGPS is agnostic to device-specific details; its goal is strictly to parse NMEA sentences. You might consult your device manual.


  33. Mikal

    12 years ago

    @Vinnie,

    I’m afraid TinyGPS only understands universal time — the “music of the spheres”. :) If you want to calculate UT+4:00, you’ll have to do it manually. If you’re just interested in the time part, it’s not too hard:

    hour = (hour + 4) % 24;

    It gets a little more complicated if you also want to calculate the correct date.


  34. flegmatoid

    12 years ago

    great library. Couple observations / requests:
    – allow configuration of “distance_between”. One might want setting earth radius to also include elevation above the sea level, resulting in 10-20 cm difference. One might also like to include altitude in this calculation, resulting in 10-20 meter difference at 10km – 20km altitudes. Or maybe it would make more sense to automatically include GPS data to adjust the accuracy of calculations?


  35. ahmer

    12 years ago

    hi Mikal . this is very useful and has reduced overhead to a great extent.

    i am currently trying to acces datetime using the “gps.crack datetime” function on arduino 1.0.3 .

    it gives an error saying that there is no matching function call .
    please help .
    thankyou


  36. Mikal

    12 years ago

    @ahmer,

    There should be an underscore (_) between crack and datetime.


  37. Christian

    12 years ago

    Hello
    I use Tiny GPS for a long time and everything goes well.
    I would like to know how to decode the following information with this library:
    (1) The number of satellites.
    (2) The meaning of coordinates North or South.
    (3) The quality of the signal.
    I have test with the Adafruit_gps.h library and is easily obtained this information.
    Thank you


  38. geoff p

    12 years ago

    Hi Mikal,

    Thanks for the libraries, they are really helping!
    This may be a stupid question as i’m fairly new to arduino..
    I’m trying to avoid using floats to save memory, so I have the lat and lon as longs but there’s no decimal point. I need to insert the decimal point when I print/log the data. Is there a good way to do that?

    Thanks,

    Geoff


  39. Nizhar Acmad

    12 years ago

    hi. i am making a gps clock system using gizduino (arduino compatible). i want to get or extract the time in the nmea string and show it to a four digit seven segment display. anyone who knows the code for this? please do help. thank you.


  40. Albarra Harahap

    12 years ago

    I have tried to replace
    SoftwareSerial nss (1, 2);
    nss.begin (38400);
    customize with GPS ITEAD Shield v1.0. But, I get

    Testing TinyGPS library v. 12
    by Mikal Hart
    Sizeof(gpsobject) = 115
    Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum
    (deg) (deg) Age Age (m) — from GPS —- —- to London —- RX RX Fail
    ————————————————————————————————————————————–
    **** **** ******* ******* **** ******* ******* **** ******* ****** ***** *** 0 0.00 *** 173 0 0
    **** **** ******* ******* **** ******* ******* **** ******* ****** ***** *** 0 0.00 *** 310 0 0
    **** **** ******* ******* **** ******* ******* **** ******* ****** ***** *** 0 0.00 *** 447 0 0
    **** **** ******* ******* **** ******* ******* **** ******* ****** ***** *** 0 0.00 *** 584 0 0

    So continuous. I’ve tried to leave it for more than half an hour but no change. I have brought it to the outside without any obstruction to the sky and I make sure the amount of satelite via Android and caught there are 11. But, I did not get a GPS Shield of data.
    Please help, I have searched various sketch. GPS shield is probably broken? I’m Using Arduino UNO-R3
    Thanx :)


  41. KenLee

    11 years ago

    Mikel,
    It would be very handy to include maidenhead GRID locator built into TinyGps. I am a ham radio operator and use grids during my contact. I’m new to Arduino and have read a lot on GPS with the Arduino but parsing to grid “square” (as we call them) is not explained so I can understand it. Normally the grid square is something like EM96ft which is my grid locator. You are probably familiar with them. It would invaluable to hams that operate mobile during contests.
    Thanks,
    Ken


  42. mike

    11 years ago

    hi Mikal,

    I was wondering if TinyGPS works on the Raspberry Pi? Or is there a different version for the pi?

    thanks!


  43. beakes

    11 years ago

    Thanks for TinyGPS!
    One suggestion for the next version – Make the fix quality (2D versus 3D) directly available. I did this in my copy to make it more complete.


  44. Alexander

    11 years ago

    My gps returns valid info at $GPRMC, but gps.encode() returns false…
    Is there any solution you might think?
    Also I have one question, gps.encode() takes one by one character, so how the hell does it check if it is valid?From the letter “V” that comes in NMEA info?


  45. Jack

    11 years ago

    Hi Mikal. I’m using a GPS simulator that outputs GGA strings. The fix in the GGA sentence is 1. But gps.encode just returns 0… Do i nid RMC strings for it to work too? All i wanted was to retrieve the lat/lon frm the GGA.


  46. Mikal

    11 years ago

    @Christian,

    1. gps.satellites();
    2. Latitudes greater than 0 are north of the equator.
    3. If you are concerned about signal quality, check the “horizontal dilution of precision”: gps.hdop();

4 Trackbacks For This Post
  1. Pre-Race Telemetry Snippets | TechWeasel

    […] TinyGPS Arduino library makes it very simple to make your GPS do something useful, much thanks… Hope […]

  2. GPS modules for sale! | Rock 7 - RockBLOCK

    […] is standard 9600 baud serial NMEA sentences.  There’s an excellent GPS NMEA decoder library for Arduino here which works with this […]

  3. Data geo-tagging primer | biodesign for the real world

    […] TinyGPS is a library for Arduino that allows to use a GPS module relatively painlessly. […]

  4. Arduino GPS Tracking System -Arduino for Projects

    […] used the tinyGPS library to decode the NMEA GPS Data. Cooking-Hacks generously supplied both the GPS shield and SD Card […]

Leave a Reply