libelium
  • rss
  • flickr
  • instagram
  • twitter
  • facebook
  • youtube

Documentation: GPS Module for Raspberry Pi Tutorial

Contents:

Introduction

Ingredients:

Difficulty: Medium -medium

Preparation Time: 30 minutes

gps_module
 

NOTE: All the code examples in this tutorial use the arduPi library. You can see the documentation and download the library here.

 

Steps Index

Steps Index:

  1. Connecting the GPS module to Raspberry Pi.
  2. Getting GPS NMEA sentences using cutecom.
  3. Getting GPS NMEA sentences in a Raspberry Pi program.
  4. Controlling the output of standard NMEA messages.
  5. Important Issues.
  6. Video-Tutorial.

Step 1: Connecting the GPS module to Raspberry Pi:

The GPS module for Raspberry Pi is a small electronic circuit that allows to connect to your 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.

gps_module

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). See the picture.

. gps_module

Step 2: Getting GPS NMEA sentences in the PC:

Open cutecom in the Raspberry Pi. Use the following serial port configuration: 4800 baud, 8 data bits, no parity, 1 stop bit, no flow control.

gps_module

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

gps_module

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

Step 3: Getting GPS NMEA sentences in Raspberry Pi:

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

gps_module

Output:

gps_module

The code:

 
/*
*  Copyright (C) 2010 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.
* 
*  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 .
*
*  Version 0.1
*  Author: Anartz Nuin Jiménez
*/

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


// variables
byte 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);  
  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
  fprintf(stderr,"GGA sentence: ");
  h = 0;
  while(GPS_GGA[h] != 42){
    fprintf(stderr,"%c",GPS_GGA[h]);
    h++;
  }
  fprintf(stderr,"\n");
  
  // print the RMC sentence to USB
  fprintf(stderr,"RMC sentence: ");
  h = 0;
  while(GPS_RMC[h] != 42){
    fprintf(stderr,"%c",GPS_RMC[h]);
    h++;
  }
  fprintf(stderr,"\n");
}


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

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.

 

Links and Documentation

Links and Documentation: