GPS Basics a learn.sparkfun.com tutorial

Available online at: http://sfe.io/t9

Contents

The Basics of GPS

You have probably used or benefitted from a GPS receiver. They are found in most smartphones, many new automobiles, and they are used to track commerce all over the globe. These tiny devices can instantaneously give your exact position and time, almost anywhere on the planet, for free! All you need is a GPS receiver, and receivers are getting less expensive and smaller every day.

GPS Module

A common GPS receiver or GPS module.

Don't take these tiny, inexpensive modules for granted. There are decades of engineering that went in to giving you accurate position anytime, anywhere. Dozens of GPS satellites, all containing extremely accurate atomic clocks, have been launched since the late 70's, and launches continue to this day. The satellites continuously send data down to earth over dedicated RF frequencies. Our pocket-sized GPS receivers have tiny processors and antennas that directly receive the data sent by the satellites and compute your position and time on the fly. Simply amazing.

Suggested Reading

There are some concepts that this tutorial builds upon and that you may need to know or prepare before starting:

Serial Communication

Asynchronous serial communication concepts: packets, signal levels, baud rates, UARTs and more!

Connector Basics

Connectors are a major source of confusion for people just beginning electronics. The number of different options, terms, and names of connectors can make selecting one, or finding the one you need, daunting. This article will help you get a jump on the world of connectors.

Binary

Binary is the numeral system of electronics and programming...so it must be important to learn. But, what is binary? How does it translate to other numeral systems like decimal?

Logic Levels

Learn the difference between 3.3V and 5V devices and logic levels.

Serial Terminal Basics

This tutorial will show you how to communicate with your serial devices using a variety of terminal emulator applications.

Suggesting Viewing

How Does GPS Work?

GPS receivers use a constellation of satellites and ground stations to compute position and time almost anywhere on earth.

animated image of earth and the view of satellites visable from a given location

Notice the moving point on the globe and the number of visible satellites.

At any given time, there are at least 24 active satellites orbiting over 12,000 miles above earth. The positions of the satellites are constructed in a way that the sky above your location will always contain at most 12 satellites. The primary purpose of the 12 visible satellites is to transmit information back to earth over radio frequency (ranging from 1.1 to 1.5 GHz). With this information and some math, a ground based receiver or GPS module can calculate its position and time.

How Does a GPS Receiver Calculate Its Position and Time?

The data sent down to earth from each satellite contains a few different pieces of information that allows your GPS receiver to accurately calculate its position and time. An important piece of equipment on each GPS satellite is an extremely accurate atomic clock. The time on the atomic clock is sent down to earth along with the satellite’s orbital position and arrival times at different points in the sky. In other words, the GPS module receives a timestamp from each of the visible satellites, along with data on where in the sky each one is located (among other pieces of data). From this information, the GPS receiver now knows the distance to each satellite in view. If the GPS receiver’s antenna can see at least 4 satellites, it can accurately calculate its position and time. This is also called a lock or a fix.

Did you catch all of that? If not or if you want more, check out a much more detailed explanation, in volume 1 of GPS Fundamentals by Dan Doberstein. Volume 1 has been released for free, but you must support the author to read volume 2.

satellites transmitting and receiveing GPS data

An artist's rendition of the control segment.

There is another piece of the global positioning system we haven’t talked about. Along with satellites and GPS receivers, there are ground based stations that can communicate with the satellite network and some GPS receivers. This system is formally called the control segment and increases the accuracy of your GPS receiver. Common systems that use the control segment to improve accuracy are WAAS and DGPS. WAAS is common on most GPS receivers and improves accuracy to about 5 meters. DGPS requires a specific type of GPS receiver and gets centimeter accuracy. DGPS units are also expensive and tend to be larger because they require an additional antenna.

GPS Accuracy

GPS Accuracy depends on a number of variables, most notably signal to noise ratio (noisy reception), satellite position, weather and obstructions such as buildings and mountains. These factors can create errors in your perceived location. Signal noise usually creates an error from around one to ten meters. Mountains, buildings and other things that might obstruct the path between the receiver and the satellite can cause three times as much error as signal noise. A GPS receiver must be able to get a lock on 4 satellites to be able to solve for a position. The first lock it gets allows the receiver to obtain the almanac information and thus what other satellites it should listen for. Although it is possible to get a position from less than 4 satellites, the margin of error of this position can be rather large. Your most accurate read of your location comes when you have a clear view of a clear sky away from any obstructions and under more than four satellites. To combat these errors, a couple of different assistants have been created.

Assisted GPS

One of these ancillaries is Assisted GPS or AGPS. This method uses wireless (ground-based) networks to help relay between the satellite and the receiver when the GPS signal is weak or not able to be picked up. There are two ways AGPS can help out. The first is to provide the receiver with the proper almanac data and the precise time. The second utilizes the higher computing power and good satellite signal of the ground base to interpret the broken or fragmented information the receiver is receiving to provide a more accurate position reading to the receiver. AGPS is mostly accomplished by GPS receivers mounted on cellular towers. When communicating with these receivers, the GPS can acquire a lock on the satellite more quickly as well as receive more accurate information. This method is what is used for GPS in mobile phones and why they’re sometimes more accurate than the GPS receivers on their own. But AGPS is present in more devices than just cellphones; it's even available in cameras and some vehicles. It’s most beneficial in cities where the GPS signal may have a difficult time making it through the dense maze of the buildings.

Differential GPS

Another method is Differential GPS or DGPS. DGPS also uses ground or fixed GPS stations to determine the location, but differs in that it finds the difference between both the satellite and the ground location reading. These ground stations may be up to 200 nautical miles from the receiver, and it is important to note that accuracy deteriorates the further you are from the ground station. DGPS is accomplished by a ground station broadcasting a signal which dictates the error between the actual pseudorange and the measured pseudorange. This value is calculated by multiplying the speed of light by the time it takes the signal to travel from the satellite to the receiver. As an example, one form of DGPS is Wide Area Augmentation System or WAAS.

Differential GPS

(Image Courtesy of ASMA)

Originally developed by the FAA to assist aircraft GPS, WAAS uses a system of specifically built ground stations. WAAS holds a specific set of accuracy standards that ground station measurements must meet. Laterally and vertically, WAAS must be accurate to within 7.6 meters 95% of the time. These ground stations send their measurements to master stations which send the corrections to WAAS satellites every 5 seconds or quicker. From the Satellite, a signal is broadcast back to the receivers on earth where the corrections are used to improve the GPS accuracy. In some locations, WAAS is able to provide an accuracy of 1 meter lateral and 1.5 meters vertically. While WAAS is only present in North America, similar systems are in place in many other parts of the world.

Message Formats

GPS data is displayed in different message formats over a serial interface. There are standard and non-standard (proprietary) message formats. Nearly all GPS receivers output NMEA data. The NMEA standard is formatted in lines of data called sentences. Each sentence contains various bits of data organized in comma delimited format (i.e. data separated by commas). Here’s example NMEA sentences from a GPS receiver with satellite lock (4+ satellites, accurate position):

language:bash
$GPRMC,235316.000,A,4003.9040,N,10512.5792,W,0.09,144.75,141112,,*19
$GPGGA,235317.000,4003.9039,N,10512.5793,W,1,08,1.6,1577.9,M,-20.7,M,,0000*5F
$GPGSA,A,3,22,18,21,06,03,09,24,15,,,,,2.5,1.6,1.9*3E

For example, the GPGGA sentence contains the follow:

The data is separated by commas to make it easier to read and parse by computers and microcontrollers. This data is sent out on the serial port at an interval called the update rate. Most receivers update this information once per second (1Hz), but more advanced receivers are capable multiple updates per second. 5 to 20Hz is possible with modern receivers.

Reading GPS Data

Most GPS modules have a serial port, which makes them perfect to connect to a microcontroller or computer.

Connecting to a Microcontroller

GPS modules to GPS Breakout connections

EM-506 GPS and GP-735 GPS with GPS Breakout

Once a GPS module is powered, NMEA data (or another message format) is sent out of a serial transmit pin (TX) at a specific baud rate and update rate, even if there is no lock. To have your microcontroller read the NMEA data, all that is needed is to connect the TX pin of the GPS to the RX (receive) pin on the microcontroller. To configure the GPS module, you will need to also connect the RX pin of the GPS to the TX pin of the microcontroller.

It is common for the microcontroller to parse the NMEA data. Parsing is simply removing the chunks of data from the NMEA sentence so the microcontroller can do something useful with the data.

For example, the microcontroller might need to read only the altitude of your GPS.

language:bash
$GPGGA,235317.000,4003.9039,N,10512.5793,W,1,08,1.6,1577.9,M,-20.7,M,,0000*5F

Instead of dealing with all of this text, the microcontroller can parse the GPGGA sentence and end up with only the altitude (in meters).

language:bash
1577

Once the microcontroller can grab the data needed, the information can be manipulated to create other interactions on the microcontroller.

The Arduino platform can parse NMEA data easily with the help of the Tiny GPS library. Check out the GPS Shield Getting Started Guide for a walk-through example on how to connect an Arduino to a GPS module and parse NMEA sentences.

Connecting to a Computer

A simple way to see the NMEA data directly is to connect the GPS module to a computer. For the connections, all that is needed is to power the GPS with the FTDI basic (in this case 5V and GND), then connect the TX pin of the GPS to the RX pin on the FTDI Basic.

GPS module to FTDI connections

EM-506 GPS - GPS Breakout - 5V FTDI Breakout

GP-735 GPS - GPS Breakout - 5V FTDI Breakout

Next, open a serial terminal program at the same baud rate of your GPS module. Even if the GPS does not have a lock, you should see NMEA sentences steaming by.

language:bash
$GPRMC,235316.000,A,4003.9040,N,10512.5792,W,0.09,144.75,141112,,*19
$GPGGA,235317.000,4003.9039,N,10512.5793,W,1,08,1.6,1577.9,M,-20.7,M,,0000*5F
$GPGSA,A,3,22,18,21,06,03,09,24,15,,,,,2.5,1.6,1.9*3E

Configuring a GPS Receiver

To configure a GPS receiver, knowing the type of chipset your GPS is using is very important. The GPS chipset contains a powerful processor that is responsible for the user interface, all of the calculations, as well as analog circuitry for the antenna. The chipset also allows for data to be sent to the GPS receiver to configure parameters like, update rate, baud rate, sentence selection, etc.

In order to send commands over a serial interface to a GPS receiver, you will need a command set or reference manual. Before diving too far into the command set for a given module, be sure to check with the vendor. Many chipset vendors provide software that allows you to easily communicate and configure the GPS module over a serial port.

The following are datasheets and command sets for some of the more common chipsets.

Some chipsets allow for alternate protocols such as SiRF binary (SiRF chipset), UBX (ublox chipset), or a proprietary messages. These protocols contain the same information, but communicate using binary (instead of ASCII) for faster communication.

When communicating with a GPS receiver, most commands need to be terminated by a checksum. In most cases, you need to XOR each of your sentences. Here is a simple XOR online calculator.

GPS Glossary

Troubleshooting

Lock Problems

Mikal Hart's TinyGPS++ library is excellent to get you up and running quickly with GPS. However, that might not be the case if you are between urban canyons, inside a concrete building, or a black pit of doom for all wireless signals to/from the outside world. The problem that we found is when GPS is used indoors, and, in the case of the SparkFun building, it makes seriously difficult to get a GPS lock. We have lots of concrete, metal girders, and a large solar array that wreaks havoc with GPS signals (and pretty much all cellular carriers for that matter).

Google map of our roof.

If you ever have issues seeing the GPS data when using a library or the output is incomplete, you may need to move to a different location to view more satellites. Sometimes moving the GPS module a few steps to a different spot or to the perimeter of a building can help. To check the amount of satellites in view, you can observe 6th and 7th field of the GPGAA sentence to see if you are having any lock problems. Below is an example GPGGA sentence when a GPS module does not have a satellite lock. As you can see, the output indicates that the data is invalid since there is no GPS fix and there are no satellites in view.

language:bash
$GPGGA,105317.709,8960.0000,N,00000.0000,E,0,0,,137.0,M,13.0,M,,*4C

Baud Rate Mismatch

If you are passing data from the GPS module's serial UART to your serial monitor and all you see is "garbage" like the image below, check to make sure the baud rate set is correctly. Depending on the GPS module, the baud rate can be vary. Make sure to check the datasheet for your GPS module to ensure that the baud rate is the same.

Baud Rate Mismatch

Baud rate mismatch (aka garbage) as described in this Common Pitfalls section.

Resources and Going Further

You should now have a clear understanding of how GPS units work and how to implement them into your next project. For more great project ideas check out these other SparkFun tutorials.

Gyroscope

Gyroscopes measure the speed of rotation around an axis and are an essential part in determines ones orientation in space.

How to Power a Project

A tutorial to help figure out the power requirements of your project.

How to Use a Breadboard

Welcome to the wonderful world of breadboards. Here we will learn what a breadboard is and how to use one to build your very first circuit.

Accelerometer Basics

A quick introduction to accelerometers, how they work, and why they're used.

Electric Power

An overview of electric power, the rate of energy transfer. We'll talk definition of power, watts, equations, and power ratings. 1.21 gigawatts of tutorial fun!

Need some inspiration for your next project? Check out some of these related tutorials:

Getting Started with U-Center for u-blox

Learn the tips and tricks to use the u-blox software tool to configure your GPS receiver.

LTE Cat M1/NB-IoT Shield Hookup Guide

The SparkFun LTE CAT M1/NB-IoT Shield equips your Arduino or Arduino-compatible microcontroller with access to data networks across the globe. This shield adds wireless, high-bandwidth cellular functionality to your IoT project!

Three Quick Tips About Using U.FL

Quick tips regarding how to connect, protect, and disconnect U.FL connectors.

MicroMod GNSS Carrier Board (ZED-F9P) Hookup Guide

Easily switch between Processor Boards using the MicroMod ecosystem and get precision down to the diameter of a dime with the ZED-F9P from u-blox using the MicroMod GNSS Carrier Board!

Or check out these blog posts.

Python and GPS Tracking Tutorial

December 27, 2012

Chernobyl and the Leiger

March 28, 2013

SparkFun Live: GPS Speedometer

June 24, 2015

Adventures in Science: How GPS Works

July 31, 2017

Where am I, Exactly? A Guide to SparkFun's GPS Modules

August 6, 2019

 

GPS Constellation

 


learn.sparkfun.com | CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado