Grade: BeginnerDon't you hate it. You find a great tutorial online that's about the exact subject you're interested in, but the prawnhead (general derogatory term) who wrote the article assumed something in step 1 that makes or breaks the whole thing. Without this assumed knowledge you're stuffed. I'm going to work up from the basics to a fully functional Arduino sketch that gets access to all the data from an EM-406A GPS. I aim to not leave anybody behind here but if I do, please send feedback and I'll make edits.
Let's make sure you have a good understanding of what GPS is and does. If you're planning a project that involves GPS you must understand some of the theory or else you may spend a lot of time on your project only to find it doesn't work as well as you wanted it to.
Introduction to GPSThe Global Positioning System (GPS) is an array of satellites that surround the earth. These satellites are in geosynchronous orbit which means they stay in the same position in the sky as the earth rotates. They transmit radio signals towards the ground that carry timing and position information. Your GPS receiver (like the EM-406A) simultaneously receives signals from a number of satellites and, by measuring incredibly small fractions of a second between the signals arriving from different satellites, can triangulate its own position.
Usually people are disappointed to find their GPS device doesn't give positions accurate down to the millimetre. But when you realise that hundreds of millions of dollars worth of satellites are beaming down free information that you can use on your $80 (Australian) receiver, and it works all over the world, you can forgive a baby receiver like the EM-406A for being a little 'sloppy'. But having position down to five or ten metres is incredibly powerful. Many receivers can do better than this, but interference plays a big part in determining accuracy.
GPS works by using radio waves. Because of this, signals are blocked or interfered with when the receiver is underground, under a bridge, in a building, next to a wall, even under a tree. You might get no information at all from your GPS or worse, get wrong information! It's also a one-way communication system. The satellites talk but don't listen. There's no way of transmitting information for a satellite to receive and there's certainly no Internet access. If you want communications, that's another thing entirely.
Part 1: Getting Data Out
AssumptionsI'm going to assume you:
- have an Arduino device, either official or a clone, and
- have an EM-406A GPS device and the necessary cables/shields to connect it, and
- you've downloaded and installed the Arduino software, and
- you are comfortable with at least pasting someone else's code into a sketch and uploading it onto your Arduino device
I'm using Arduino software version 1.0.
OK, starting with a very basic Arduino device. I'm using my Arduino Duemilanove. It's been superseded by the Uno but is much the same. I always start with a basic sketch like BlinkingLED to make sure everything works. I can't stress this enough. If you aren't very familiar with programming or microcontrollers, you're tempted to buy parts, download someone else's code and then get upset at the whole thing. Take a breath, start simple, build up.
Here's what I usually do:
- Unplug the Arduino
- Open the Arduino software and select Serial Port from the Tools menu. Right now, only COM1 shows up.
- Plug in the Arduino and check Serial Port on the Tools menu again
- Voila! COM3 has appeared and must be my Arduino. I select COM3.
- Next, set the correct Board on the Tools menu. Sometimes there isn't an exact match for your board so you'll need to research which option to use.
- Go to the Arduino web site, copy the code for the BlinkingLED application and paste it in.
- Upload and run the sketch and ensure you get the expected result. My LED's blinking like a winner.
I'm sure it would be helpful to show what can go wrong but there are so many possible issues it's impossible to cover them. Here are two basic failures:
- By selecting the wrong COM port, I got this:
- By selecting the wrong board, the Arduino program said Uploading... and never responded after that. I had to close and re-open the program.
So at this point we know the Arduino device works, the Arduino program works and they play well together. If you haven't got this far it's time to turn around. Whenever you get stuck, the forum is your friend; or lots of friends! But read first! In 99.9% of cases the problem you're having has been seen and solved before. You just need to find the solution that's already documented for you.
Adding the GPS
OK, so now the cool stuff. I'd like to be able to cover a number of different methods for connecting the GPS to an Arduino. Unfortunately I only have two available to me; the SparkFun shield and a breadboard. I'll do the shield first.
SparkFun GPS Shield (version 13)
I've got a SparkFun GPS Shield for my EM-406A with a short data cable. When I plug this together with my Arduino Duemilanove the lights come on and everything looks great. The EM-406A's LED starts to blink after a while, telling me it's got a fix!
if (Serial.available()) Serial.write(Serial.read());
Let's do a quick walk through on this sketch. The void setup() line and the opening and closing curly braces following it are essential, every Arduino sketch has them. Likewise the void loop() line and it's curly braces. So they're just structure. The setup() function runs once when your program begins to, well, set-up anything that needs to be configured just once. The loop() function starts after setup() and when it finishes, it starts all over again, in a loop.
So the first of only two lines of interesting code in this program
configures the Arduino's one and only serial port to run at 4800 baud. If you check the manual for the EM-406A you'll see it uses this speed by default. But the trick here is that the serial port is connected to two devices! This is not normal. But in this case, it should work. The Arduino's microcontroller is connected to an FTDI chip that handles USB communications to the PC. With the GPS shield connected, the same serial port is also connected to the GPS. In our application the Arduino is receiving data from the GPS and sending data to the host computer.
What I want is the serial data that comes out of the GPS, to be displayed on the screen in the Serial Monitor, like this:
With the connections as shown above, I'm getting screenfulls of GPS data out of my EM-406A with essentially a 2-line program.
Next time we'll take a look at all this text that's coming out of the GPS and work out what it means and how to interpret it.
That is all.