OK, it is time for us to take our projects up to that next level. We are going to build a GPS tracker from scratch. This is going to take several lessons to complete, but it will build on what you already know, and is really not going to be that difficult of a project. We will be using the most excellent Adafruit Ultimate GPS module. This is an excellent GPS. I like it because it is affordable, easy to use, and is one of the few that will work at extreme elevations, making it ideal for our Edge of Space/High Altitude Balloon work.
This unit is pretty easy to hook up, as you can see in the Table below:
Connecting the Adafruit Ultimate GPS Unit to Arduino |
|
GPS Pin | Arduino Pin |
Vin | 5V |
GND | GND |
RX | Pin 2 |
TX | Pin 3 |
Our goal in this lesson is to get the GPS connected, and get it reading NMEA sentences. NMEA is a data format used by GPS and mapping devices and software to keep track of position coordinates. There are lots of different NMEA sentences, but the two that contain the most useful information are the $GPRMC and $GPGGA sentences.
Our interest is in creating a location tracker for our High Altitude Balloon work, and the $GPRMC and $GPGGA sentences contain all the information and data we would need for that work. These sentences contain the lattitude, longitude, time, altitude, and velocity.
The GPS modules are pretty easy to work with. When you apply power to the GPS, it immediately starts spitting out NMEA sentences to its serial port. Our job on the arduino side is to simply read these data strings, and then parse them into useful data. The thing that is a challenge is that they constantly spit out data, whether you want it, or whether you are ready for it or not. In developing the software, we have to be mindful the the data is always spewing out of the GPS. Typically, we will have other components on our package, like temperature, pressure and inertial sensors. While our arduino is out making measurements on these other sensors, data continues to stream in from the GPS, likely overflowing our serial buffer. When we return to make a GPS measurement, it is very likely that the serial buffer will have old or corrupt GPS data in it. Hence, we must be mindful to deal with this issue in developing our software.
The video above takes you step-by-step through connecting and reading the NMEA sentences step-by-step. Then in the next lesson we will parse and log the data to create a portable GPS tracker. The code below is explained in the video. You need to watch the video to understand this code, and so you will be able to begin to work with this code to create a portable GPS tracker.
In order for this software to work, you need to download and install the Adafruit GPS Library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//Make sure to install the adafruit GPS library from https://github.com/adafruit/Adafruit-GPS-Library #include <Adafruit_GPS.h> //Load the GPS Library. Make sure you have installed the library form the adafruit site above #include <SoftwareSerial.h> //Load the Software Serial Library. This library in effect gives the arduino additional serial ports SoftwareSerial mySerial(3, 2); //Initialize SoftwareSerial, and tell it you will be connecting through pins 2 and 3 Adafruit_GPS GPS(&mySerial); //Create GPS object String NMEA1; //We will use this variable to hold our first NMEA sentence String NMEA2; //We will use this variable to hold our second NMEA sentence char c; //Used to read the characters spewing from the GPS module void setup() { Serial.begin(115200); //Turn on the Serial Monitor GPS.begin(9600); //Turn GPS on at baud rate of 9600 GPS.sendCommand("$PGCMD,33,0*6D"); // Turn Off GPS Antenna Update GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Tell GPS we want only $GPRMC and $GPGGA NMEA sentences GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate delay(1000); //Pause } void loop() // run over and over again { readGPS(); //This is a function we define below which reads two NMEA sentences from GPS } 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(""); } void clearGPS() { //Since between GPS reads, we still have data streaming in, we need to clear the old data by reading a few sentences, and discarding these while(!GPS.newNMEAreceived()) { c=GPS.read(); } GPS.parse(GPS.lastNMEA()); while(!GPS.newNMEAreceived()) { c=GPS.read(); } GPS.parse(GPS.lastNMEA()); } |