My Cart

GPS Module for Arduino and Raspberry Pi

Difficulty Level: Beginner -

Introduction

Ingredients:

Preparation Time: 30 minutes

The GPS module for Arduino and Raspbery Pi is a small electronic circuit that allows to connect to your Arduino/Raspberry Pi board to get position and altitude, as well as speed, date and time on UTC (Universal Time Coordinated). It uses the standard NMEA protocol ( http://www.nmea.org ) to transmit the position data via serial port.

Step 1: Connecting the GPS module (Gateway mode)

Connecting the GPS module to Arduino (Gateway mode)

Now you have two options: you can upload a "BareMinimum" to Arduino or remove the Microcontroller (Atmega328) from Arduino to use it in gateway mode.

Once the GPS is connected you have to connect power to it. Using two small wires (red for 5V and black for GND). See the picture.

Connecting the GPS module to Raspberry Pi (Gateway mode)

For connecting the GPS module to a Raspberry Pi we need to use the Rx and Tx pins of the shield.

Once the GPS is connected, you have to connect power to it. Using two small wires (red for 5V and black for GND). The switch in the connection bridge must be at the right. See the picture.

Step 2: Getting GPS NMEA sentences in gateway mode

Getting GPS NMEA sentences in gateway mode with Arduino

Finally, plug the USB cable to your computer and open a terminal program to listen the serial port. Use the following serial port configuration: 4800 baud, 8 data bits, no parity, 1 stop bit, no flow control.

You'll see the NMEA sentences coming from the GPS module.

With these NMEA sentences you get position, altitude, speed data...etc: ( http://www.nmea.org ).

Getting GPS NMEA sentences in gateway mode with Raspberry Pi

To stablish UART connection with the module you can use several programs as cutecom in Raspbian graphical environment or minicom in a terminal window in order to use it via SSH . For using minicom follow these steps:

  1. Open a Terminal window on Raspberry Pi, or connect to Raspberry Pi through SSH. In order to enter via SSH change here the IP from your Raspberry Pi. The default user is pi.
    ssh -X pi@192.168.1.X

    Now you will have to type your password. The default password is “raspberry”.

  2. Install minicom (only if you haven't done it before):
    sudo apt-get install minicom

  3. Open comunication with the module UART, the baudrate is 4800:
    minicom -b 4800 -o -D /dev/ttyAMA0

  4. Then you'll see the NMEA sentences coming from the GPS module.

  5. To exit minicom press CTRL+A, then press X and Yes.

Step 3: Getting GPS NMEA sentences:

Getting GPS NMEA sentences in Arduino

Connect the GPS to Arduino (with Atmega microcontroller connected) as shown in the picture.

Upload the code to Arduino and watch the Serial monitor.

Arduino code:

Code:
/*  
 *  GPS Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           2.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Yarza, Jorge Casanova & Marcos Martinez
 */

// Include the SoftwareSerial library
#include "SoftwareSerial.h"

// Constants
#define txPin 8      //tx pin in GPS connection
#define rxPin 9      //rx pin in GPS connection

// Set up the GPS serial port
SoftwareSerial gps = SoftwareSerial(rxPin, txPin);


// Variables
byte byteGPS = 0;  
int i = 0;
int state = 0;
char dataGPG[100]="";
char *pch;
char *GGA[15];
int sat = 0;

void setup()
{
//setup for Serial Port
Serial.begin(9600);
Serial.flush();
 
//setup for GPS Serial Port  
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
gps.begin(4800);
gps.flush();

//setup satellites signal
pinMode(13, OUTPUT);
digitalWrite(13, LOW);     // Turn off the led until a satellite signal
}


void loop()
{
gps.flush();
Serial.flush();

Serial.println("Waiting GPS data...");

// Prepare all for reading GPS Serial Port
memset(dataGPG, 0, sizeof(dataGPG));    // Remove previous readings
byteGPS = 0;                            // Remove data
byteGPS = gps.read();                   // Read the byte that is in the GPS serial port
delay(1000);

// Find the desired string
while(byteGPS != '$')
{
byteGPS = gps.read();
} 

// Save the string in an array
i=1;
dataGPG[0] = '$';

while(byteGPS != '*' )
{
byteGPS = gps.read();
dataGPG[i]=byteGPS; 
i++; 
}

dataGPG[i]= '\0';
string();                                 // Call to the function that manipulates our string

}

/*
This function will allow us to identify the data we need to get the longitude, latitude ...
*/

void string()
{
i=0;
memset(GGA, 0, sizeof(GGA));          // Remove previous readings

pch = strtok (dataGPG,",");

// Analyze the saved interval in pch to see if it the needed string
if (strcmp(pch,"$GPGGA")==0)
{
while (pch != NULL)
{
pch = strtok (NULL, ",");
GGA[i]=pch;    
i++;
} 

plot(GGA,"$GPGGA");         // Call to the function that is going to display the data
}
}

/*
This function organize the gps data received for printing in the serial monitor.
*/

void plot(char **GGAPrint, char *trama)
{
state = atoi(GGAPrint[5]);
sat = atoi(GGAPrint[6]);

if(trama=="$GPGGA" && state==1)
{
digitalWrite(13, HIGH);            // Then there are satellites, the LED switch ON

Serial.println("");
Serial.println("----------------------------------------------");
Serial.print("UTC Hour -> ");
Serial.println(GGAPrint[0]);
Serial.print("Latitude -> ");
Serial.print(GGAPrint[1]);
Serial.println(GGAPrint[2]);
Serial.print("Longitude -> ");
Serial.print(GGAPrint[3]);
Serial.println(GGAPrint[4]);
Serial.print("GPS quality: 0=null; 1=GPS fixed -> ");
Serial.println(GGAPrint[5]);
Serial.print("Number of satellites -> ");
Serial.println(sat);
Serial.print("Horizontal Dilution of Precision -> ");
Serial.println(GGAPrint[7]);
Serial.print("Antenna altitude -> ");
Serial.print(GGAPrint[8]);
Serial.println(GGAPrint[9]);
Serial.print("Geoid Separation -> ");
Serial.print(GGAPrint[10]);
Serial.println(GGAPrint[11]);
Serial.println("----------------------------------------------");
Serial.println("");

}

else                                // If no satellite connection
{
digitalWrite(13, LOW);                                              // Turn off the LED
Serial.println("");
Serial.println("-----------------------------");
Serial.print("|--- Satellites Used -->");
Serial.print(sat);
Serial.println(" |");
Serial.println("|----Waiting location----|");
Serial.println("-----------------------------");
Serial.println("");
}
}		
		

Serial monitor:

NOTE: With some sketches, the buffer of the Software Serial may be small, to increase the length of the buffer you need to change this line in the file SoftwareSerial.h (/arduino-1.0.X/hardware/arduino/libraries/SoftwareSerial):

#define _SS_MAX_RX_BUFF 64 // RX buffer size
for
#define _SS_MAX_RX_BUFF 194 // RX buffer size

Getting GPS NMEA sentences in Raspberry Pi

Connect the GPS to Raspberry Pi as shown in the picture.

Raspberry Pi code:

Code:
/*  
 *  GPS Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           2.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Yarza, Jorge Casanova & Marcos Martinez
 */

//Include ArduPi library
#include "arduPi.h"


// variables
char byteGPS = 0;
int i = 0;
int h = 0;

// Buffers for data input
char inBuffer[300] = "";
char GPS_RMC[100]="";
char GPS_GGA[100]="";

void setup(){

  //setup for mySerial port
  Serial.begin(4800); 
  printf("Starting...\n");
  delay(1000);
}

void loop(){
	
  
   // Read the RMC sentence from GPS
  byteGPS = 0;
  byteGPS = Serial.read();
  while(byteGPS != 'R'){
    byteGPS = Serial.read();
  }
  
  GPS_RMC[0]='$';
  GPS_RMC[1]='G';
  GPS_RMC[2]='P';    
  GPS_RMC[3]='R';
  
    i = 4;
    while(byteGPS != '*'){                  
      byteGPS = Serial.read();         
      inBuffer[i]=byteGPS;
      GPS_RMC[i]=byteGPS;
      i++;                      
  }
  
  // Read GGA sentence from GPS
  byteGPS = 0;
  byteGPS = Serial.read();
  while(byteGPS != 'A'){
    byteGPS = Serial.read();
  }
  GPS_GGA[0]='$';
  GPS_GGA[1]='G';
  GPS_GGA[2]='P';    
  GPS_GGA[3]='G';
  GPS_GGA[4]='G';
  GPS_GGA[5]='A';
  
    i = 6;
    while(byteGPS != '*'){                  
      byteGPS = Serial.read();         
      inBuffer[i]=byteGPS;
      GPS_GGA[i]=byteGPS;
      i++;                      
  }
    
  // print the GGA sentence to USB
  printf("GGA sentence: \n");
  h = 0;
  while(GPS_GGA[h] != 42){
    printf("%c",GPS_GGA[h]);
    h++;
  }
  printf("\n");
  
  // print the RMC sentence to USB
  printf("RMC sentence: \n");
  h = 0;
  while(GPS_RMC[h] != 42){
    printf("%c",GPS_RMC[h]);
    h++;
  }
  printf("\n");
}


int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

	
NOTE: The example developed for Raspberry Pi use the arduPi library. You can see the documentation and download the library here.

Step 4: Controlling the output of standard NMEA messages

There are some NMEA commands that we can use for controlling the output NMEA messages of the GPS module. We can activate / deactivate each NMEA message (GGA, GLL, GSA, GSV, RMC and VTG) sending to the module these commands.

GGA:

Activate => $PSRF103,00,00,01,01*25

Deactivate =>$PSRF103,00,00,00,01*24

GLL:

Activate => $PSRF103,01,00,01,01*24

Deactivate => $PSRF103,01,00,00,01*25

GSA:

Activate => $PSRF103,02,00,01,01*27

Deactivate => $PSRF103,02,00,00,01*26

GSV:

Activate => $PSRF103,03,00,01,01*26

Deactivate => $PSRF103,03,00,00,01*27

RMC:

Activate => $PSRF103,04,00,01,01*21

Deactivate => $PSRF103,04,00,00,01*20

VTG:

Activate => $PSRF103,05,00,01,01*20

Deactivate => $PSRF103,05,00,00,01*21

Also it is possible to change the frequency of the output, see the GPS module firmware documentation.

This configuration will be lost when the GPS module is powered OFF.

Step 5: Important Issues

Important Issues:

  • Handle with care the internal antenna. It's fragile.
  • The GPS module should be inside a plastic box, isolated from the environment.
  • The antenna must be in horizontal position.
  • For improving the satellites signal, the antenna has to be in a place with a clear sky view (no trees, no buildings...).
  • Maybe the GPS module takes 2-3 minutes to get signal the firs time.

Step 6: Video-Tutorial

Here's an explanatory video, which shows the whole process developed in this tutorial:

Step 7: Coordinates Conversion / Representation in The Plane

If you want to convert the output of your GPS module to the coordinates format that you need, you can visit this website to make it easier:

http://www.earthpoint.us/convert.aspx

As you can see in the step 3, you can watch in the Serial monitor the next data:

Latitude: 4140,8317
Longitude: 00053,1717

We write them in the website and we will get the values based on Degrees Lat Long to seven decimal places:

If you want more information you can read the GPS module firmware documentation .

Fritzing Libraries

Download Fritzing GPS Module for Arduino and Raspberry Pi

GPS Module for Arduino and Raspberry Pi is a small electronic circuit that allows to connect to your Arduino board to get position and altitude, as well as speed, date and time on UTC (Universal Time Coordinated).

GPS Module for Arduino is a perfect complement for developing geolocalization applications.

You can download our Fritzing libraries from this area.

Links and Documentation





If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: