Arduino and GPS Receivers
Arduino and GPS Receivers
GPS Receivers are great for adding tracking and autopilot functionality to a UAV, robot, radio controlled plane, car or boat.
GPS Receivers output data as standard ascii text at either 4800 baud or 9600 baud and can be easily read by the serial port on any microcontroller
The GPS receiver outputs data in National Marine Electronics Association (NMEA) format The idea of NMEA is to send a line of data called a sentence that is totally self contained and independent from other sentences. The standard sentences have a two letter prefix that defines the device that uses that sentence type. (For gps receivers the prefix is GP.) which is followed by a three letter sequence that defines the sentence contents. In addition NMEA permits hardware manufactures to define their own proprietary sentences for whatever purpose they see fit. All proprietary sentences begin with the letter P and are followed with 3 letters that identifies the manufacturer controlling that sentence. For example a Garmin sentence would start with PGRM and Magellan would begin with PMGN.
Each sentence begins with a '$' and ends with a carriage return/line feed sequence and can be no longer than 80 characters of visible text (plus the line terminators). The data is contained within this single line with data items separated by commas. The data itself is just ascii text and may extend over multiple sentences in certain specialized instances but is normally fully contained in one variable length sentence. The data may vary in the amount of precision contained in the message. For example time might be indicated to decimal parts of a second or location may be show with 3 or even 4 digits after the decimal point. Programs that read the data should only use the commas to determine the field boundaries and not depend on column positions. There is a provision for a checksum at the end of each sentence which may or may not be checked by the unit that reads the data. The checksum field consists of a '*' and two hex digits representing an 8 bit exclusive OR of all characters between, but not including, the '$' and '*'. A checksum is required on some sentences.
$GPRMC NMEA Sentence
The $GPRMC Sentence is the most useful one and contains the position fix. An example is
225446 Time of fix 22:54:46 UTC A Navigation receiver warning A = Valid position, V = Warning 4916.45,N Latitude 49 deg. 16.45 min. North 12311.12,W Longitude 123 deg. 11.12 min. West 000.5 Speed over ground, Knots 054.7 Course Made Good, degrees true 191194 UTC Date of fix, 19 November 1994 020.3,E Magnetic variation, 20.3 deg. East *68 mandatory checksum
Decoding the Sentence on an Arduino
We need to read each of the sentences that the GPS receiver sends out and then break them down into their constituent parts based on the comma seperation of the fields. Fortunately for the Arduino there is a useful library for decoding the GPS NMEA strings called TinyGPS, so i won't be showing you any code here. Please refer to the TinyGPS homepage for code and examples (see TinyGPS homepage)
The sourcecode is also available here