LESSON 23: Arduino GPS with Data Logger

In this lesson we will extend what we did in lesson 22 to include a data logger with our GPS. You should be sure and do lesson 22 first, as that shows you how to hook up and program the GPS. In this project we will connect an SD Card Reader/Writer, and will power the whole thing with a 9 Volt battery. With this, you can walk around, and the device will constantly monitor and log your position. You can then bring it back inside, and look at where you have been using Google Earth.

GPS Data Logger
Arduino connected to Adafruit ultimate GPS and a n SD card Data Logger

To start with, lets talk about what you will need to do this project. As described earlier, you need to start by doing lesson 22 which will get you going on the GPS hookup and initial software. Now, to move forward, you will need an Arduino, Adafruit Ultimate GPS, Virtuabotix SD card reader, and a Battery Clip to power the arduino during your mobile jaunts.  You will need a 9V battery. If you are going to be doing a lot of mobile work, it is probably worth it to invest in a good 9v Rechargable battery system.

When you get your gear gathered up, you will be ready to get this project going. First, hook the GPS up to the arduino as we did in lesson 22:

Connecting the Adafruit Ultimate GPS Unit to Arduino
GPS Pin Arduino Pin
Vin 5V
GND GND
RX Pin 2
TX Pin 3

You also need to hook up the SC Card Reader/Writer. You should be familiar with this from lesson 21.

Connecting the SD Card Reader
Sd Card Reader Pin Arduino Pin Details
GND GND Common Ground
3.3 V – (NOT USED)
+5 5V Power
CS 4 Chip Select
MOSI 11 SPI Data
SCK 13 Clock
MISO 12 SPI Data
GND GND Common Ground

Remember that this SD card reader has two rows of pins. Don’t let that confuse you, both rows are equivalent. You can connect to either row.

Most of the code has already been written in Lesson  22. In this lesson, the main thing we will be doing is to write the GPS data to the SD card. This way, as we walk around outside, it will log our positions to the SD card.  We will create two files. One will hold the raw NMEA sentences, and the other our location data, as parsed by the adafruit GPS library. You should be able to figure most of this out from what you already know from lesson 22 and lesson 21. In the video above, we take you through this code step-by-step, so watch the video to understand what we are doing. If you get stuck, you can look at the code below.

 

44 thoughts on “LESSON 23: Arduino GPS with Data Logger”

  1. I just stumbled over your blog after searching for Python and Arduino related projects.

    Very clear and easy to understand videos.
    Keep up the great work.

  2. I found this tutorial to be the best for what I wanted to do, and have successfully built a GPS data logger. Thank you for making these excellent tutorials available!
    I changed the NMEA update rate as it wasn’t quite creating a log every second (I’m using it for geotagging photos and need to guarantee a log at least every second to marry up with the photo timestamp). I omitted the code for GPSData.txt as this was superfluous for me. I added an LED that goes high / low before / after opening / closing the SD card file to show that it’s writing to the card, and a buzzer to sound when it gets a fix.
    Apart from that the basic code is identical.
    However, after power on when it gets a GPS fix and tries to write to the card it fails to write (the NMEA.TXT file isn’t created) until the reset button is pressed . Also, which I can’t work out why , is that the LED light is also dim until the reset button is pressed.
    Once reset is pressed the LED is bright and data are written to the SD card, and it all works fine.
    I’ve tried adding delays to the setup to initialise everything slowly, and have completely re-wired it carefully, triple-checking every connection.
    Although I know that after power-on I have to press reset to get it all to work, but I’d like to try and understand why it doesn’t work from initial power-on. I’d welcome your thoughts please.

    1. I am really not sure what is going on. Look at your void setup and maybe add some delays to see if slowing things down helps.
      PJM

  3. I found this tutorial very clear and easy to follow, however I’m wondering how much the code and setup would vary when using a GPS logger shield?

  4. I found this tutorial great for UAV aplications!
    Is there any way to directly save the cordinates in a WSG84 format ? This will simplify a lot the batch processing options for several aplications

    1. I am sure it is doable, you just have to figure out 1) the format you have, 2) the format you want, 3) then do the conversion. Python is very powerful with both numbers and strings, so it is just a matter of doing it.

  5. Hi sir
    I m trying to make a GPS guided ultrasonic obstacle avoidance autonomous vehicle . I am having problem with program I m requesting u to plz guide me.I m using arduino mega2560 with 2ultrasonic sensors one at front and another at back and GPS is CBKAF-1503 with magnetometer .plz help me

  6. Hi,
    I used this lesson to create arduino gps data logger. I used exactly the same hardware except I used arduino mega 2560. I get the serial monitor output as follow.but cannot get data writing to SD card.

    $GPGSA,A,1,,,,,,,,,,,,,,,*1E
    $GPRMC,132129.091,V,,,,,0.00,0.00,230515,,,N*4F
    $GPRMC,132130.091,V,,,,,0.00,0.00,230515,,,N*47
    $PGTOP,11,2*6E
    $GPGSA,A,1,,,,,,,,,,,,,,,*1E
    $GPRMC,132132.091,V,,,,,0.00,0.00,230515,,,N*45
    $GPGSV,1,1,01,02,,,29*71
    $GPRMC,132133.091,V,,,,,0.00,0.00,230515,,,N*44
    $GPRMC,132134.091,V,,,,,0.00,0.00,230515,,,N*43
    $PGTOP,11,2*6E

    I also changed the code to suite my arduino mega as follow

    #include //Load SD card library
    #include //Load SPI Library

    #include //Install the adafruit GPS library
    #include //Load the Software Serial library
    SoftwareSerial mySerial(11,10); //Initialize the Software Serial port
    Adafruit_GPS GPS(&mySerial); //Create the GPS Object

    String NMEA1; //Variable for first NMEA sentence
    String NMEA2; //Variable for second NMEA sentence
    char c; //to read characters coming from the GPS

    int chipSelect = 53; //chipSelect pin for the SD card Reader
    File mySensorData; //Data object you will write your sesnor data to

    void setup() {

    Serial.begin(115200); //Turn on serial monitor
    GPS.begin(9600); //Turn on GPS at 9600 baud
    GPS.sendCommand(“$PGCMD,33,0*6D”); //Turn off antenna update nuisance data
    GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Request RMC and GGA Sentences only
    GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate to 1 hz
    delay(1000);

    pinMode(53, OUTPUT); //Must declare 10 an output and reserve it to keep SD card happy
    SD.begin(chipSelect); //Initialize the SD card reader

    if (SD.exists(“NMEA.txt”)) { //Delete old data files to start fresh
    SD.remove(“NMEA.txt”);
    }
    if (SD.exists(“GPSData.txt”)) { //Delete old data files to start fresh
    SD.remove(“GPSData.txt”);
    }

    }

    void loop() {

    readGPS();

    if(GPS.fix==1) { //Only save data if we have a fix
    mySensorData = SD.open(“NMEA.txt”, FILE_WRITE); //Open file on SD card for writing
    mySensorData.println(NMEA1); //Write first NMEA to SD card
    mySensorData.println(NMEA2); //Write Second NMEA to SD card
    mySensorData.close(); //Close the file
    mySensorData = SD.open(“GPSData.txt”, FILE_WRITE);
    mySensorData.print(GPS.latitude,4); //Write measured latitude to file
    mySensorData.print(GPS.lat); //Which hemisphere N or S
    mySensorData.print(“,”);
    mySensorData.print(GPS.longitude,4); //Write measured longitude to file
    mySensorData.print(GPS.lon); //Which Hemisphere E or W
    mySensorData.print(“,”);
    mySensorData.println(GPS.altitude);
    mySensorData.close();
    }

    }

    void readGPS() {

    clearGPS();
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    NMEA1=GPS.lastNMEA();

    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    NMEA2=GPS.lastNMEA();

    Serial.println(NMEA1);
    Serial.println(NMEA2);
    Serial.println(“”);

    }

    void clearGPS() { //Clear old and corrupt data from serial port
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

    }

    But cant get the data to write in to SD card. any Ideas what went wrong here ?
    Please can you help me on this.

    thanks!

    1. You should start with just some simple demo code not related to this project to just verify that you can write data to an SD card. There are some libraries that come with Arduino and use the sample code. You have to isolate this problem down to just the SD card. Don’t try and debug in the context of the entire project. One thing, make sure the SD card is not “locked”. Many have a little switch that can write protect them. I have not played with Arduino mega so dont have much to say there.

    2. I did the same as you did and found that the Arduino Mega works better when you use the hardware ports.

      Instead of SoftwareSerial use HardwareSerial and the Tx and Rx ports on the Mega

      Worked for me 🙂

  7. The tutorial is wonderful!
    I want to tell you a question. The GPS + SD reader/writer + arduino, connected to the computer, it works fine, but if I used a 9V Battery, the memory SD fails and the suface is heated.
    I used a regulator LM7805 to provide only 5V al SD unit, but do not record the file.

    Do you use something to protect the SD card reader / writer?

    I will appreciate your comments.

    1. The 5 volt output on the arduino should be regulated, so use a volt meter and confirm that you are applying 5 volts to the SD card reader. At 5 volts it should work and not get warm. Make sure you are not putting 9 volts on the SD card reader.

  8. Hi there,
    thank you for tutorials like this one!
    I have a similar project myself, but i’m dealing with an error for quite a while now.
    My components are:
    -Arduino Uno
    -same GPS Modul, but it is on a shield, which necessary pins i connect via wires, because otherwise it would block all the other pins i need for other components (http://www.antrax.de/site/Onlineshop/Home/Arduino-Komponenten/GSM-GPRS-GPS-Shield::51.html)
    -same SD-Card reader
    (- sonar for water depth information via TX/RX)

    The thing is, that i want to communicate via the SPI data bus. Do You have a clou, how you can get the GPS Data via SPI bit transfer (Instead of using the serial port) from the GPS-Module? Or is there another way, i did overlook?

    Thanks in advance for your comment!

  9. hi..i wana know that is there any alternate brand of the card reader that can be used insted of the brand which u used with the same library and codes.?

  10. Hello
    I used Arduino UNO but it doesn’t work with this programm when I compile it they write that the memory isn’t enough
    Thanks in advance for your comment !

  11. I am looking to the raspberry PI to fill a need for Search and Rescue.

    I would like to something close to a tablet called “Earl”, but be able to track people in the field.

    I understand that “line of site” my be an issue, but what to know your thoughts.

    Seems like your lessons are heading in that direction, but I am wondering about range?

    Thanks!

  12. Thank you very much for your great tutorials.
    Do you have any recommendation about how to send the GPS data (location and altitude) of a high altitude meteorological balloon down to earth real time, or close to real time, via radio transmission. I do not know which Arduino shield to look for (e.g. at Adafruit) that would be able to transmit data by radio waves over a long distance (i.e. 50km away !)
    Thks for your help !

    1. That is not something you can do with an arduino shield. The way we do it is put the arduino in an ethernet shield, and then do telemetry over ubiquiti bullet radios. This is a fairly advanced thing to try and do.

  13. Hi Paul,

    I have followed your lessons about GPS etc and have also managed to program the arduino to read the gps data on to anSD Card. This is the first ever program I have ever written. I cannot copy your program because I am using a U-BLOX gps. One Problem I have is that the Position given in the GPS data puts me about 25 KM south of where I am really. Do you have an idea what is going wrong. I am making no calculations on the LAT and LON so it cannot be a calculation error. I could send you my code but it is really a mess… but it NEARLY works 🙂
    I might add that you are never too old to learn, I was born in 1948. I want to use the GPS fo tracking my model airplanes on Google earth.
    Many thanks in advance for any comments you might have.

    Best Wishes Paul OE8PCK

    1. Likely problem is that there are many different formats for GPS data, and you are probably running into an issue assuming you have one format and using another. Mapping programs sometimes want different format than what is provided by raw data from GPS. That would be my guess.

    2. Like I say, you really have to understand the format of the Lat Lon coming off your GPS, and how to parse it into the format wanted by your mapping software. There are many different formats. Look at my lesson on parsing NMEA sentences.

  14. Hi,

    Thank you for tutorials. I have a project where i need to transmit GPS coordinates wireless to a computer. i need to pass the data in API mode using an xbee s1. i am using xbee-arduino master library, but i am not able to pass the data.

    1. First thing is to just get the xbee working. Just assign a value to a varialbe and try and pass it back and forth with nothing else. Reduce it to just Xbee and passing data. Once that works, start adding more things

  15. Thanks for the great tutorials! I am learning a lot and having fun at the same time. The program seems to be working as it should. I am trying to also include the speed in the GPSData.txt file by adding the following line: mySensorData.println(GPS.speed); This isn’t working and I was wondering if you know of a way to parse and write the speed to the file. Thanks!

  16. I like this project, and nice tutorials. I want to start this project for school, but your link to the Virtuabotix SD Card Reader/Writer is currently unavailable. I also checked several different sites, and it seems out of stock everywhere. Now that I can’t find it, what do I use in place of this Virtuabotix Card Reader?

  17. I have followed your code and have a small problem, when I go into the serial monitor I get the third set as first line GPGLL…… which looks like your second line and then as second line I get GPRMC. From then on for every pair, I get first line GPGSA …. second line GPGSV …….

    Any help please?

  18. the Virtuabotix SD Card Reader/Writer for Arduino is out of stock and unavailable. Can you recommend a suitable alternative, please? Thank you.

  19. Hello Mr. Mcwhorter,

    I am part of a community college team putting together a High Altitude Balloon payload, which we will be launching in a few weeks.

    I have an Arduino Uno (though I will be switching over to a Mega soon for more memory and computing power), the Adafruit Ultimate GPS, a BMP180, and several other sensors.

    I have gone through your arduino/gps tutorials and found them extremely helpful. I have gotten the GPS logging data just fine to the Arduino, but i run into problems when I try to incorporate other sensors into the code. The gps data, and another sensor will log fine for about two loops, then the NMEA sentences become scrambled gibberish. it seems like the Arduino just cannot keep up.

    in one of your final tutorials, you mentioned a possibility of posting further videos covering the addition of the BMP180 to the sensor package.
    Did you ever figure out a way to combine code for other sensors into the gps datalogging code, and do you have the code from that?

    Thanks much,

  20. I have Inserted a 4GB SD card still IDE showing me the following warning

    Low memory available, stability problems may occur.”
    Why ?

    1. That error is the program size is not fitting into the available on-chip memory. It has nothing to do with the SD card. Only real option is make code smaller, or go to something like the arduino MEGA.

      1. Thanks for your quick reply and support.
        I have one more query.
        can we directly print latitude, longitude and altitude values using Serial.print ? So that we can see those values in IDE only?
        I tried my readGPS() fuction as follows:
        void readGPS(){ //This function will read and remember two NMEA sentences from GPS
        clearGPS(); //Serial port probably has old or corrupt data, so begin by clearing it all out
        while(!GPS.newNMEAreceived()) { //Keep reading characters in this loop until a good NMEA sentence is received
        c=GPS.read(); //read a character from the GPS
        }
        GPS.parse(GPS.lastNMEA()); //Once you get a good NMEA, parse it
        NMEA1=GPS.lastNMEA(); //Once parsed, save NMEA sentence into NMEA1
        while(!GPS.newNMEAreceived()) { //Go out and get the second NMEA sentence, should be different type than the first one read above.
        c=GPS.read();
        }
        GPS.parse(GPS.lastNMEA());
        NMEA2=GPS.lastNMEA();
        Serial.println(NMEA1);
        Serial.println(NMEA2);
        Serial.println(“”);
        Serial.print(GPS.latitude,4);
        Serial.printl(GPS.lat);
        Serial.print(“,”);
        Serial.print(GPS.longitude,4);
        Serial.print(GPS.lon);
        Serial.print(“,”);
        Serial.print(GPS.altitude);
        }
        But I am getting output on IDE as 0.0000,0.0000,0.00

  21. Excellent tutorials Paul! You’ve helped a complete beginner to Arduino and C get a GPS logger (Adalogger M0) working. Thank you!

    As an aside; I use a recent addition to the Adafruit GPS library commands: –

    There is a function that correctly gives the lat and lon in decimal degrees with the point in the right place.

    Serial.print(GPS.latitudeDegrees,5);
    Serial.print(GPS.longitudeDegrees,5);

    No need to append anything to these either.

    This should simplify the process of reviewing tabulated GPS positional data in Google Earth 🙂

  22. Sir, Iam dont know how to code for extracting only latitude and longitude ……This is the code that i have uploaded in aurduino.
    #include
    #include
    SoftwareSerial mySerial(3,2); port
    Adafruit_GPS GPS(&mySerial); //Create the GPS Object

    String NMEA1; //Variable for first NMEA sentence
    String NMEA2; //Variable for second NMEA sentence
    char c; //to read characters coming from the GPS

    void setup() {

    Serial.begin(115200); //Turn on serial monitor
    GPS.begin(9600); //Turn on GPS at 9600 baud
    GPS.sendCommand(“$PGCMD,33,0*6D”); //Turn off antenna update nuisance data
    GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Request RMC and GGA Sentences only
    GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate to 1 hz
    delay(1000);

    }

    void loop() {

    readGPS();

    if(GPS.fix==1) { //Only save data if we have a fix

    mySerial.print(GPS.latitude,4); //Write measured latitude to file
    mySerial.print(GPS.lat); //Which hemisphere N or S
    mySerial.print(“,”);
    mySerial.print(GPS.longitude,4); //Write measured longitude to file
    mySerial.print(GPS.lon); //Which Hemisphere E or W
    mySerial.print(“,”);
    mySerial.println(GPS.altitude);

    }

    }

    void readGPS() {

    clearGPS();
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    NMEA1=GPS.lastNMEA();

    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    NMEA2=GPS.lastNMEA();

    Serial.println(NMEA1);
    Serial.println(NMEA2);
    Serial.println(“”);

    }

    void clearGPS() { //Clear old and corrupt data from serial port
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }
    GPS.parse(GPS.lastNMEA());

    }

  23. thank you sir for helping me in making my project. My project is 80% over, Now it is in last phase where i need to send the received data through GSM modem.
    I tried it but iam getting a empty message in my phone.

  24. Sir, your tutorial was very helpful for my project. Thank you Sir.
    sir i tried to integrate it with GSM modem and it worked, but only once i received the message then after i didn’t get message please help me in solving this problem.
    thank you Sir

  25. Hi,

    I love your lessons and it is such a great way to follow you with your 3 windows.
    I have worked through all the 23 tutorials and it works. Now I have a problem.
    I am using every component like you did and the same code. The program runs great if the arduino is plugged into the computer via USB port.
    The moment I use a 9V battery it is only creating the files, but is not writting any data into them. I have read that maybe the 9V block is not strong enough, so I put 3 parallel and it is still not working.
    Do you have any advise?

  26. Hello sir,
    I am learning aurdino from your lessons. They are very good and helpful for learners like me. The only help Ineed is that complete list of components used in these sequence of lessons. I need it because I live in village and I got to city for only one day. So, if I will have the list of components lesson wise then I can purchase them. I hope you will help me.

  27. Hallo Sir
    Thank you very much for all your dedication. It is a great honor for me to follow your projects, they are really excellent. You show the people the topic, the wiring and especially the programming which is the most defying part for all.

    I also want to send you congratulations from Germany. We all think that you as your person make electronics interesting for all both “hobbyists” and “profis”. Thank you very much for that.

    I did all your projects with a lot of fun and every project works. I only had a problem with the GPS project. Every code worked fine only from this lesson not. I have searched a lot in the code an found at the end of your code that :

    GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
    while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c=GPS.read();
    }

    It was doubbled in the code.
    So the GPS module worked but send no data. After errasing that part of code which is double, the code works very fine.

    I hope you to see many more projects because you are really the best absoulutly. Your Filming with multiple cams, explaining the issue and realising it in an simple and most effective way is really amazing. Thank you from southern Germany.

  28. Hi Paul,

    First of all, thank you for all your educational videos which helped me a lot with programming and understanding the same GPS. Although, the programming went well we still have some difficulties. In one of the NMEA-sentences we received on our location we saw an altitude of -22,4 meters. This isn’t correct at all! It may be -2,24 meters though. Do you know what the problem might be and how to fix that?
    Furthermore the time given by the GPS is in GMT. How can I change this time to GMT +1?
    Lastly, we want to send the NMEA-sentences from our satellite to our groundstation using an antenna. We already have the materials and we tested the antenna with other sensors but we don’t know how to send these NMEA-sentences to our groundstation. Can you help me in the right direction?

    Kind regards,

    Bob

Leave a Reply

Your email address will not be published. Required fields are marked *