How to Outputting GPS data via Serial Output?

Hi everyone

I just got a GPS shield (EM-406a) from sparkfun, and I know that the GPS is working well (I get signal lock). However, I’m truing to print out my lat, lon and light level (I have a photoresistor attached to the shield) using the following sketch, but for some reason, I can’t make it print the GPS data. Maybe there is a problem with the void loop?

The output I am getting is:
Luux:Awaiting for GPS signal lock…

Lux: 659 Lux: 660 Lux: 660 Lux: 660 Lux: 660 Lux: 660 Lux: 661 … … … … …

Since I am at quite a loss, I am hoping that a “fresh pair of eyes” might be able to see where my sketch might be having trouble.

Here’s the sketch I made:

#include <SoftwareSerial.h>  // for softserial
#include <TinyGPS.h>  // for GPS info
#include <Wire.h>    // for reading temp

#define RXPIN 2    // define arduino pin to TX info to GPS
#define TXPIN 3    // define arduino pin to RX info from GPS
SoftwareSerial uart_gps(RXPIN, TXPIN);  // Initialize the NewSoftSerial library 
                                        // to the pins defined above
#define TERMBAUD  9600    // This is the serial rate for your terminal program.
#define GPSBAUD  4800    // Set this value equal to the baud rate of your GPS
TinyGPS gps;    // Create an instance of the TinyGPS object
void getgps(TinyGPS &gps);  // This is where you declare prototypes for the 
                            // functions that will be using the TinyGPS library.
void lux_data();     // function that calls for reading the LUX sensor
int photocellPin = 0; // the cell and 10K pulldown are connected to Arduino A0
int photocellReading; // the analog reading from the sensor divider
int photocellPWR = 7; //5v for photocell


void setup()    // the setup routine runs once when you press reset:
{
  Serial.begin(TERMBAUD);            // Sets baud rate of the terminal program
  Wire.begin();
  uart_gps.begin(GPSBAUD);           // Sets baud rate of your GPS
  pinMode(photocellPWR, OUTPUT);     //Sets the Photocell pin out to 5v
  
  // Print status...  
  Serial.println("Awaiting for GPS signal lock...");
  Serial.println("");
  
  // Time to power up the sensors...
  digitalWrite(photocellPWR, HIGH);    //send 5v to PHOTORESISTOR
}
  

void loop()    // the loop routine runs over and over again forever:
{
       //Light levels measurements
       photocellReading = analogRead(photocellPin);  
       Serial.print("  Lux: ");
       Serial.print(photocellReading); // the raw analog reading
       
       
       //Get GPS data
       // Here, we check for data on the RX pin of the ardiuno, makes sure the 
      //data is valid NMEA sentences, then we jump to the getgps() function.
      while(uart_gps.available())     // While there is data on the RX pin...
      {
        int c = uart_gps.read();    // load the data into a variable...
          if(gps.encode(c))      // if there is a new valid sentence...
          {
          
            getgps(gps);         // then grab the data.   
           
          }
      }  
}


void getgps(TinyGPS &gps)
{
  float latitude, longitude;   // Define the variables that will be used
  gps.f_get_position(&latitude, &longitude);
  Serial.print("Buoy #1  ");    // Start printing data from GPS
  Serial.print(latitude,8); 
  Serial.print(", "); 
  Serial.println(longitude,8);
 
}
  Serial.print("  Lux: ");
  Serial.print(photocellReading); // the raw analog reading

Maybe comment out those two lines, it will be spamming your serial monitor.

Thanks for the suggestion. I modified the sketch slightly (per below) and I am now getting the GPS information. However, the data I am getting is multiple “Lux” values before I get the actual lat & lon reading (example below). How can I do to get an output that would look like this?
Lux: 765
25.18457, -80.1221
Lux: 764
25.11457, -80.1351
Lux: 763
etc etc?

I have a feeling that the loop is going “too fast” for the GPS data stream. Hence, it looks like the loop is getting multiple Lux readings before it can retrieve the GPS lat & lon info. Is this correct? If so, what I could I do to correct this?

Actual output I am getting now:
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
25.7877, -80.2241
Lux: 765
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
25.18457, -80.1221
Lux: 764
Lux: 765
Lux: 764
Lux: 764
Lux: 764
Lux: 765

etc… etc…

Here’s my sketch:

#include <SoftwareSerial.h>  // for softserial
#include <TinyGPS.h>  // for GPS info
#include <Wire.h>    // for reading temp

#define RXPIN 2    // define arduino pin to TX info to GPS
#define TXPIN 3    // define arduino pin to RX info from GPS
SoftwareSerial uart_gps(RXPIN, TXPIN);  // Initialize the NewSoftSerial library 
                                        // to the pins defined above
#define TERMBAUD  9600    // This is the serial rate for your terminal program.
#define GPSBAUD  4800    // Set this value equal to the baud rate of your GPS
TinyGPS gps;    // Create an instance of the TinyGPS object
void getgps(TinyGPS &gps);  // This is where you declare prototypes for the 
                            // functions that will be using the TinyGPS library.
void lux_data();     // function that calls for reading the LUX sensor
int photocellPin = 0; // the cell and 10K pulldown are connected to Arduino A0
int photocellReading; // the analog reading from the sensor divider
int photocellPWR = 7; //5v for photocell


void setup()    // the setup routine runs once when you press reset:
{
  Serial.begin(TERMBAUD);            // Sets baud rate of the terminal program
  Wire.begin();
  uart_gps.begin(GPSBAUD);           // Sets baud rate of your GPS
  pinMode(photocellPWR, OUTPUT);     //Sets the Photocell pin out to 5v
  
  // Print status...  
  Serial.println("Awaiting for GPS signal lock...");
  Serial.println("");
  
  // Time to power up the sensors...
  digitalWrite(photocellPWR, HIGH);    //send 5v to PHOTORESISTOR
}
  

void loop()    // the loop routine runs over and over again forever:
{
       //Get GPS data
       // Here, we check for data on the RX pin of the ardiuno, makes sure the 
      //data is valid NMEA sentences, then we jump to the getgps() function.
      while(uart_gps.available())     // While there is data on the RX pin...
      {
        int c = uart_gps.read();    // load the data into a variable...
          if(gps.encode(c))      // if there is a new valid sentence...
          {
          
            getgps(gps);         // then grab the data.   
           
          }
      }  


       //Light levels measurements
       photocellReading = analogRead(photocellPin);  
       Serial.print("Lux: ");
       Serial.println(photocellReading); // the raw analog reading
}


void getgps(TinyGPS &gps)
{
  float latitude, longitude;   // Define the variables that will be used
  gps.f_get_position(&latitude, &longitude);
  Serial.print(latitude,8); 
  Serial.print(", "); 
  Serial.println(longitude,8); 
}

Well, what did you expect? Every time through loop() you print the light level. So naturally you see that a lot.

Maybe show the light level once a second or so.

Maybe show the light level once a second or so.

Or, only read and show the light level when there is a GPS sentence recognized.

Or, show the light level only when it changes. That shouldn't be hard to do. Or if it changes by more than 1 or 2.

Thanks everyone for your very valid suggestions. However... PaulIS, you were completely right! Thank you so much for your suggestion! I can't believe I didn't think of that earlier... Sometimes, when you look at the code for so long, you stop seeing the obvious things...

Thank you so much for your awesome support & feedback, guys! :slight_smile: