Moving Forward with Arduino – Chapter 17 – GPS

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

In this instalment we will introduce and examine the use of the Global Positioning System receivers with Arduino systems. This will be part one.

What is the GPS? In very simple terms, a fleet of satellites orbit the earth, transmitting signals from space. Your GPS receiver uses signals from these satellites to triangulate position, altitude, compass headings, etc.; and also receives a time and date signal from these satellites. The most popular GPS belongs to the USA, and was originally for military use, however it is now available for users in the free world. Interestingly, the US can switch off or reduce accuracy of their GPS in various regions if necessary. For more information, have a look at Wikipedia or the USAF Space Command GPS Ops Centre site. As expected,  other countries have their own GPS as well – such as Russia, China, and thankfully the EU is working on one as well.

So – how can us mere mortals take advantage of a multi-billion dollar space navigation system just with our simple Arduino? Easy – with an inexpensive GPS receiver and shield. When searching for some hardware to use, I took the easy way out and ordered the Sparkfun GPS pack which includes the required Arduino shield and header sockets, short connecting cable and an EM-406A 20-channel GPS receiver with in-built antenna:

For reference now and in the future, here is the data book for the GPS receiver: EM-406 manual.pdf.

If you have a different receiver and need some help or questions answered, please ask in the Google Group and we’ll do our best to help you.

All you will need is an Arduino Duemilanove or 100% compatible board, and the usual odds and ends. With regards to the Arduino software, you will need v18 or above. When it comes time to solder up your shield, if possible try and sit it into another shield or board – this keeps the pins in line and saves a lot of trouble later on:

And we’re done:

Please notice in the photo above the cable is a lot longer between the shield and the GPS receiver. This was an extra cable, which makes things a lot more convenient, and it never hurts to have a spare. Finally, on the shield please take note of the following  two switches – the shield/GPS power switch:

and the UART/DLINE switch:

For now, leave this set to UART while a sketch is running. When uploading a sketch to the board, this needs to be on DLINE. Always turn off your GPS shield board before changing  this switch to avoid damage.

Example 17.1 – Is anyone out there?

Now, let’s get some of that juicy GPS data from outer space.

You will need:

Once you have your hardware assembled, upload the following sketch: example 17.1. Now for desk jockeys such as myself, there is a catch, as a GPS receives signals from satellites the receiver will need to be in line of sight with the open sky. If you have your desk next to a window, or a portable computer you’re in luck.  Look at the LED on your GPS receiver – if it is blinking, it has a lock (this is what you want); on – it is searching for satellites; off – it is off (!). Frankly I though it would blink for searching, and be on for satellite lock. Moving along…

The first time you power up your receiver, it may take up to forty-two seconds to lock onto the available satellites, the period of time is the cold start time. Once this has been done, the next time you power it up, the searching time is reduced somewhat as the receiver stores some energy in a supercap (very high-value capacitor) to remember the satellite data, which it will use the next time to reduce the search time (as it already has a “fair idea” where the satellites are).

Now open the serial monitor box, sit back and wait a moment or two, and you should be presented with something very similar to this:

What a mess. What on earth does all that mean? For one thing the hardware is working correctly. Excellent! Now how do we decode these space-signals…

They are called NMEA codes. Let’s break down one and see what it means. For example, the line:


Each field represents:

  • $GPRMC tells us the following data is essential point-velocity-time data;
  • 165307.000 is the universal time constant (Greenwich Mean Time) – 16:53:07 (hours, minutes, seconds). So you now have a clock as well.
  • A is status – A for active and data is valid, V for void and data is not valid.
  • 2728.9620 is degrees latitude position data = 27 degrees, 28.962′
  • S for south (south is negative, north is positive)
  • 15259.5159 is degrees longitude position data = 152 degrees, 59.5159′
  • E for east (east is positive, west is negative)
  • 0.20 is my speed in knots over ground. This shows the inaccuracy  that can be caused by not having a clear view of the sky
  • 48.84 – course over ground (0 is north, 180 is south, 270 is west, 90 is east)
  • 140910 is the date – 14th September, 2010
  • the next is magnetic variation for which we don’t have a value
  • checksum number

Thankfully the data is separated by commas. This will be useful if you are logging the data to a text file using a microSD shield, you will then be able to use the data in a spreadsheet very easily. Later on we will work with data from other codes, but if you can’t wait, here is the NMEA Reference Manual that explains them all.

In the meanwhile, how can we convert the location data (longitude and latitude) received into a position on a map?

  • Visit this website
  • In the box that says “paste your data here”, enter (for example, using my data above)
name,desc,latitude,longitude home,home,-2728.9660,15259.5143

For example:

Then click “Draw the Map”, and you will be presented with a Google map in a new window that you can zoom around in, change views and so on. Interestingly enough the coordinates returned in the test above were accurate down to around three meters. Later on that website will be of great use, as you can import text files of coordinates, and it will plot them out for you. If you use this mapping site a lot, please consider making a donation to help them out. If you’re going to stalk me, it’s easier to wait at the PO Box.

Now as always, there is an easier way. The purpose of the previous demonstrations were to see the raw data that comes from a receiver, and understand how to work with it.

Moving on… now we can receive GPS signals – and in the past we have used LCD modules – so we can make our own variations of portable (!) GPS modules and other devices. At this point you will need to install two libraries into the Arduino software – NewSoftSerial and TinyGPS. Extract the folders into the librariesfolder within your arduino-001x folder.

Example 17.2 – My First GPS

Using various pieces of hardware from the past, we will build a simple, portable unit to display our data.

You will need:

  • Arduino Duemilanove or compatible board;
  • a suitable GPS setup – for example the GPS shield bundle;
  • An LCD with HD44780 interface that has the ability to connect to your Arduino system. The size is up to you, we’re using a 20 x 4 character unit. If you have dropped in or are a bit rusty on LCDs, please read chapter two; and the LCD shield projects one and two;
  • An external power supply for your setup (if you want to walk up and down the street at midnight like I did) – for example, a 9V battery snap soldered to a DC plug is a quick and dirty solution!

Luckily I have made an LCD shield in the past which works nicely, and doesn’t use digital pins D0 and D1 – these are used by the GPS shield to get the data back to the Arduino. Therefore the whole lot just plugged in together as shields do. Here is the sketch for your consideration: example 17.2.pdf. Before uploading the sketch, turn off the GPS shield, set the DLINE/UART switch on the GPS shield to DLINE, upload the sketch, then set it back again, then back on with the GPS shield.

So here it is all thrown together in my LBE shipping container/lunchbox:

And a close-up view of the LCD. There was not room for the course data, but you can modify the sketch accordingly. The data will be a little off due to the photo being taken indoors:

Now for some outdoor fun. In the video clip below, we take a ride on the bus and see our GPS in action…

For the record, Brisbane has a huge fleet of nice new buses, however in off-peak they dust off the reserve fleet. I had to take an old bus that wasn’t full of security cameras, so the ride is bumpy:

As we have a lot of electronics in this setup, it would be interesting to know the current draw, to help plan for an appropriate power supply. The trusty meter gives us:

Wow – a maximum of 122 milliamps even with that LCD backlight blazing away. So when we make some GPS logging devices without such a monstrous LCD, we should be able to get the current draw down a lot more.

The purpose of this example was to show how you can manipulate the data from the GPS receiver. So from now on it is just working with numbers, and displaying numbers. Hopefully by now your imagination is starting to wind up with various ideas! And we made something, instead of zoning out in front of the television.

Thus concludes our introduction to Arduino and GPS. There will be another GPS chapter, most likely chapter nineteen, so please subscribe to receive updates (see top-right of page). This article was a lot of fun to write, and I hope you enjoyed reading it. If you have any comments at all please leave a comment in our dedicated Google Group – please sign up, it’s free and we can all learn something. If you would like to showcase your work from this article, email a picture or a link to john at tronixstuff dot com. You might even win a prize!

So have fun, stay safe and see you soon for our next instalment – delving deeper into Arduino and Ethernet

High resolution photos from this article are available in flickr.

Please subscribe (see the top-right of this page) to receive notifications of new articles.

Don’t forget to check out the range of gear at Little Bird Electronics

By John Boxall

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s