Logging GPS Data to an SD Card with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello from the forcetronics YouTube channel welcome to logging GPS data to an SD card with Arduino and that's exactly what we're going to talk about in this video if you like what you see in this video please subscribe to my YouTube channel let's get started let's first talk about the hardware we're going to use for this tutorial so for the GPS module I'm going to be using the Neo 7M from ublock which is a GPS chip or I should say system on a chip now ublock makes a couple different families of these chips the neo6 was the first I'm using the neo7 they have the neo8 this tutorial should work the code I'm going to show should work for all these Generations uh but once again I'm only going to test it on the neo7 and I'm using this development board that I got from AliExpress for the neo7 as long as you have a neo7 on a board that has a u connection then this tutorial should work for you for the SD card I got this holder from uh AliExpress as well they're very low cost I think I got it for like 50s you know you can also use you know I know Arduino has some Shields with with SD card holders on it an important thing to note if you're not already aware is the brains of the SD card is actually on the SD card so the holder is nothing but a holder with a wiring interface the SD card actually has a microcontroller and a and memory of course and so when you you can't talk to the holder you only talk to the card something to point out and then for this tutorial both these devices both the neo7 as well as the SD card are 3.3 volt for power as well as for logic or communication here's something I grabbed from the data sheet for the Neo 7M that kind of just show some of its features notice they have a they have you know more than one version they have the 7n and they have the 7M so actually you can see it connects to more than just GPS it actually connects to glas but uh GPS is what we're going to be using here you can see the voltage range up to 3.6 what's nice too is it actually has multiple communication interfaces now most development Boards out there don't actually use all of these but here we're going to be using the uar connection and then you can see the the end version actually has more featur than the M version but once again I'm going to be using the M version here's a little bit on my setup or I guess a partial schematic so the SD card is going to run off of spy communication and all these pins will be labeled uh if you're using a shield it should directly go to the Spy pins on that Arduino board now here in this picture I'm going to I'm using the zero now the zero actually has a icsp header what's different than with the zero than some of the the AVR boards is you actually have spy at this header as well as at Pin 13 12 and 11 for the zero they just have the Spy communication at this header so that's what I have it connected to once again if you're using an AVR I have the pins right up here now for the neo7 it uses uart but guess what it just constantly spits out data you don't actually have to communicate with it so I'm just connecting the transmit pin to the receive pin of my Hardware serial Port now important thing to note the Arduino 0 has multiple serial ports the programming Port is a Serial Port as well as digital pins 0o and one so I can actually program it without worrying about having this connected to it now if you're using an an AVR Arduino you want to make sure you unplug this pin when you do your programming or you'll get an error when you try to upload the sketch and I'll just mention as a reminder if you have a 5 Vol Arduino you need to use level shifters cuz these are you know the S SPI interface on the SD micro card is 3.3 Volts for the GPS module doesn't matter because you're just really using the the transmit you're not actually communicating to it so here's a picture of my setup just for a reference you know here's my zero board and I will mention I tested this code with a zero as well as a pro mini which is a 3.3 volt AVR based Arduino so I did test it with both I do have to change a couple parts of the code to use it for AVR but I'll Point those out when I show the code but anyway this is the connection you can see I just have one communication pin for the GPS I have my pins from the SD card here's the micro SD card going to my spy header and then I'm using 3.3 Volts for powering everything okay and before we get to the code I think it's important to understand that all GPS chips I shouldn't say all most GPS chips abide by this Nema standard or national Marine Electronics Association what they did is they said you know what we have all these GPS modules coming out let's have them all speak the same language so they made this great standard and most GPS modules that I've seen follow it and how this standard works is basically the module will print out or the module will communicate out the these sentences and the sentence will begin with a dollar sign GP for GPS I guess and then a heading for the sentence so this one I'm going to use is RMC and H heading means it's going to communicate different data so for RMC it's going to communicate time position velocity date things like that and you could actually see an example so it'll throw out this sentence on the serial line you look for this header and then you know the next data that's going to come and you can see here's a description of each part of the data and so the idea is you read this in and you parse through it now I'll mention that there's a lot of different sentences and you can actually see here's a picture of a serial Port where they just print out all the different sentences this one actually doesn't print out a lot of data compared to some of these others but I will mention for for this example I'm only going to be reading this sentence in I'm not going to be reading all these other sentences and that's that's what my code is set up to do if you want more information on these different sentences you can go to this website right here okay I just wanted to give you a background on sort of a standard interface that you can expect in GPS modules now that we have that we have the hardware set up let's take a look at the code okay here we are at the code and what I did is I created a simple library for the GPS communication and you can see since I have quotation marks that means I have it locally in the folder so here's we we'll we'll take a look at the library but so I'm calling my library I call spy communication library because I need it for the SD card and then Arduino already has a nice SD card library for getting data from the SD card or putting it on the SD card so here's some of my setup variables I basically declare a chip select pin and maybe I didn't mention that when I showed the the picture you know you have your spy connections but you also have a chip select pin and I set this to digital pin 10 I then when I log my data to the SD card I'm actually going to log it in a CSV file which you can open as a spreadsheet in any spreadsheet program like the popular Microsoft Excel and so what I want to do is I want to create labels for my spreadsheet for the data and so CSV stands basically for comma separated I don't know if it's variables or what but anyway comma separated something and so you can see I have a comma between those and what the comma does is when you open it up with a spreadsheet they know to put these in different cells so here's all my headings for the data I'm going to read the next string is for the name of the file so I just call it w data and the W is actually for win data because I'm using this for recording win so w data is the name and I will remind you I shouldn't say remind you for SD cards for the file formats they follow this 8 .3 rule so it means you can't have more than eight digits in the name and you can't have more than three digits for the file type here I declare the library I probably should have put some comments there but this is my library I'm calling I call it neo7 m and I I name the object Neo then here is my setup code so first what I do is I check for the SD card and if it's not there I just return if it is there what I do next is I check to see if the name of the file already exists and I'll show this when I show my an example of some of the files so on an SD card you can have multiple files well if I don't want to keep logging to the same file what I do is I say check to see if W data. CSV exists and that's what I do by this sd. exists if it does I up my count variable and then I add that to the string so now I have W dat 1 and I check if that exists and I keep looping until I find a new version that doesn't exist and then I create that file once I do that I write my headings so basically I create this other file object so I have my SD object and I have my file object I then open the file name the new file name it might be SD it might be excuse me W dat it might be W 1 it might be W 20 I then write my labels to the file I then close it I then print oh I should say I then close it if there was an error I just basically print a message to the serial serial monitor here's my main Loop I'll delay for 2 seconds and then what I'm going to do is grab the GPS data and write it to the SD card so here is my main function for getting the GPS data so Neo doget Nema data and what I do is I feed in the serial object now I need to point this out I last used this with the pro mini so I I feed in the hardware serial Port cereal if when I was using the zero though I feed in serial one because that's the serial Port that corresponds to d0 and D1 when I post this code to GitHub I'm going to have it configured for the Arduino zero so I'm just going to warn you this is something you'll need to change to work with with different arduinos so once I do that I checked basically this is going to get read the data from the GPS module and I'll show you the code in the library once it does that it stores it in Array that's in the library if it fails I just print and let you know and then if it doesn't I go here and we do a similar thing that we did in the setup code we create a data file or a file object we open up the file name and then we basically do a for Loop and I'm calling the array from the library ne. Nema and then I read through the array now I actually don't print out all the the parameters that were read from the uh from the sentence I think it reads I think it gets 15 pieces of data one of them is actually a you know a a CRC check but I I just I'm just interested in the first nine out to the date so that's what I that's what I do that's what I print out and the reason I have an if statement here is I print these out once again comma separable comma separated data for the CSV file that's why I have I add the comma and then for the last one I print line to go to the next uh Row in the spreadsheet I then close the file I then open up a Serial port and you can add you can leave this in if you want but this just is going to print out the raw data to the Ser Port so you can see it once again using that same array from the library I then end the cial notice that I use serial end a lot the reason is is because the library itself is using art and what I want to don't want to do is mix up different serial Communications because they use interrupts and it can make funky things happen so that's why you see me use serial begin and serial end a lot and then if the file failed to open I just print it out to you the user so you know so that's the main sketch now let's take a look at the library and I'm going to I'm going to post this to GitHub and I'll have a link in the description of the video so here's theh file I just give a brief overview of the library I kind of share some of the Nema characteristics CU once again all this Library does is read in the RMC sentence in Nema it doesn't read in every type of sentence I also want to mention that I leveraged some of this code in this library from a tutorial by uh David W who has a great YouTube channel he did a tutorial related to the Neo 6m so I did Leverage a little bit that of that code from his video and turned it into a library uh here's a link to his video if you want to check it out okay I'm not going to go through theh file much just to show you that here's that main function I told you about and we call in the hardware serial Port here's the the array and then I just have two private functions that basically what when when you get the latitude and longitude from the GPS module it's not in a very readable form so these functions just convert it into a better readable form so here's the CPP file and what I'm doing here oh here's another thing I want to point out this line of code needs to be added for the zero and it's a little confusing because it says AVR if you have an AVR Arduino it automatically pulls in this Library if you have a zero though it does it and we need this library for reading in the GPS data here's my Constructor my Destructor and here's the main function so this takes in this serial Port it starts the serial it basically looks for data once it there's data once again this is going to be from the GPS module it reads it then we search through that that buffer to find the sentence heading the RMC On once we do we can then parse through and that's all I'm doing here is parsing through that data and then what I do is I convert the latitude and longitude data into an easier read form this is the Neema array that we call in the main sketch you know if it works I set this to true and return true and there we go and then this is just I'm not going to go through these but these are the functions for converting latitude and longitude to an easier to read form okay that's the code I will post it on GitHub and also I'll have the main sketch on my blog here I wanted to show you some of the data in in a spreadsheet and so we can see the time the time is in UTC so when I when I made these measurements it was 3:00 a.m. and 55 minutes past 3 3:00 a.m. and and 6 seconds the a means active here's the latitude here's the hemisphere I need to mention about the longitude I went in here to the spreadsheet and change longitude just cuz I didn't want my exact coordinates of my home on here so I just I just pasted in this 100 that was not the real reading then here's the hemisphere again here's the speed when I did this I wasn't moving so that's why we're seeing no speed I'm not sure why this came up as a question mark this may be because it was too small of a number and same thing for the tracking angle the tracking angle is more of a measurement for when you're moving and so I think this is blank because you get get a long trail of of of zeros and then here is the date so basically I did this reading on February 8th 2017 so that's what it's going to print out to in the CSV so it's a nice way to log you know I did this because I want to have a a device that moves and it's going to be making sensor measurements and I want to record where the time and place that those sensor measurements were made and you can't beat a GPS mod module for that now I will warn you if you start if you're doing this indoors the GPS may not connect to all the satellites it needs and sometimes the GPS takes time to connect a satellite so if you start logging right away sometimes some of these fields will be blank what happens is the if the GPS can get a single satellite right away it'll give you time data and date data but it won't have the position and speed data yet so sometimes if you start logging right away you'll see the time data show up before you'll see the the position data show up and just to show this here's my folder with my SD card and you can see basically it makes you know W data.csv and then it'll keep making more as you log more and more data okay that's it for logging GPS data to an SD card with Arduino if you have any comments or insights to add please use the comment section on on on below this video and if you're ever bored please check out the forcetronics decom store in the next couple months I'm going to be adding more and more products so so feel free to check that out thank you for watching
Info
Channel: ForceTronics
Views: 23,810
Rating: undefined out of 5
Keywords: Arduino, Zero, AVR, Pro Mini, GPS, SD, card, datalog, logging, datalogging, SPI, UART, Serial
Id: 9-v5hNuHFq4
Channel Id: undefined
Length: 18min 13sec (1093 seconds)
Published: Thu Feb 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.