Introduction to Arduino



Reading Location, Date, Time, Speed and Heading data from a GPS module



Arduino UNO R3 Pin Setup


Overview:

    Have you ever wanted to pull Date, Time, and Location data from a GPS module? Here is how I did it. I used a simple Adafruit Ultimate GPS (V3) module and wired it up to my Arduino UNO (image shown above).

    If you have not already done so, you need to install a couple libraries. These are easy to install and only need to be installed once. Go to Tools / Manage Libraries and search for these libraries:

      Adafruit_GPS
      SoftwareSerial

    After wiring the GPS to the UNO and uploading the code (found below), I waited until the GPS calculated its location (called a "fix"). This took a couple minutes and it works best if you are outside with a clear view of the sky, however I did manage to get a fix with the unit sitting on my desk inside my house but don’t count on being this lucky. A small red LED on the GPS board will flash every second when it is searching for satellites, it will flash once every 15 seconds when it has calculated a fix.

    Raw GPS data normally comes formatted in lines of information called NMEA (National Marine Electronics Association) sentences. A GPS can deliver several NMEA sentences, each containing different pieces of information but we are only interested in the data contained in the RMC (Recommended Minimum Navigation Information) and GGA (Global Positioning System Fix Data) sentences. We use a technique called Parsing to pull out the information we want from each of these sentences.

    After the GPS has a fix, you can view your data through your Serial Monitor once every second (1 Hz). I have it set up to return the following information:

      Year/Month/Day,Hour:Minute:Second,Latitude,Longitude,Altitude(m),Number of Satellites,Speed(knots),Heading(degrees)
      Note: Speed and Heading are based on the distance and direction moved from the previous coordinate so you need to be moving for these values to be useful.

    IMPORTANT: GPS uses Universal Time Coordinated (UTC) which used to be called Greenwich Mean Time (GMT), so you will need to apply the time zone difference between Greenwich, England and your location if you want to use the time for local reference.


Example of the parsed GPS data:

    23/04/10,08:01:59,39.06455,-94.33780,96.60,4,0.51,222.59
    23/04/10,08:02:00,39.06455,-94.33780,96.60,4,0.68,358.50
    23/04/10,08:02:01,39.06457,-94.33779,96.40,4,0.85,357.85
    23/04/10,08:02:02,39.06459,-94.33779,96.10,4,0.91,1.64
    23/04/10,08:02:03,39.06461,-94.33779,95.80,4,1.11,2.73
    23/04/10,08:02:04,39.06464,-94.33779,95.50,4,1.17,4.04
    23/04/10,08:02:08,39.06460,-94.33779,95.30,4,0.76,354.57
    23/04/10,08:02:09,39.06459,-94.33779,95.20,4,0.72,352.06
    23/04/10,08:02:10,39.06459,-94.33779,95.00,4,0.65,357.83


Parts List:

    1 Adafruit Ultimate GPS (V3)
    1 Arduino UNO R3
    1 Breadboard
    Connector Wires


Code:
    //------------- Code Starts Here ----------------------------
    
    //-----------------------------------------
    //Published by IntroductionToArduino.com
    //Created by Paul Illsley (www.paulillsley.com)
    //Please use and share so others can enjoy
    //-----------------------------------------
    
    // Install the Adafruit GPS library
    #include <Adafruit_GPS.h>
    
    // Install Software Serial library
    #include <SoftwareSerial.h>
    
    //Initialize the Software Serial port (pins 2 and 3)
    SoftwareSerial mySerial(2, 3);
    
    // Creating the GPS Object from mySerial
    Adafruit_GPS GPS(&mySerial);
    
    // Creating "c" to hold characters coming from the GPS
    char c;
    
    void setup() {
    
    // Turn on serial monitor at a baud rate of 9600
    Serial.begin(9600);
    
    // Turn on the GPS at a baud rate of 9600
    GPS.begin(9600);
    
    // Requesting only RMC and GGA NMEA Sentences from the GPS
    GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
    
    // Setting a 1 Hz update rate (from GPS)
    GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
    
    // Delay 1 second to let the commands activate
    delay(1000);
    }
    void loop() {
    
    // Clear corrupted GPS logs from buffer (runs code found at the end)
    clearGPS();
    
    // Reading a new NMEA sentence
    while (!GPS.newNMEAreceived()) {
    c = GPS.read();
    }
    
    // Parse the last good NMEA sentence
    GPS.parse(GPS.lastNMEA());
    
    // If the GPS has a fix, print this information to the Serial Monitor
    if (GPS.fix) {
    Serial.print(GPS.year, DEC);
    Serial.print('/');
    if (GPS.month < 10) Serial.print('0');
    Serial.print(GPS.month, DEC);
    Serial.print('/');
    if (GPS.day < 10) Serial.print('0');
    Serial.print(GPS.day, DEC);
    Serial.print(",");
    if (GPS.hour < 10) Serial.print('0');
    Serial.print(GPS.hour, DEC);
    Serial.print(':');
    if (GPS.minute < 10) Serial.print('0');
    Serial.print(GPS.minute, DEC);
    Serial.print(':');
    if (GPS.seconds < 10) Serial.print('0');
    Serial.print(GPS.seconds, DEC);
    Serial.print(",");
    Serial.print(GPS.latitudeDegrees, 5);
    Serial.print(",");
    Serial.print(GPS.longitudeDegrees, 5);
    Serial.print(",");
    Serial.print(GPS.altitude);
    Serial.print(",");
    Serial.print(GPS.satellites);
    Serial.print(",");
    Serial.print(GPS.speed);
    Serial.print(",");
    Serial.println(GPS.angle);
    }
    }
    
    // Clearing the buffer so you get good GPS data
    void clearGPS() {
    while (!GPS.newNMEAreceived()) {
    c = GPS.read();
    }
    GPS.parse(GPS.lastNMEA());
    }
    
    //------------- Code Stops Here ----------------------------
    
    


    Created by Paul Illsley

    Return to www.introductiontoarduino.com