EM-406A GPS From Scratch - Part 1

Grade: Beginner

Don'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 GPS

The 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

Assumptions

I'm going to assume you:
  1. have an Arduino device, either official or a clone, and
  2. have an EM-406A GPS device and the necessary cables/shields to connect it, and
  3. you've downloaded and installed the Arduino software, and
  4. 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.

Starting Off

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:
  1. Unplug the Arduino
  2. Open the Arduino software and select Serial Port from the Tools menu. Right now, only COM1 shows up.
  3. Plug in the Arduino and check Serial Port on the Tools menu again
  4. Voila! COM3 has appeared and must be my Arduino. I select COM3.
  5. 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.
  6. Go to the Arduino web site, copy the code for the  BlinkingLED application and paste it in.
  7. 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!
I wrote the shortest of all sketches to test this out.

void setup()
{
  Serial.begin(4800);
}


void loop()
{
  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
Serial.begin(4800);
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:
But an interesting thing happened when I tried it. Nothing. Nothing happened. I tried another Arduino (clone) device I have, DFRobot Mega 2560. Nothing happened again. So it wasn't my Arduino Duemilanove at fault. "What's wrong with the GPS?" I though. I took the shield off and connected it with jumper wires. Still nothing. Then I connected the shield and Arduino the way the pin labels suggest: TX (transmit) on the GPS to RX (recieve) on the Arduino. Booyah! It works.
So it looks like this version of the GPS shield is a dud. The transmit and receive connections are backwards. Bummer. I've noticed SparkFun don't sell my version of the shield any more (wonder why?) so for a few bucks I'll be I can just replace it with the latest version.
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.

Comments

  1. Christian, Nice job of breaking down to a simple and understandable format. Do you know if this can be down with the Mega 2560? I'm a total newbie and am stumbling my way though, so simple works for me.
    Thanks,
    Al

    ReplyDelete
  2. Hey, awesome blog thanx for the advice:D

    however i did everything u said and there is no output AT ALL!!.. nothingg.. im unsing arduino uno R3.. and have the shield and the exact same gps from sparkfun... any help is appreciated.. thnx

    ReplyDelete
  3. Very interesting. Just for completion it might be worth mentioning what the GPS module default settings are for Parity and Number of Stop Bits. (I have no idea what these are because the documentation does not say - unless I have missed it)

    Regards, and thanks for putting in the effort,

    Steve

    ReplyDelete
  4. Apologies for the lack of feedback from me. I wasn't getting email notices for comments.

    GPS Guy: Yes, a Mega will do a great job. The Mega has four serial ports (UARTs). If you follow this example you'll need to use Serial Port 0. On my DFRobot Mega 2560, that's pins TX0 and RX0.

    BigBoi!, did you work it out? I'd still like to help if you haven't.

    Steve, thanks for the input. I haven't made the update yet.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi there... I own a Duemilanove and I found a software solution instead of using physical cables the best way to have the base arduino stacked with the GPS shield. I have the GPS chip on a Sparkfun CAN-BUS shield but the approach is the same. Here's the code for it. Note: Set Serial Monitor to 9600!:

    I've deleted the previous post since not all the code was pasted ;-)

    #include
    SoftwareSerial GPS(4,5); // RX/TX Digital Pins for GPS


    void setup()
    {
    Serial.begin(9600);
    GPS.begin(4800);
    }


    void loop()
    {
    GPS.listen();

    if(GPS.available())
    {
    Serial.write(GPS.read());
    }
    }

    ReplyDelete
  7. Just FYI I am connecting the shield as it is and not like the author suggested here cross connecting RX<->TX pins and it works fine.

    ReplyDelete
  8. The software development company helps the business in getting their required programming matching with business requirements

    ReplyDelete
  9. Hello everybody, I am using arduino mega and wants to communicate with device GR-213 gps receiver, i don't have usb host shield.. Is communication possible without usb host shield.

    ReplyDelete

Post a Comment