The GPS example from Tiny Library not work on MEGA2560

Hi,
the same sketch upload to Arduino UNO got output data even not full (why just got CHARS? )? and got nothing from a MEGA2560, why?
Thanks
Adam

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;
SoftwareSerial ss(6, 7);

void setup()
{
  Serial.begin(115200);
  ss.begin(4800);
  
  Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    Serial.print("LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" LON=");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print(" SAT=");
    Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
    Serial.print(" PREC=");
    Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
  }
  
  gps.stats(&chars, &sentences, &failed);
  Serial.print(" CHARS=");
  Serial.print(chars);
  Serial.print(" SENTENCES=");
  Serial.print(sentences);
  Serial.print(" CSUM ERR=");
  Serial.println(failed);
  if (chars == 0)
    Serial.println("** No characters received from GPS: check wiring **");
}

Change ss.begin to 9600

1 Like

Thanks.
changed not work.

You have posted enough times to know by now that you MUST STATE which GPS module you have. If the GPS works on the Uno but not on the Mega, you have made a wiring mistake.

On the Mega, use one of the other hardware serial ports, not software serial.

1 Like

Thanks.
The GPS: GY-NEO6MV2 and the link below.
The reason posted here is because of I have tried all MEGA2560 TX1/2/3.
All no reading, that's why I was thing maybe something special in code?

Wiring error.

Post the Mega code showing your use of hardware serial, and explain how you wired the GPS unit to the Mega, including power and ground.

1 Like

thanks.
sm_UNO
sm_MEGA2560
there are just 4 wires, and did shift the Tx and Rx for testing.

EXPLAIN HOW YOU WIRED THE GPS TO THE MEGA, and POST THE CODE.

1 Like
  1. same code as #1; 4800 changed to: 9600
  2. GPS Vcc to 5V; GND to GND; Tx to 6; Rx to 7;
  3. do changes when testing: pin6 / pin7 interchange。
    MEGA2560 hardware serial code:
 //Connect with pin 18 and 19
#include <TinyGPS.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat,lon;
TinyGPS gps; // create gps object

void setup(){
Serial.begin(115200); // connect serial
Serial.println("The GPS Received Signal:");
Serial1.begin(9600); // connect gps sensor

}
 
void loop(){

    while(Serial1.available()){ // check for gps data
    if(gps.encode(Serial1.read()))// encode gps data
    
    { 
        Serial.println("loop");
        
    gps.f_get_position(&lat,&lon); // get latitude and longitude

    Serial.print("Position: ");
    
    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat,6);
    
    Serial.print(",");
    
    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon,6); 
    
   }
  }
}

relative to serial3/2/1, changed GPS Tx to: 15/17/19 and Rx to: 14/16/18

Hello shanren.
May I ask where you think the problem lies?

Serial1.begin(9600); // connect gps sensor

Serial1 is on pins 18 and 19

Hello HillmanImp.
May I guess that the MEGA2560's multi-serial ports is just a gimmick?
I've used an empty sketch tested MEGA+GPS, got outputs on pin0/1 mean both devices all good. but non of the serial1.2.3 works.

That will come as a major surprise to all the folks using those 3 hardware serial ports on the Mega. Occam's Razor suggests there's a different explanation.

Wrong guess.

Lets make a test:

// multi-serial test

void setup() {
  // 9600 is the baud rate to use.  The baud rate determines how
  // fast the communication goes.
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
}

void loop() {
  Serial.println("test Serial!");
  Serial1.println("test Serial1!");
  Serial2.println("test Serial2!");
  Serial3.println("test Serial3!");

  delay(100);
}

That's the result I'd expect. What did you expect?

So the first serial port is echoed to the monitor but not the other three. What can that mean?

1 Like

well, see what he said:
https://rambutan.readthedocs.io/projects/librambutan/en/latest/lang/api/serial.html

There's nothing there at odds with the results you posted. Serial1, 2, 3 don't output to (nor input from) the Serial Monitor.

Thanks.
So now I got output of raw data from Serial1, and other call failed.
in the sketch below, the paragraph p1 does output, the paragraph p2 doesn't, why?

//Connect with pin 18 and 19
#include <TinyGPS.h>

#define GPSSerial Serial1

//long   lat,lon; // create variable for latitude and longitude object
float lat,lon;
TinyGPS gps; // create gps object

void setup(){
Serial.begin(57600); // connect serial
Serial.println("The GPS Received Signal:");
Serial1.begin(9600); // connect gps sensor

}
 
void loop(){

/*
   if (Serial.available()) {  //////// p1
    char c = Serial.read();
    GPSSerial.write(c);
  }
  if (GPSSerial.available()) {
    char c = GPSSerial.read();
    Serial.write(c);
  }
*/

  
    while(Serial1.available()){ // check for gps data   p2
    if(gps.encode(Serial1.read()))// encode gps data
    { 
    gps.f_get_position(&lat,&lon); // get latitude and longitude

    Serial.print("Position: ");
    
    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat,6);
    
    Serial.print(",");
    
    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon,6); 
    
   }
  }  
}


sm p2