TinyGPS: the original small NMEA parser
TinyGPS by Mikal Hart is the library that TinyGPS++ replaced, and plenty of older tutorials and projects still use it. A GPS module such as the u-blox NEO-6M sends lines of text called NMEA sentences at 9600 baud; TinyGPS takes them one character at a time through encode() and pulls out position, altitude, speed, course and the date and time.
Built in on Mokxi: #include <TinyGPS.h> and it builds, with nothing to install. Free, and no account needed.
What TinyGPS does
Its calls fill variables you pass by address. f_get_position(&lat, &lon, &age) writes the latitude and longitude as floats and how many milliseconds old the fix is. crack_datetime() fills year, month, day, hour, minute, second and hundredths. Values the module has not reported yet come back as TinyGPS::GPS_INVALID_F_ANGLE and its siblings.
For a new project TinyGPS++ is the better choice: its objects say whether each value is valid and freshly updated. Keep TinyGPS when you are running or repairing a sketch written for it. In Mokxi, encode() is true at the end of every sentence with a good checksum, where upstream鈥檚 is true for RMC and GGA only.
| Library Manager name | TinyGPS |
|---|---|
| Author | Mikal Hart |
| Include | #include <TinyGPS.h> |
| Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | GPS module, NEO-6M |
Install it
It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32.
In Mokxi it is built in: #include <TinyGPS.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "TinyGPS" by Mikal Hart from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add TinyGPS to lib_deps in PlatformIO. Mokxi follows the calls of release 13.0.0, so a sketch written here builds there too.
What to know in the simulator: encode() is true at the end of every sentence with a good checksum, where upstream鈥檚 is true for RMC and GGA only.
Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 Arduino Pro Mini 3.3 V 路 ESP8266 NodeMCU 路 ESP32 DevKit 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 ESP32-S3-DevKitC-1 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32
The calls you will use
In the order a sketch meets them.
TinyGPS gps
Creates the parser.
gps.encode(c)
Feeds one character from the GPS serial port. True when a complete sentence has just been decoded.
while (Serial1.available()) {
if (gps.encode(Serial1.read())) newData = true;
}gps.f_get_position(&flat, &flon, &age)
Latitude and longitude in degrees as floats, and the fix鈥檚 age in milliseconds.
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age)
The date and the UTC time from the last sentence.
gps.f_speed_kmph()
Ground speed in kilometers per hour, with f_speed_mph() and f_speed_knots() beside it.
gps.f_altitude()
Altitude above mean sea level in meters.
gps.satellites()
How many satellites are in the fix, or TinyGPS::GPS_INVALID_SATELLITES.
gps.stats(&chars, &sentences, &failed)
How many characters and good sentences have been seen, and how many failed their checksum, the first thing to print when nothing works.
Example: Position from a NEO-6M with the original TinyGPS
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(4, 3); // the GPS's TX, RX
void setup() {
Serial.begin(9600);
ss.begin(9600);
Serial.print("TinyGPS library v. ");
Serial.println(TinyGPS::library_version());
}
void loop() {
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;) {
while (ss.available()) {
if (gps.encode(ss.read())) newData = true;
}
}
if (newData) {
long lat, lon;
unsigned long age;
gps.get_position(&lat, &lon, &age);
Serial.print("LAT=");
Serial.print(lat);
Serial.print(" LON=");
Serial.print(lon);
Serial.print(" SAT=");
Serial.println(gps.satellites());
}
}Wiring
- GPS module, NEO-6M TX to Arduino Uno R3 4
- GPS module, NEO-6M RX to Arduino Uno R3 3
- Arduino Uno R3 5V to GPS module, NEO-6M VCC
- Arduino Uno R3 GND to GPS module, NEO-6M GND
Common errors and how to fix them
Latitude prints 1000.000000
That is TinyGPS::GPS_INVALID_F_ANGLE: there is no fix yet. Take the module outdoors or near a window and wait a minute or more for the first fix; compare against the constant before printing.
stats() shows 0 characters
Nothing is arriving from the module. Swap TX and RX (the module鈥檚 TX goes to the board鈥檚 RX), check the baud rate is 9600, and on an Uno use SoftwareSerial so the USB port stays free.
The hour is wrong by several hours
GPS time is UTC. Add your time zone鈥檚 offset in the sketch, or use the Timezone library to handle daylight saving.
Keep going
Parts
More communication libraries
Try TinyGPS without installing anything
Open the example, change the sketch, and keep your version in a free account.