Mini GPS Shield Hookup Guide

Pages
Contributors: Alex the Giant
Favorited Favorite 5

Arduino Examples

Now that we have everything connected, lets get started with some code. Before we start writing code though, we do need to install a library to parse the GPS messages. If you haven't worked with downloading Arduino libraries before or you just need a quick refresh, check out our tutorial on installing Arduino libraries. The library we need is called TinyGPS++ from Mikal Hart. You can download and install the library from the link below.

Now that we have the library installed, let's look at the code.

In this first example, we'll test our our GPS and make sure we're able to get a signal from the satellites.

language:c
/******************************************************************************
Mini_GPS_Shield_Serial_Example.ino
Example using the Mini GPS Shield with the GP-735
Alex Wende @ SparkFun Electronics
October 12th 2016
~

This sketches uses the Mini GPS Shield with the 
GP-735 (https://www.sparkfun.com/products/13670). The Arduino reads the data 
from the GPS module on the defined software serial pins, and prints data on 
to the serial window.

Resources:
SoftwareSerial.h (included with Arduino IDE)
TinyGPS++.h

Development environment specifics:
Arduino 1.0+
Hardware Version 10

This code is beerware; if you see me (or any other SparkFun employee) at
the local, and you've found our code helpful, please buy us a round!

Distributed as-is; no warranty is given.   
******************************************************************************/

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

#define RX_PIN  4 // GPS TX
#define TX_PIN  5 // GPS RX
#define LED_PIN 7 // GPS Fix LED
#define GPS_BAUD  9600  // GP-735 default baud rate

TinyGPSPlus gps;

SoftwareSerial ss(RX_PIN,TX_PIN);

void setup() {
  Serial.begin(115200); // Begin serial communication with computer
  ss.begin(GPS_BAUD); // Begin serial communication with GPS

  pinMode(LED_PIN,OUTPUT);

  Serial.println("Date       Time        Latitude   Longitude   Alt    Course Heading Speed");
  Serial.println("(MM/DD/YY) (HH/MM/SS)     (deg)       (deg)  (ft)                   (mph)");
  Serial.println("-------------------------------------------------------------------------"); 
}

void loop() {
  char gpsDate[10];
  char gpsTime[10];

  if(gps.location.isValid()){ // GPS has a fix
    digitalWrite(LED_PIN,HIGH); // Turn LED on

    sprintf(gpsDate,"%d/%d/%d", gps.date.month(),gps.date.day(),gps.date.year()); // Build date string
    sprintf(gpsTime,"%d/%d/0%d", gps.time.hour(),gps.time.minute(),gps.time.second());  // Build time string

    Serial.print(gpsDate);
    Serial.print('\t');
    Serial.print(gpsTime);
    Serial.print('\t');
    Serial.print(gps.location.lat(),6);
    Serial.print('\t');
    Serial.print(gps.location.lng(),6);
    Serial.print('\t');
    Serial.print(gps.altitude.feet());
    Serial.print('\t');
    Serial.print(gps.course.deg(),2);
    Serial.print('\t');
    Serial.println(gps.speed.mph(),2);
  }
  else  // GPS is looking for satellites, waiting on fix
  {
    digitalWrite(LED_PIN,LOW);  // Turn LED off
    Serial.print("Satellites in view: ");
    Serial.println(gps.satellites.value());
  }
  smartDelay(1000);
}

// Delay ms while still reading data packets from GPS
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while(ss.available())
    {
      gps.encode(ss.read());  
    }
  } while(millis() - start < ms);
}

Our second example is very similar to our first example. In this example though, instead of displaying the GPS data in just the serial window, we'll also log the data to a microSD card. Note that this example makes use of the built-in SD library in the Arduino IDE.

language:c
/******************************************************************************
Mini_GPS_Shield_Data_Log_Example.ino
Example using the Mini GPS Shield with the GP-735
Alex Wende @ SparkFun Electronics
October 12th 2016
~

This sketches uses the Mini GPS Shield with the 
GP-735 (https://www.sparkfun.com/products/13670) and a microSD card. The
Arduino reads the data from the GPS module on the defined software serial
pins, and saves the data to a SD card.

Resources:
SoftwareSerial.h (included with Arduino IDE)
SD.h (included with Arduino IDE)
SPI.h (included with Arduino IDE)
TinyGPS++.h

Development environment specifics:
Arduino 1.0+
Hardware Version 10

This code is beerware; if you see me (or any other SparkFun employee) at
the local, and you've found our code helpful, please buy us a round!

Distributed as-is; no warranty is given.   
******************************************************************************/

#include <SPI.h>
#include <SD.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define RX_PIN  4 // GPS TX
#define TX_PIN  5 // GPS RX
#define LED_PIN 7 // GPS Fix LED
#define CHIP_SELECT 10  // uSD card 
#define GPS_BAUD  9600  // GP-735 default baud rate

TinyGPSPlus gps;
File myFile;
SoftwareSerial ss(RX_PIN,TX_PIN);

void setup() {
  Serial.begin(115200); // Begin serial communication with computer
  ss.begin(GPS_BAUD); // Begin serial communication with GPS

  pinMode(LED_PIN,OUTPUT);

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(CHIP_SELECT)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  myFile = SD.open("data.txt", FILE_WRITE); // Create or open a file called "data.txt" on the SD card
  if(myFile) 
  {
    if(myFile.size() == 0)  // Only create the header if there isn't any data in the file yet
    {
      myFile.println("Date\t\tTime\t\tLatitude\tLongitude\tAlt\tCourse\tSpeed");
      myFile.println("MM/DD/YYYY\tHH/MM/SS\tdeg\t\tdeg\t\tft\t\tmph");
      myFile.println("-------------------------------------------------------------------------------------");
    }
    myFile.close(); // Close the file to properly save the data
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  char gpsDate[10], gpsTime[10];

  if(gps.location.isValid()){ // GPS has a fix
    digitalWrite(LED_PIN,HIGH); // Turn LED on
    myFile = SD.open("data.txt", FILE_WRITE); // Open file "data.txt"
    if(myFile)
    {
      // Get date and time
      sprintf(gpsDate,"%d/%d/%d", gps.date.month(),gps.date.day(),gps.date.year());
      if(gps.time.second() < 10){
        sprintf(gpsTime,"%d/%d/0%d", gps.time.hour(),gps.time.minute(),gps.time.second());
      }
      else
      {
        sprintf(gpsTime,"%d/%d/%d", gps.time.hour(),gps.time.minute(),gps.time.second());
      }

      // Save data to SD card
      myFile.print(gpsDate);
      myFile.print('\t');
      myFile.print(gpsTime);
      myFile.print('\t');
      myFile.print(gps.location.lat(),6);
      myFile.print('\t');
      myFile.print(gps.location.lng(),6);
      myFile.print('\t');
      myFile.print(gps.altitude.feet());
      myFile.print('\t');
      myFile.print(gps.course.deg(),2);
      myFile.print('\t');
      myFile.println(gps.speed.mph(),2);

      // Print GPS data to serial window
      Serial.print(gpsDate);
      Serial.print('\t');
      Serial.print(gpsTime);
      Serial.print('\t');
      Serial.print(gps.location.lat(),6);
      Serial.print('\t');
      Serial.print(gps.location.lng(),6);
      Serial.print('\t');
      Serial.print(gps.altitude.feet());
      Serial.print('\t');
      Serial.print(gps.course.deg(),2);
      Serial.print('\t');
      Serial.println(gps.speed.mph(),2);
    }

    myFile.close(); // Close the file to properly save the data
  }
  else  // GPS is looking for satellites, waiting on fix
  {
    digitalWrite(LED_PIN,LOW);  // Turn LED off
    Serial.print("Satellites in view: ");
    Serial.println(gps.satellites.value());
  }

  smartDelay(1000);
}

// Delay ms while still reading data packets from GPS
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while(ss.available())
    {
      gps.encode(ss.read());  
    }
  } while(millis() - start < ms);
}

Troubleshooting Tips

If you're testing your GPS inside, make sure you're next to a window. One of the problems with GPS is the radio waves have a difficult time passing through roofs and ceilings, so, the closer the GPS is to being outside, the better. If you still aren't reading from enough satellites to get a position fix, try pointing the GPS antenna in a different direction.

Due to the symmetrical pins of the mini boards, installing the shield backwards is an easy mistake to make. The correct orientation has the GPS connector on the same side as the USB or FTDI connection and the microSD card over the crystal and reset button. See the Hardware Hookup section for a picture of the correct orientation.

If you are unable to access the SD card, make sure your card is installed with the pins facing down and that you've initialized the SD card's chip select as pin 10 when you call SD.begin(CHIP_SELECT).