You are on page 1of 11

GPS Shield

GPS is a powerful tool that can be fun and easy to use with the help
of Arduino. Projects ranging from logging a trip in your car, all the way
to navigating autonomously are all possible with GPS.

This quickstart guide will tell you everything you need to know about
how to get time and position data with a GPS module. All you need is
a GPS module, the GPS Shield, and an Arduino.
What do you do with GPS?
We all know GPS can show your immediate location, similar to what
you would find in a hand held unit or automobile console. GPS is also
good for everything from tracking and logging, extremely accurate
time keeping, to autonomous navigation and geocaching. These
types of applications rely on more than just simple latitude and
longitude, but elevation, time, heading, ground speed, and signal
strength from the satellites. This guide will show you how to get
started writing code that will parse or pick out meaningful information
from the data that comes out of the TX pin on a GPS module.
For a detailed explanation on how GPS works, please refer to the
GPS Tracking Comparisons Tutorial.
Where to start?
The first thing you want to do is assemble the GPS shield as shown
above. See the GPS Shield Assembly Guide for detailed assembly
instructions.
• GPS Shield Assembly Guide
Next, choose a GPS module that will work with the GPS Shield (see
the product page for compatible GPS modules). The GPS Shield has
a connector for an EM-406, so the EM-406 is the easiest option to get
moving.
Here are the configuration options for the GPS Shield.

There are two slide switches, one push button switch, and two solder
jumpers on the GPS Shield as shown above.

1 - Power Switch: Controls power to the GPS module


2 - UART Selection Switch: If the UART is selected, the GPS module
will be connected to Arduino digital pins 0 and 1. If DLINE is selected,
the GPS will be connected to digital pins 2 and 3 (default) or
potentially any other digital pin. DLINE must be selected in order to
upload code. The reason for this is because the UART selection uses
the same lines that are used for programming. If UART is selected
and you load code, you will probably get errors in the Arduino IDE,
but nothing should get damaged. This is called bus contention. For
this tutorial, you can leave the switch in the DLINE position for
both loading code and running code.
3 - Solder Jumpers: These jumpers, when disabled (solder removed),
will disconnect the GPS communication lines (TX and RX) from the
Arduino. This allows for the user to connect any digital line to the
GPS communication lines. To use, simply disconnect the jumpers,
then wire the TX and RX labeled pins on the shield to any one of the
digital pins 2-13 on the shield.
4 - Reset Switch: This button is directly tied to the Arduino reset
switch, so hitting this button will restart your Arduino sketch.
Finally, plug in the GPS module into the Shield, then plug the Shield
into an unpowered Arduino as shown above. Now you are ready to
read and manipulate GPS NMEA data.

How do I make it work?


• Example GPS Parser Arduino Project
Once you have everything assembled, you are almost ready to load
the example project on the Arduino. But first, you will need to create a
directory called libraries within your arduino sketchbook, if you don't
already have one. Then, download the TinyGPS and NewSoftSerial
libraries and unzip them into the libraries folder.
This is what my libraries folder looks like. Once you have put the new
libraries in the correct locations, the example code should compile
with no errors. Also, Make sure the UART Selection Switch is
moved to DLINE and proceed to load the example code onto
your Arduino.
If everything has gone as planned, your code compiles without errors
and you can successfully load your code, you should be able to open
the serial monitor to see what the Arduino is outputting.
Here is what you should see if you just turned on your GPS after it
has been powered off for a while.
If your GPS does not have a lock, you should see this.

Here is what it looks like after the GPS has sat for about a minute:
If you see this, your GPS has a valid lock.
No, I am not moving at 5km per hour, I am just inside an office
building and not moving. It's sometimes hard for GPS to know if you
are completely still, especially if you are inside.

Using the example code


All of the parsing and complicated stuff goes on in the TinyGPS and
NewSoftSerial libraries, the only thing we need to do is invoke the
functions available in the libraries. To see how this is done, refer to
the guide below. For a complete list of available TinyGPS functions
look in the keyword.txt file in the TinyGPS library folder located in
your Arduino directory.
• Be sure to include the newly added libraries TinyGPS and
NewSoftSerial
• Define the pins you are using and the baud rate of your GPS
• Create instances of the objects from the new libraries and define
prototypes
• Create function prototypes
• Setup or initialize the hardware
• Main loop
• GetGPS function
NOTE: This sketch will only work with Arduino 0018 and above.

Be sure to include the newly added libraries


TinyGPS and NewSoftSerial
In order for this sketch to compile without errors, you will need to
download the NewSoftSerial and TinyGPS libraries and include them
in your sketch. These libraries contain all of the code that allows us to
parse the GPS NMEA string and receive it through any digital pin on
the Arduino.
#include SoftwareSerial.h
The NewSoftSerial library allows us to define any digital pin to work
as a serial UART pin.
#include TinyGPS.h
The TinyGPS library is where the GPS parsing takes place. The
library has many cool features beyond the scope of this tutorial, so be
sure to check out arduiniana.org for more examples.

Define the pins you are using and the baud


rate of your GPS
#define RXPIN 2
#define TXPIN 3
Since we are using the GPS Shield with the DLINE setting, then we
need to define which pins on the Arduino the GPS will connect to.
The GPS Shield uses these pins as the default connections.
#define GPSBAUD 4800
We also need to define the baud rate for the GPS module. The EM-
406 works at 4800bps, but if you are using another type of GPS, be
sure to check what baud rate the GPS is using to communicate.

Create instances of the objects from the new


libraries
TinyGPS gps;
SoftwareSerial nss(RXPIN, TXPIN);
These lines of code basically initialize the new libraries, TinyGPS and
NewSoftSerial, and define how we will use the libraries.

Create function prototypes


We will only use one function to help us grab the data.
void getgps(TinyGPS &gps);
The getgps function will organize and print the values we want into
the serial monitor.

Setup or initialize the hardware


Serial.begin(115200);
Set the baud rate of the outgoing data from the Arduino board that
will be received by the Arduino serial monitor. It must be at least
115200 bps, because we need to print everything before a new
sentence comes in. If you slow it down, the messages might not be
valid and you will get errors.
uart_gps.begin(GPSBAUD);
Then set the baud rate for the NewSoftSerial library. This is the baud
rate of your GPS.

Main loop
The main loop constantly runs as long as your Arduino is powered.
Here is where we want to check to see if there is any serial data
coming out of the GPS, check to see if that data is valid, and if so,
jump to the getgps function and print the data we want.
while(uart_gps.available())
{
int c = uart_gps.read();
if(gps.encode(c))
{
getgps(gps);
}
}
Here is the step through: While there is data on the RX pin,
nss.available() is TRUE, so jump into the while loop and read one
byte off of the RX pin with nss.read() and load it into variable 'c'. Then
check to see if variable 'c' is a finish to a new valid GPS NMEA
sentence with gps.encode(c). If there is a new valid sentence,
gps.encode(c) is TRUE and we jump into getgps(gps).

GetGPS function
The GetGPS function simply calls the TinyGPS functions which
automatically load the data into variables then prints them out in a
readable format.
To get latitude and longitude you need to define variables and call the
TinyGPS function responsible for grabing that data.
float latitude, longitude;
gps.f_get_position(&latitude, &longitude);
Why float and not int or byte?Think about the data you want and if
that data will need to have fractional precision. In other words, will the
values be whole numbers, like the day or month, or will the values be
decimals, like latitude or elevation (i.e. 40.06477 N)? If you want to
print decimal numbers you will need to define float variables.
Serial.print(latitude, 5);
Serial.print(longitude, 5);
This will print the latitude and longitude with 5 decimal precision,
which looks like: 40.06477.
If the data you want is a whole number, you need to define them as
either int or byte. However, if using the crack_datetime function, you
need to define the variables in exactly this way.
int year;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year, &month, &day, &hour, &minute,
&second, &hundredths);

Then print the value with Serial.print.


Serial.print(year);
Serial.print(hour, DEC);
Serial.print(minute, DEC);
Be sure to print byte defined variables as decimals (hour, minutes, or
seconds). Since year is defined already as a integer, you can just
print.
To test to see if your location is valid, simply highlight and copy the
latitude and longitude values from the serial monitor and paste them
into Google Maps. A map with your location pin pointed should be
visible.
If you want to know more about how these functions are working,
keep looking around in the library folders, all of the information is
there, it just might not be that easy to interpret. But I hope the
example code and this tutorial will serve as a start to get you up and
running with GPS.

You might also like