Learn how to use GPS and Arduino in chapter seventeen 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.
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)
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 - TinyGPS. So download and install that before moving forward.
“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.
Latest posts by John Boxall (see all)
- Arduino Tutorials – Chapter 22 – the AREF pin - December 12, 2013
- Tutorial – LM3915 Logarithmic Dot/Bar Display Driver IC - December 9, 2013
- Arduino Tutorials – Chapter 16 – Ethernet - December 6, 2013