GPS Shield on Mega

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

GPS Shield on Mega

Post by Brewington »

I am having difficulty getting the GPS Ultimate Shield working, so I need to see what I am missing:)

I am using an Arduino Mega 2560. The shield is mounted. There is no external antenna; I have a weight limit for the instrument.

A. The Direct comm test appears to work, although no data is present (lots of empty fields in the displayed Serial console).

B. I then moved to the SoftwareSerial test. I know that the Mega does not have TX/Rcv on pins 7&8. I am using pins 18&19:
SoftwareSerial mySerial(18, 19);

When it runs, it never gets a fix. I have tried it indoors and outdoors, letting it sit for 30-40 minutes without ever getting a fix. The red light on the shield flashes every second or so, apparently when it tries to get a fix.

Attached is the code - basically the SoftTest example program.

1) I don't know where to start to make this shield work. Perhaps there is some other issue related to the Mega? I understand that other people have used this on a Mega...


2) I read (I think in the tutorial) that the sensor will not work indoors. Won't the radio signal go indoors? Is there some other problem preventing it from working indoors? My application will be sending it up in essentially a small styrofoam box attached to a weather balloon to 100,000 feet. Is there some reason the GPS would not get a fix inside a foam box?

Thanks for any help/clues:)

Code:

Code: Select all

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 8
// Connect the GPS RX (receive) pin to Digital 7

// you can change the pin numbers to match your wiring:

SoftwareSerial mySerial(18, 19);    // Tx, Rcv
Adafruit_GPS GPS(&mySerial);

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO  true

void setup()
{
  
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  delay(5000);
  Serial.println("Adafruit GPS library basic parsing test!");
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  // Ask for firmware version
  mySerial.println(PMTK_Q_RELEASE);
 
}

uint32_t timer = millis();
void loop()                     // run over and over again
{

  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if ((c) && (GPSECHO))
    Serial.write(c);

  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) {
    timer = millis(); // reset the timer

    Serial.print("\nTime: ");
    if (GPS.hour < 10) { Serial.print('0'); }
    Serial.print(GPS.hour, DEC); Serial.print(':');
    if (GPS.minute < 10) { Serial.print('0'); }
    Serial.print(GPS.minute, DEC); Serial.print(':');
    if (GPS.seconds < 10) { Serial.print('0'); }
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    if (GPS.milliseconds < 10) {
      Serial.print("00");
    } else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
      Serial.print("0");
    }
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);

      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }
  
}

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

I tried to run this on a regular UNO, but it took too much memory.

Maybe I need to jumper the pins 18/19 over to 7 and 8?



OK, I am having more problems trying to use the GPS Ultimate shield. I want to log data (GPS as well as other sensors) to the SD card.

I may be confused as to which library should be used for accessing the sd card. So far I am seeing 6? possible candidates:

1) Arduino IDE 1.8.15 installed a library SD, version 1.2.4. This works with the SD card on an ethernet shield on a Mega 2560, also works with a datalogger shield SD card on the Mega. It does not work with the GPS shield. I have tried various chipSelect pins (4, 10, 53) based on different notes found on the internet. The SD.begin statement always fails. This begin statement only accepts 1 integer; there is no signature for 4 integers.

2) I also have Adafruit_GPS v. 1.5.4 installed.

Also available in the Library Manager:
3) SD_card_logger by Natan Lisowski v. 1.0.0
4) Sd_Fat by Bill Greiman v. 2.1.0
5) SdFat - Adafruit Fork by Adafruit v. 1.2.4

6) Found this note

Code: Select all

 https://learn.adafruit.com/adafruit-shield-compatibility/ultimate-gps-shield#arduino-mega-all-variants-642938-6
which says to use "Adafruit SD library". This link goes to github for Adafruit/SD, which has a library named SD. 

However, the readme for this library says

this library fork is being archived - the Arduino SD lirbary has all these capabilities in it now

please use the SdFat Library instead for any advanced SD card needs! (its much better) 

---------------------------


** SD - a slightly more friendly wrapper for sdfatlib **

This library aims to expose a subset of SD card functionality in the
form of a higher level "wrapper" object.

License: GNU General Public License V3
         (Because sdfatlib is licensed with this.)

(C) Copyright 2010 SparkFun Electronics

Now better than ever with optimization, multiple file support, directory handling, etc - ladyada!
It sounds like the SD library installed by the IDE is the newer one, or perhaps the library in (4) above?

The arduino-mega-all-variants-642938-6 article also recommends using pin 10,11,12,13 in the begin statement and to edit the utility/Sd2Card.h file to set
#define MEGA_SOFT_SPI 1

Would that still be needed if I use the correct library?

Here is my code, which is a simplified form of the Adafruit_GPS/shield_sdlog.ino example.

Code: Select all

#include <SPI.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <avr/sleep.h>

// Set the pins used
#define chipSelect 53     // tried 4, 10, 53

File logfile;

void setup() {
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println(F("\r\nUltimate GPSlogger Shield Disk test"));

  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(chipSelect, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println(F("sd Card init failed!"));
    while(1);
  }
  char filename[15];
  strcpy(filename, "GPSLOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    Serial.print("Checking file: "); 
    if (! SD.exists(filename)) 
      {
      Serial.println("Use this one");
      break;
      }
    else
      {
      Serial.println("Already exists");
      }
  }

  logfile = SD.open(filename, FILE_WRITE);
  if( ! logfile ) {
    Serial.print(F("Couldnt create "));
    Serial.println(filename);
    while(1);
  }

  Serial.println(F("Ready!"));
}

void loop() 
{
    char *myString = "Test log string";
    uint8_t stringsize = strlen(myString);
    if (stringsize != logfile.write((uint8_t *)myString, stringsize))    //write the string to the SD file
        {
        Serial.println("  >> logfile.write failed");
        }
    logfile.flush();
}

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

I got the SD disk to work finally. I had missed the part about updating the SD library manually. The Mega now works with the SD.begin(10,11,12,13) command.

Now back to getting the GPS itself to run:)

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

Try with hardware serial and the approach covered here:
https://learn.adafruit.com/adafruit-ult ... al-connect

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

Well, it almost worked. I ran it 50 times, waiting various times from 10 minutes to 1.5 hours (twice) for it to get a fix. I rebooted between each attempt, sometimes by closing/opening the Serial Console window, sometimes using the Reset switch on the GPS shield, sometimes using the Reset switch on the Arduino, sometimes pulling the USB cable, reinserting, and re-downloading the sketch. Nothing seemed to help.

It worked once, about the 10th time, after about a 30 minute wait. All other times it never got a fix. The one time, once it got a fix it maintained the fix; it did not lose and regain the fix.

I have the shield switch set to software serial, and jumpered pins 18 and 19 to 7 and 8 using jumper cables between the shield stacking header pins. I had tried this before, but didn't realize the Tx/Rcv pins needed to cross over. Now I have Tx connected to Rcv and vice versa. I am using Serial1 for the hardware communication rather than mySerial as a Software serial. I am basically running the Hardware Parse example sketch.

This device needs to reliably get a fix in 5-10 minutes. It will be attached to a weather balloon; I get about 10 minutes notice to turn it on before the balloon launches. I do not have the opportunity to sat "Hey guys, can we hold up for a couple of hours while I get a GPS fix?". It would be awkward to have 8 people work on this for 6 months and have it fail because the GPS doesn't work. Help!

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

How set are you on using a Mega and the shield? For something going up on a weather balloon, could maybe consider the Feather form factor:
https://www.adafruit.com/product/3133
That's the GPS FeatherWing, which would need to be pair with a Feather main board. The M4 Express is a good general purpose option:
https://www.adafruit.com/product/3857

Are you at least seeing the NMEA sentences being sent out by the GPS?

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

I need the Mega for the size of the sketch. Even after replacing strings with F() and looking for other memory savings it still doesn't fit on a Uno. I also need the extra analog ports.

? I don't see any sentences until it gets a fix. Are you saying this section should be showing me stuff?

Code: Select all

  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if (GPSECHO)
    if (c) Serial.print(c);
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trying to print out data
    Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return; // we can fail to parse a sentence in which case we should just wait for another
  }

Hmmm, I have GPSECHO set to false. I will retry it with echo enabled.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

Not surprising it won't fit on an Uno.

But just to be clear, the Feather M4 mentioned above has 2x the flash and 24x the RAM as the Mega:

Mega = 256kB flash / 8 kB RAM
Feather M4 = 512kB flash / 192 kB RAM

The Feather M4 also runs at 120MHz vs. the Mega's 16MHz.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

Oh - I haven't looked at that, I thought the feathers were smaller devices like the nano or teensy? Tiny? I will have to check it out for future projects.

Unfortunately I don't have time to switch platforms now. I have about a week to make this work.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

Feather is just a form factor. There are Feather boards in a wide range of processor options. Tons of info here on the whole deal, good reading for future ref:
https://learn.adafruit.com/adafruit-feather

OK, let's get back your GPS shield. Are you still having issues getting a fix?

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

Yes, I haven't thought of many other things to try.

The shield is on the Mega, the switch is set to Software, and I'm using hardware Serial1 to talk to the GPS. I have jumpered pins 18/19 (Serial1) to pins 7/8, where RX is connected to TX and vice versa. I am unaware of any other pins being used by the GPS (I2C, SPI, other pins).

I need to uncomment the GPS_ECHO to see if I am seeing messages from the GPS chip to something? I'm not clear on what the GPS would be writing to.

I have another shield on the Mega as well, a Qwiic shield. This requires jumpering the SDA/SCLK signals over to A4 and A5; I don't expect that to interfere with the GPS, but who knows. I will remove that shield and see if anything changes.

I will also transfer the shield to a Uno and try the example HardwareParse sketch on the Uno. I was unable to run my main program on the Uno (not enough memory) but I expect the example sketch should fit.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

Using the UNO might help to just get the basic working. You should be able to get a fix with just power though. No need to be running any sketch. Can just watch for the change in the blink rate of the red LED.

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

Well, this is definitely a mess:) I hope someone can make sense of this.

I put the GPS on a Uno, no other shields or jumpers. The code is basically the example Hardware Parsing example, and compiles while barely avoiding the message about too little memory.

The debug logic seemed strange. It just prints the first character of the message, which doesn't tell me much. Plus, the characters are garbled:

Code: Select all

:                                       << character
Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0
u                                       << character
Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0
                                       << garbled character
Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0
I
Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0
	
Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0

Time: 00:00:00.000
Date (dmy): 0/0/200
Fix: 0 quality: 0 3d: 0
&
I changed the logic as shown

Code: Select all

  char c = 0;
  /****** Orig Code
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if (GPSECHO)
    if (c) Serial.print(c);
    *****/
  // Replaced Debug code
  if (GPSECHO)
      {
      Serial.print("Dbug msg: {");
      while (c = GPS.read())
          {
          if (c) Serial.print(c);  
          }
      Serial.println("}");
      }
Maybe my "improved" code affects which lastNMEA message is processed?

So, my issues:
a) messages are getting garbled/mixed together, which causes the parsing to fail.
I had the antenna status message enabled, which caused this symptom to occur much more often. I have turned that off now.
Example below, where the newNMEA message is a mix of two messages (partial second message with first one appended) which fails to parse:

Dbug msg: {$GPGGA,211249.086,,,,,0,00,,,M,,M,,*7B
$GPRMC,211249.086,V,,,,}
newNMEAreceived
$GPRMC,211247.086,V,,,,$GPGGA,211249.086,,,,,0,00,,,M,,M,,*7B
Failed to parse {$GPRMC,211247.086,V,,,,$GPGGA,211249.086,,,,,0,00,,,M,,M,,*7B
}

b) Some values never seem to parse correctly
- Can I get a fix with zero satellites? Number of satellites is always 0.
- quality is always 0. If I get a fix, shouldn't the quality be 1?
- Angle and speed seem to be random. Speed is generally a small number so maybe that is
noise. Angle changes significantly, although the unit is sitting still.
- Altitude is always 0, even when I can see the altitude in the message (the whole requirement of my application!)


Date (dmy): 12/10/2021
Fix: 1 quality: 0 3d: 0
Location: 3257.5852N, 11139.2128W <<<< appears to have old data in the parsed values
latitude fixed: 32.96
longitude fixed: -111.65
latitude Degrees: 32.96
longitude Degrees: -111.65
Speed (knots): 0.55
Angle: 272.31
Altitude: 0.00
Satellites: 0

When I turn off the debug code I never get a newNMEAreceived to occur. Do I need to do some amount of reading from the GPS for it to recognize that a new message has been received?
Last edited by adafruit_support_carter on Tue Oct 12, 2021 6:27 pm, edited 1 time in total.
Reason: fix [code] tag

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: GPS Shield on Mega

Post by adafruit_support_carter »

It looks like you removed this line from the loop:

Code: Select all

  char c = GPS.read();
That is needed. It should be called at the top of the loop to read from the GPS and update the current state. Not like this:

Code: Select all

      while (c = GPS.read())
          {
          if (c) Serial.print(c); 
          }

User avatar
Brewington
 
Posts: 54
Joined: Sat Sep 27, 2014 7:45 pm

Re: GPS Shield on Mega

Post by Brewington »

Ah, I did not realize that doing the single char read caused the system to do stuff other than return the latest character.

I have played with this for a number of hours again after putting the code back to normal (I think). It really seems that the problem is that the messages from lastNMEA() are generally garbled. Some examples of the message from GPS.lastNMEA() which fail to parse:

$GPRMC,215255.100,V,,,,$GPGGA,215257.100,,,,,0,00,,,M,,M,,*7F
$GPRMC,21531$GPGGA,215321.306,,,,,0,00,,,M,,M,,*7B
$GPGGA,215407.309,,,,,0,0$GPGGA,215408.309,,,,,0,00,,,M,,M,,*78

These occurred while searching for a fix. Even when a fix is established most of the messages are like this (but with data in them).

Generally a second message comes in and overwrites the first message in the middle of the buffer. As a result, the message is almost never parsed correctly and the system cannot get a fix. On rare occasions it actually gets a message it can parse; after a number of these it will get a fix. However, the messages continue to be garbled so the data is rarely updated.

I am using the Mega with the switch in the Soft Serial position. I have jumpered pins 18/19 to pins 7/8, and am using Serial1 (pins 18/19) for communication.

Here is my initialization code which gets called from setup:

Code: Select all

#include <Adafruit_GPS.h>
Adafruit_GPS GPS(&Serial1);    // This is pins 18/19

void CGPSSensor::InitSensor()
{
    // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
    if (!GPS.begin(9600))
        {
        FlashStatusError(GPS_NOT_FOUND, "GPS failed to begin");  
        }
  
    // turn on RMC (recommended minimum) and GGA (fix data) including altitude
    GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
    // Set the update rate
    GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
    delay(1000);
    
    // Ask for firmware version
    //gpsSerial.println(PMTK_Q_RELEASE);         <<< I assume I don't need this?

    // These get filled in when
    //   a fix is established, so messages update them
    GPS_fix = false;
    Value = 0.0;          
    Latitude = 0.0;
    Longitude = 0.0;
}
And here is the code that reads the sensor:

Code: Select all

bool CGPSSensor::ReadSensor()
{
    bool readOK = true;
    
    bool done = false;
    int tries = 0;
#define MAX_TRIES 10000    // about 20 msec
    // checking timing for getting a fix
    long fixTimer = millis();
    double fixMinutes = 0.0;    // num minutes to find (or lose) fix
    
    while (!done)
        {
        tries++;
        // read data from the GPS in the 'main loop'
        char c = 0;       // needed for GPS code to change state
        c = GPS.read();
          
        // if a sentence is received, we can check the checksum, parse it...
        if (GPS.newNMEAreceived()) 
            {
            // a tricky thing here is if we print the NMEA sentence, or data
            // we end up not listening and catching other sentences!
            // so be very wary if using OUTPUT_ALLDATA and trying to print out data
            if (!GPS.parse(GPS.lastNMEA()))
                {  // this also sets the newNMEAreceived() flag to false
                Serial.print("  Failed to parse {"); Serial.print(GPS.lastNMEA());Serial.println("}");
                }
            else
                { // good parse of message
                GPS_fix = (GPS.fix > 0);
                if (GPS_fix ) 
                    {   // save parsed data from message
                    Latitude= GPS.latitudeDegrees;    // degrees
                    Longitude= GPS.longitudeDegrees;
                    Value = GPS.altitude;           // meters
                    done = true;
                    }
                } // good parse
            } // new msg received
          if (!done && !GPS_fix && (tries > MAX_TRIES))
              {
              done = true;     // stop the loop if no fix has been achieved in 10000 tries
              }
          } // !done
    
    return (readOK);
}
It seems that I am very close, but the communications just don't work right.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”