t r o n i x s t u f f

fun and learning with electronics

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.

Updated 23/01/2013

In this instalment we will introduce and examine the use of the Global Positioning System receivers with Arduino systems. 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, however many people tell me this is not an issue unless you’re in a combat zone against the US forces. 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 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 this retail 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. All you will need is an Arduino Uno or 100% compatible board, and the usual odds and ends. 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. 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 (!). The first time you power up your receiver, it may take a  minute or so to lock onto the available satellites, this period of time is the cold start time.

This will be in ideal conditions – i.e. with a clear line of sight from the unit to the sky (clouds excepted!). Once this has been done, the next time you power it up, the searching time is reduced somewhat as our 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: $GPRMC,165307.000,A,2728.9620,S,15259.5159,E,0.20,48.84,140910,,*27 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. 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 another Arduino library - TinyGPSSo download and install that before moving forward.

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 Uno 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 twenty-four;
  • 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. 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 lunch box:

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…

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. We continue with GPS part II here.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

September 17, 2010 - Posted by | arduino, beginnner, education, GPS, GPS-09123, learning electronics, microcontrollers, RTL-10709 | , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

13 Comments »

  1. Does anyone know what would need to be changed to make this work correctly with the latest Arduino software (at the time of posting, 1.0.3)?

    Comment by mlevin3 | January 20, 2013 | Reply

    • You’re fine now, the GPS tutorials have been updated.

      Comment by John Boxall | January 24, 2013 | Reply

  2. John,

    I’m using a EM406A, but using a Sparkfun CAN-bus shield (https://www.sparkfun.com/products/10039). When I try to run your example sketch, nothing is returned… how do I fix this?

    Comment by Aaron McRuer | March 7, 2013 | Reply

  3. I’m trying to do this using an EB-85A receiver (https://www.sparkfun.com/products/8266), but I’m stuck. I put my setup out on my windowsill and later took it into the middle of a park; I tried your sketch along with two others I found, and it just never locks on no matter how long I wait. There were no tall buildings around, and the sky was mostly clear. The code compiles and uploads fine and the switches are all thrown in the right direction, but the red light just stays solid. Do I need to do something different since I’m using a different receiver? Any other ideas? The EM-406A seems to be the overwhelming choice for this kind of project, so I’m almost ready to order one and give up on this model. Thanks!

    Comment by Ryan | March 14, 2013 | Reply

    • No idea – haven’t used that receiver. Try cranking the baud rate down to 4800 bps and the refresh rate to 1Hz. Not much help as Sparkfun have retired the EB-85A. My tutorial was written for the EM-406A which is somewhat different to your unit.

      Comment by John Boxall | March 14, 2013 | Reply

  4. How or where can we get the math that converts the GPS data to points on a map or (more importantly) pixels on an LCD screen?

    Comment by J. Mellenin | April 4, 2013 | Reply

    • The code demonstrated in the tutorials gives out latitude and longitude – so there’s points on a map. You can use some maths to convert to pixel positions for your own screen.

      Comment by John Boxall | April 4, 2013 | Reply

  5. If I wanted to get altitude , is that possible ?

    Comment by Steve56 | April 27, 2013 | Reply

  6. Hi John,

    I tried using the GPS with the Colour LCD shield here
    http://tronixstuff.wordpress.com/2011/02/07/tutorial-arduino-and-colour-lcd/

    and it wouldn’t work. The GPS was getting data but was not printing to the LCD. Are there any conflicts between the GPS shield and the colour lcd shield ?

    Comment by steve56 | May 4, 2013 | Reply

    • The LCD shield uses D3 for one of the buttons, and the GPS shield uses D3 for data TX. Bend out the D3 pin on the LCD shield to isolate it and try again.

      Comment by John Boxall | May 6, 2013 | Reply

      • Thanks John, that did the trick.

        Comment by steve56 | May 6, 2013


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 3,836 other followers

%d bloggers like this: