Components and supplies
Adafruit GPS shield
Arduino UNO
Tools and machines
Power Supply, Soldering Station
Solder Wire, Lead Free
Apps and platforms
Arduino IDE
Project description
Code
Arduino UNO
arduino
1// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver 2// 3// This code shows how to listen to the GPS module in an interrupt 4// which allows the program to have more 'freedom' - just parse 5// when a new NMEA sentence is available! Then access data when 6// desired. 7// 8// Tested and works great with the Adafruit Ultimate GPS module 9// using MTK33x9 chipset 10// ------> http://www.adafruit.com/products/746 11// Pick one up today at the Adafruit electronics shop 12// and help support open source hardware & software! -ada 13 14#include <Adafruit_GPS.h> 15#include <SoftwareSerial.h> 16 17// Connect the GPS Power pin to 5V 18// Connect the GPS Ground pin to ground 19// Connect the GPS TX (transmit) pin to Digital 8 20// Connect the GPS RX (receive) pin to Digital 7 21 22// you can change the pin numbers to match your wiring: 23SoftwareSerial mySerial(8, 7); 24Adafruit_GPS GPS(&mySerial); 25 26// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console 27// Set to 'true' if you want to debug and listen to the raw GPS sentences 28#define GPSECHO true 29 30void setup() 31{ 32 33 // connect at 115200 so we can read the GPS fast enough and echo without dropping chars 34 // also spit it out 35 Serial.begin(115200); 36 delay(5000); 37 Serial.println("Adafruit GPS library basic test!"); 38 39 // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800 40 GPS.begin(9600); 41 42 // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude 43 GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); 44 // uncomment this line to turn on only the "minimum recommended" data 45 //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); 46 // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since 47 // the parser doesn't care about other sentences at this time 48 49 // Set the update rate 50 GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate 51 // For the parsing code to work nicely and have time to sort thru the data, and 52 // print it out we don't suggest using anything higher than 1 Hz 53 54 // Request updates on antenna status, comment out to keep quiet 55 GPS.sendCommand(PGCMD_ANTENNA); 56 57 delay(1000); 58 // Ask for firmware version 59 mySerial.println(PMTK_Q_RELEASE); 60} 61 62uint32_t timer = millis(); 63void loop() // run over and over again 64{ 65 char c = GPS.read(); 66 // if you want to debug, this is a good time to do it! 67 if ((c) && (GPSECHO)) 68 Serial.write(c); 69 70 // if a sentence is received, we can check the checksum, parse it... 71 if (GPS.newNMEAreceived()) { 72 // a tricky thing here is if we print the NMEA sentence, or data 73 // we end up not listening and catching other sentences! 74 // so be very wary if using OUTPUT_ALLDATA and trytng to print out data 75 //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false 76 77 if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false 78 return; // we can fail to parse a sentence in which case we should just wait for another 79 } 80 81 // if millis() or timer wraps around, we'll just reset it 82 if (timer > millis()) timer = millis(); 83 84 // approximately every 2 seconds or so, print out the current stats 85 if (millis() - timer > 2000) { 86 timer = millis(); // reset the timer 87 88 Serial.print("\ 89Time: "); 90 if (GPS.hour < 10) { Serial.print('0'); } 91 Serial.print(GPS.hour, DEC); Serial.print(':'); 92 if (GPS.minute < 10) { Serial.print('0'); } 93 Serial.print(GPS.minute, DEC); Serial.print(':'); 94 if (GPS.seconds < 10) { Serial.print('0'); } 95 Serial.print(GPS.seconds, DEC); Serial.print('.'); 96 if (GPS.milliseconds < 10) { 97 Serial.print("00"); 98 } else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) { 99 Serial.print("0"); 100 } 101 Serial.println(GPS.milliseconds); 102 Serial.print("Date: "); 103 Serial.print(GPS.day, DEC); Serial.print('/'); 104 Serial.print(GPS.month, DEC); Serial.print("/20"); 105 Serial.println(GPS.year, DEC); 106 Serial.print("Fix: "); Serial.print((int)GPS.fix); 107 Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 108 if (GPS.fix) { 109 Serial.print("Location: "); 110 Serial.print(GPS.latitude, 4); Serial.print(GPS.lat); 111 Serial.print(", "); 112 Serial.print(GPS.longitude, 4); Serial.println(GPS.lon); 113 114 Serial.print("Speed (knots): "); Serial.println(GPS.speed); 115 Serial.print("Angle: "); Serial.println(GPS.angle); 116 Serial.print("Altitude: "); Serial.println(GPS.altitude); 117 Serial.print("Satellites: "); Serial.println((int)GPS.satellites); 118 } 119 } 120} 121
Downloadable files
Schematic of the GPS Adafruit shield.
This is to show the connected components.
Schematic of the GPS Adafruit shield.
Comments
Only logged in users can leave comments