Lessons and the learning game

GPS and navigation

A NEO-6M on the Uno: NMEA by hand and with TinyGPS++, fix quality, distance and bearing, geofences, track logs, and a reverse geocache box.

The bench

An Uno with a NEO-6M on a software serial port: the module's TX goes to pin 4 and its RX to pin 3, at 9600 baud. The module starts at the National Mall in Washington, DC (38.88947, -77.03523), with 8 satellites. Later lessons add a microSD module on the SPI pins with CS on 10, a buzzer on pin 8, a 16x2 I2C LCD on A4 and A5, a button on pin 2 and a lamp on pin 7 for the geocache box's latch.

Click the module to lose its fix, and click again to get it back. Its properties set the position, altitude, satellites, speed in km/h and course; with a speed above zero it moves along that course, so a lesson can drive it away from home.

What each sketch does

  • gpsnmea: reads sentences a line at a time with no library, checks each checksum, splits the RMC and turns ddmm.mmmm into degrees.
  • gpstiny: TinyGPS++ for position, altitude, satellites, time, speed and course, with a time zone offset for local time.
  • gpsquality: judges the fix once a second by satellites, HDOP and the age of the last position.
  • gpsdist: the haversine distance and initial bearing to the Lincoln Memorial, checked against TinyGPS++ (1294 m on a course of 269 degrees).
  • gpsfence: a 100 m geofence around the start, with a 10 m hysteresis band and a buzzer.
  • gpslog: a CSV track log on the card, one row every two seconds, opened and closed per row.
  • gpsboss: the boss, a box that shows how far it is from the Smithsonian Castle, logs every try and lights its latch lamp within 20 m.

The formulas

With latitudes and longitudes in radians and R the Earth's radius:

  • Haversine distance: a = sin²(Δφ/2) + cos φ1 cos φ2 sin²(Δλ/2), then d = 2R asin(√a).
  • Initial bearing: θ = atan2(sin Δλ cos φ2, cos φ1 sin φ2 − sin φ1 cos φ2 cos Δλ), from true north, turned into 0 to 360 degrees.

TinyGPS++ uses R = 6372795 m. Both treat the Earth as a sphere, which is good to about half a percent.

Honest limits of the simulator

  • There is no sky: no satellites to acquire, no cold start and no multipath. The module has a fix from its first second unless you take it away.
  • HDOP is modeled as 4 divided by the square root of the satellite count, a stand-in for real satellite geometry.
  • The position does not wander. A real receiver drifts by a few meters while standing still, which is why the geofence has a band.
  • The geoid separation in the GGA sentence is reported as 0.0.
  • On the Uno a double is a 32-bit float, so TinyGPS++ prints 38.889469 for 38.889470. That is the real chip's behavior, not a simulator limit.
  • SD card writes take a good part of a second of simulated time.

Data logging and storage for more on SD cards, and Clocks and time for keeping time without a sky.

Take the course at GPS and navigation.