1 // Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
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
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
14 #include <Adafruit_GPS.h>
15 #include <SoftwareSerial.h>
17 // If you're using a GPS module:
18 // Connect the GPS Power pin to 5V
19 // Connect the GPS Ground pin to ground
20 // If using software serial (sketch example default):
21 // Connect the GPS TX (transmit) pin to Digital 3
22 // Connect the GPS RX (receive) pin to Digital 2
23 // If using hardware serial (e.g. Arduino Mega):
24 // Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
25 // Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3
27 // If you're using the Adafruit GPS shield, change
28 // SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
29 // and make sure the switch is set to SoftSerial
31 // If using software serial, keep these lines enabled
32 // (you can change the pin numbers to match your wiring):
33 SoftwareSerial mySerial(3, 2);
35 Adafruit_GPS GPS(&mySerial);
36 // If using hardware serial (e.g. Arduino Mega), comment
37 // out the above six lines and enable this line instead:
38 //Adafruit_GPS GPS(&Serial1);
41 // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
42 // Set to 'true' if you want to debug and listen to the raw GPS sentences.
45 // this keeps track of whether we're using the interrupt
47 boolean usingInterrupt = false;
48 void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
53 // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
56 Serial.println("Adafruit GPS library basic test!");
58 // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
61 // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
62 GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
63 // uncomment this line to turn on only the "minimum recommended" data
64 //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
65 // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
66 // the parser doesn't care about other sentences at this time
68 // Set the update rate
69 GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
70 // For the parsing code to work nicely and have time to sort thru the data, and
71 // print it out we don't suggest using anything higher than 1 Hz
73 // Request updates on antenna status, comment out to keep quiet
74 GPS.sendCommand(PGCMD_ANTENNA);
76 // the nice thing about this code is you can have a timer0 interrupt go off
77 // every 1 millisecond, and read data from the GPS for you. that makes the
78 // loop code a heck of a lot easier!
82 // Ask for firmware version
83 mySerial.println(PMTK_Q_RELEASE);
87 // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
88 SIGNAL(TIMER0_COMPA_vect) {
90 // if you want to debug, this is a good time to do it!
94 // writing direct to UDR0 is much much faster than Serial.print
95 // but only one character can be written at a time.
99 void useInterrupt(boolean v) {
101 // Timer0 is already used for millis() - we'll just interrupt somewhere
102 // in the middle and call the "Compare A" function above
104 TIMSK0 |= _BV(OCIE0A);
105 usingInterrupt = true;
107 // do not call the interrupt function COMPA anymore
108 TIMSK0 &= ~_BV(OCIE0A);
109 usingInterrupt = false;
113 uint32_t timer = millis();
114 void loop() // run over and over again
116 // in case you are not using the interrupt above, you'll
117 // need to 'hand query' the GPS, not suggested :(
118 if (! usingInterrupt) {
119 // read data from the GPS in the 'main loop'
121 // if you want to debug, this is a good time to do it!
123 if (c) Serial.print(c);
126 // if a sentence is received, we can check the checksum, parse it...
127 if (GPS.newNMEAreceived()) {
128 // a tricky thing here is if we print the NMEA sentence, or data
129 // we end up not listening and catching other sentences!
130 // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
131 //Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
133 if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
134 return; // we can fail to parse a sentence in which case we should just wait for another
137 // if millis() or timer wraps around, we'll just reset it
138 if (timer > millis()) timer = millis();
140 // approximately every 2 seconds or so, print out the current stats
141 if (millis() - timer > 2000) {
142 timer = millis(); // reset the timer
144 Serial.print("\nTime: ");
145 Serial.print(GPS.hour, DEC); Serial.print(':');
146 Serial.print(GPS.minute, DEC); Serial.print(':');
147 Serial.print(GPS.seconds, DEC); Serial.print('.');
148 Serial.println(GPS.milliseconds);
149 Serial.print("Date: ");
150 Serial.print(GPS.day, DEC); Serial.print('/');
151 Serial.print(GPS.month, DEC); Serial.print("/20");
152 Serial.println(GPS.year, DEC);
153 Serial.print("Fix: "); Serial.print((int)GPS.fix);
154 Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
156 Serial.print("Location: ");
157 Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
159 Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
161 Serial.print("Speed (knots): "); Serial.println(GPS.speed);
162 Serial.print("Angle: "); Serial.println(GPS.angle);
163 Serial.print("Altitude: "); Serial.println(GPS.altitude);
164 Serial.print("Satellites: "); Serial.println((int)GPS.satellites);