Arduino GPS Tutorial: Get Latitude and Longitude Coordinates

by Miguel on December 3, 2012

in Arduino

an Arduino Uno development board connected to a GPS receiver module

The Arduino connected to a GPS receiver module

This software was tested with parallax’s PMB-648 GPS SiRF Internal Antenna but will work with any GPS module that uses the same type of communication protocol.

Hardware Connections:

According to the module’s datasheet the power can be from 3.3V to 5V. The yellow wire is the wire through which the module sends GPS satellite data to devices which ask for it, in this case the Arduino.

  • Red wire to 5V pin
  • Black wire to GND pin
  • Yellow wire to pin 2

Software Set Up:

Download the TinyGPS library, extract the contents and put the “TinyGPS” inside your Arduino’s “libraries” folder.

Code:

Upload the following code to the Arduino and open the serial monitor window. The code will print out the latitude and longitude values from the GPS receiver. Remember to divide the GPS values by a factor of 10 so that they fall within the GPS range. Example: if your serial monitor value is printing 37664939 and -121234543 for your latitude and longitude values respectively, then divide each by 1000000 to get the real value.

#include <SoftwareSerial.h>
#include <TinyGPS.h>

long lat,lon; // create variable for latitude and longitude object

SoftwareSerial gpsSerial(2, 3); // create gps sensor connection
TinyGPS gps; // create gps object

void setup(){
  Serial.begin(9600); // connect serial
  gpsSerial.begin(4800); // connect gps sensor
}

void loop(){
  while(gpsSerial.available()){ // check for gps data
   if(gps.encode(gpsSerial.read())){ // encode gps data
    gps.get_position(&lat,&lon); // get latitude and longitude
    // display position
    Serial.print("Position: ");
    Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
    Serial.print("lon: ");Serial.println(lon); // print longitude
   }
  }
}

How To Plot Your Receiver’s Data On Google

Google your latitude separated by a comma followed with the longitude. Example: 37.664939, -121.234543

Code Explanation:

I will now explain the crucial parts of the program.

The GPS receiver uses serial communication so we will first need to create a set of serial pins, we can’t use the Arduino’s 0 and 1 serial pins because these are used to program the board ( we actually can use them but you would have to disconnect your GPS receiver from the board every time you want to upload a new program ).

The serial pins will be created in pin 2 and 3 for RX and TX respectively, we will not be connecting anything to pin 3 however because we do not wish to send any data to the GPS module, only receive. We also need an object of the TinyGPS library:

SoftwareSerial gpsSerial(2, 3); // create gps sensor connection
TinyGPS gps; // create gps object

The “gpsSerial” object is used to extract data from the GPS receiver, the “gps” object is used to separate the receiver’s data into individual components.

We first check if there is any data available in the receiver.

while(gpsSerial.available())

Then read and encode the data

if(gps.encode(gpsSerial.read()))

Lastly we store the latitude and longitude values into the lat and lon variables.

gps.get_position(&lat,&lon); // store values into lat and lon variables (passed by reference)

and that is it.

Previous post:

Next post: