Unable to connect GPS - LS20031

Hi,
I'm new the the arduino world so please bear with me. I have no idea where to start so please help me out.

I recently purchased an arduino Mega 2560 board and a GPS Receiver - LS20031 5Hz (66 Channel). However, Im unable to connect and receive data from the gps. I have tried the receiving side of the GPS to connect to either pin 2 or 3 with no success. I've played around with both wires(rx and tx) in either pin but again with no success. Any ideas? Code and error below.

Thank you

#include "SoftwareSerial.h"

int mode=-1;
char im[80];
char incoming_char = 0;
int count = 0;

SoftwareSerial gps(2,3);

float lat;
char latD;
float lng;
char lngD;
float time;

void setup(){
  gps.begin(4800);
  Serial.begin(9600);
  
  char b = '0';
  while(b != 36)
  {
     b = gps.read();
  }
  
  Serial.println('c');
  char c = 'b';
  while(c !='c')
  {
    c = Serial.read();
  }
}

void loop(){
  if(gps.available()>0){
  incoming_char = gps.read();
  
  if(incoming_char == 36){
  
    if(im[0]=='G' && im[1]=='P' && im[2]=='G' && im[3]=='G' && im[4]=='A'){
      
      char timeBuffer[10];
      timeBuffer[0] = im[6];
      timeBuffer[1] = im[7];
      timeBuffer[2] = im[8];
      timeBuffer[3] = im[9];
      timeBuffer[4] = im[10];
      timeBuffer[5] = im[11];
      timeBuffer[6] = im[12];
      timeBuffer[7] = im[13];
      timeBuffer[8] = im[14];
      timeBuffer[9] = im[15];
      time = atof(timeBuffer);
      
      char latBuffer[9];      
      latBuffer[0] = im[17];
      latBuffer[1] = im[18];
      latBuffer[2] = im[19];
      latBuffer[3] = im[20];
      latBuffer[4] = im[21];
      latBuffer[5] = im[22];
      latBuffer[6] = im[23];
      latBuffer[7] = im[24];
      latBuffer[8] = im[25];
      lat = atof(latBuffer);
      
      latD = im[27];
      
      char lngBuffer[10];
      lngBuffer[0] = im[29];
      lngBuffer[1] = im[30];
      lngBuffer[2] = im[31];
      lngBuffer[3] = im[32];
      lngBuffer[4] = im[33];
      lngBuffer[5] = im[34];
      lngBuffer[6] = im[35];
      lngBuffer[7] = im[36];
      lngBuffer[8] = im[37];
      lngBuffer[9] = im[38];
      lng = atof(lngBuffer);
      
      lngD= im[40];
      
    }
    count = 0;
    
  }else{
  im[count] = incoming_char;
  count++;
  }
}

if(Serial.available()>0)
{
    mode = Serial.read();
    switch(mode)
    {
      case 'T':
      Serial.println(time);
      break;
      
      case 'A':
      Serial.println(lat);
      break;

      case 'a':
      Serial.println(latD);
      break;
      
      case '0':
      Serial.println(lng);
      break;      

      case 'o':
      Serial.println(lngD);
      break;      
    }
}
}

error:

In file included from C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:13:
C:\Program Files\Arduino\hardware\arduino\cores\arduino\/NMEAGPS.h:62: error: expected ',' or '...' before '&' token
C:\Program Files\Arduino\hardware\arduino\cores\arduino\/NMEAGPS.h:62: error: ISO C++ forbids declaration of 'SoftwareSerial' with no type
C:\Program Files\Arduino\hardware\arduino\cores\arduino\/NMEAGPS.h:144: error: 'SoftwareSerial' does not name a type
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:27: error: expected ',' or '...' before '&' token
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:27: error: ISO C++ forbids declaration of 'SoftwareSerial' with no type
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In constructor 'NMEAGPS::NMEAGPS(int)':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:30: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:30: error: 'gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'void NMEAGPS::copyHelper(const NMEAGPS&)':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:63: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:63: error: 'const class NMEAGPS' has no member named '_gpsSerial'
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'void NMEAGPS::initialize()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:95: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:95: error: 'SoftwareSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'bool NMEAGPS::isListening()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:132: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'void NMEAGPS::listen()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:136: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'int NMEAGPS::available()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:140: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'bool NMEAGPS::overflow()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:144: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp: In member function 'bool NMEAGPS::readGPSStream()':
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:170: error: '_gpsSerial' was not declared in this scope
C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:173: error: '_gpsSerial' was not declared in this scope

However, Im unable to connect and receive data from the gps.

How would you know, when the code you are using won't even compile?

That code won't work on a Mega, even of it did compile. The Mega has 4 hardware serial ports. Why are you using SoftwareSerial?

Why are you using it on pins it doesn't work on?

In file included from C:\Program Files\Arduino\hardware\arduino\cores\arduino\NMEAGPS.cpp:13:

Why did you put this file here? That isn't even close to where user-downloaded libraries go.

I guess bear with me was not big enough to read. I'm new at this so I'm not very familiar with the ins and outs of arduino. So, I take it I should probably go with the Uno rather than the Mega?

And yes I've just read about it... the code should have had NewSoftSerial.h rather than SoftwareSerial.h

I've changed that but now I'm getting the following error:

In file included from sketch_apr18b.ino:1:
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/NewSoftSerial.h:71: error: conflicting return type specified for 'virtual void NewSoftSerial::write(uint8_t)'
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:48: error:   overriding 'virtual size_t Print::write(uint8_t)'

So we have your original code, but you've changed that and we have no idea what's in your new code, and have no way of figuring out what you error is caused by.

Well, no matter for now. As you've discovered, there are differences between the Uno and the Mega, so here's a tip for you.

Rather than just grabbing some code from somewhere and trying to make it run, it might be wise to actually read the code and try to understand what's happening. Better still, try to figure out what you really need to get a very simple sketch running and accessing your GPS. To that end, here's a simple sketch. The main purpose of it is:

  1. To discover what baud rate the GPS is running at
  2. to do the very minumum required to convince yourself that the GPS is actually working
  3. to give yourself something to build on.

Use your Mega.
Wire the GPS Tx to the Mega's Rx1 (pin 19)
Wire the GPS Rx to the Mega's Tx1 (pin 20)

To run this, compile and upload to your Mega.
Open the Serial Monitor and set the baud rate to 115200 (or to whatever you have set in the Serial.begin() argument.
In the Serial monitor input box, enter 0, 1, 2, 3, or 4 to discover the baud rate of the GPS.
For some reason I can't figure out, once you set the baud rate, the next time you run the sketch, even if the Serial1.begin() line has the wrong baud rate, it will still work. In order to figure out the right baud rate, enter a 1, press Send, then a 0, and press Send, then try all numbers; 0 through 4. The right baud rate should show you some valid sentences. The rest will show garbage.

unsigned long baud[] = {4800, 9600, 19200, 57600, 115200};
byte indx = 0;

void setup() {
  Serial.begin(115200);
  Serial1.begin(baud[1]);
  inform();
}

void loop() {
    if (Serial.available()) {
      char ch = Serial.read();
      Serial.print("\n***********************************  ");
      Serial.println (ch);
      if (ch >= '0' && ch <= '4') {
        indx = ch - '0';
        Serial1.end();
        delay(500);
        inform();
        Serial1.begin(baud[indx]);
      }
    }

    if (Serial1.available()) {
      char inbyte = Serial1.read();
      Serial.print(inbyte);
    }
}

void inform() {
  Serial.print("\nGPS serial is set to ");
  Serial.print(baud[indx]);
  Serial.println(" baud");
}

lar3ry:

thank you very much for your help... Yes, I know, you are right regarding being somewhat clueless. I'm doing this for my Aerodynamics class and this was the best way to approach the task. I thought I wouldn't be so bad since I have good knowledge of Matlab and programing. However, I'm getting a little bit overwhelmed since I'm pressed on time and the C+ and Matlab code are somewhat different.

I've done what you're asked, I slightly modified the code, and here's what I got:

    if (Serial1.available()) {
      char inbyte = Serial1.read();
      Serial.print(inbyte);
      Serial.print("Works");
    }
GPS serial is set to 4800 baud
Works
***********************************  2

GPS serial is set to 19200 baud
***********************************  1
Works

GPS serial is set to 9600 baud
***********************************  3

GPS serial is set to 57600 baud
Works
***********************************  4

GPS serial is set to 115200 baud
Works

And yes I've just read about it... the code should have had NewSoftSerial.h rather than SoftwareSerial.h

Where did you read that nonsense? (Or perhaps I should ask when it was written).

You should take your snippets to http://snippets-r-us.com. It's amazing what they can tell from your snippets.

ok ok ok ok ok you got me... SoftwareSerial.h is on the old arduino software where as the NewSoftSerial.h is on the new software. I just dont understand the attacking and attitude... relax

I'm here for help... if I did something wrong please let me know otherwise no need for attitude... Thank you

I've made changes to the initial code that reflects the Mega board. It compiles and uploads to the board. However, I dont think Im communicating with the GPS.

int mode=-1;
char im[80];
char incoming_char = 0;
int count = 0;

float lat;
char latD;
float lng;
char lngD;
float time;

void setup(){
  Serial1.begin(19200);
  Serial.begin(115200);
  
  char b = '0';
  while(b != 36)
  {
     b = Serial1.read();
  }
  
  Serial.println('c');
  char c = 'b';
  while(c !='c')
  {
    c = Serial.read();
  }
}

void loop(){
  if(Serial1.available()>0){
  incoming_char = Serial1.read();
  
  if(incoming_char == 36){
  
    if(im[0]=='G' && im[1]=='P' && im[2]=='G' && im[3]=='G' && im[4]=='A'){
      
      char timeBuffer[10];
      timeBuffer[0] = im[6];
      timeBuffer[1] = im[7];
      timeBuffer[2] = im[8];
      timeBuffer[3] = im[9];
      timeBuffer[4] = im[10];
      timeBuffer[5] = im[11];
      timeBuffer[6] = im[12];
      timeBuffer[7] = im[13];
      timeBuffer[8] = im[14];
      timeBuffer[9] = im[15];
      time = atof(timeBuffer);
      
      char latBuffer[9];      
      latBuffer[0] = im[17];
      latBuffer[1] = im[18];
      latBuffer[2] = im[19];
      latBuffer[3] = im[20];
      latBuffer[4] = im[21];
      latBuffer[5] = im[22];
      latBuffer[6] = im[23];
      latBuffer[7] = im[24];
      latBuffer[8] = im[25];
      lat = atof(latBuffer);
      
      latD = im[27];
      
      char lngBuffer[10];
      lngBuffer[0] = im[29];
      lngBuffer[1] = im[30];
      lngBuffer[2] = im[31];
      lngBuffer[3] = im[32];
      lngBuffer[4] = im[33];
      lngBuffer[5] = im[34];
      lngBuffer[6] = im[35];
      lngBuffer[7] = im[36];
      lngBuffer[8] = im[37];
      lngBuffer[9] = im[38];
      lng = atof(lngBuffer);
      
      lngD= im[40];
      
    }
    count = 0;
    
  }else{
  im[count] = incoming_char;
  count++;
  }
}

if(Serial.available()>0)
{
    mode = Serial.read();
    switch(mode)
    {
      case 'T':
      Serial.println(time);
      break;
      
      case 'A':
      Serial.println(lat);
      break;

      case 'a':
      Serial.println(latD);
      break;
      
      case '0':
      Serial.println(lng);
      break;      

      case 'o':
      Serial.println(lngD);
      break;      
    }
}
}
  char b = '0';
  while(b != 36)
  {
     b = Serial1.read();
  }

What is this supposed to be doing?

For now, run a simple sketch that does nothing but test for Serial1 data, from the GPS. If there is any, read it and print it to Serial.

Unfortunately, using Lar3ry's code does not seem to print anything to the Serial. So i'm not really sure what else to do to test it.

Is there anything that I could add to Lar3ry's code to see if it works?

I was expecting to see something here but I'm not getting anything...

   if (Serial1.available()) {
      char inbyte = Serial1.read();
      Serial.print(inbyte);
    }

I was expecting to see something here but I'm not getting anything...

You should take your snippets to http://snippets-r-us.com, then, They are whizzes with snippets.

CountVlad:
Unfortunately, using Lar3ry's code does not seem to print anything to the Serial. So i'm not really sure what else to do to test it.

Is there anything that I could add to Lar3ry's code to see if it works?

I was expecting to see something here but I'm not getting anything...

   if (Serial1.available()) {

char inbyte = Serial1.read();
     Serial.print(inbyte);
   }

You should be getting sentences from the GPS, coming in on Serial1. This code sends those sentences out on Serial, one character at a time.

Did you try sending '0' from the Serial Monitor? It looks like you only
Is your GPS connected to 5V and GND?
Please double-check your connections RX1 -> GPS_Tx , Tx1 to GPS_Rx

Here's what I get...

***********************************  0

GPS serial is set to 4800 baud
??J?&È*KZY ...

***********************************  1

GPS serial is set to 9600 baud
$GPRMC,171206.000,V,5033.1055,N,10404.2297,W,0.23,0.00,190414,,,N*64
$GPGGA,171207.000,5033.1114,N,10404.2337,W,0,4,,675.4,M,-20.7,M,,*7B
... 

***********************************  2

GPS serial is set to 19200 baud
*Z+ZY*kYJI+J  ...

***********************************  3

GPS serial is set to 57600 baud
àààà ...

***********************************  1

GPS serial is set to 9600 baud
$GPGSV,3,2,10,25,27,052,22,01,27,246,14,14,25,087,16,23,23,277,*75

OK so I'm embarrassed. I just realized that I soldered and wired the GPS the wrong way to the board. It works now. Thank you

OK so I'm embarrassed. I just realized that I soldered and wired the GPS the wrong way to the board. It works now. Thank you

Thank you for following up and letting us know.