U-Blox 6M Sensor doesn't work well

I added just this sensor to my mix, to detect the location. But,

  1. It takes a long time to start detecting.
  2. When it starts detecting (LED on the sensor board blinks) it only returns -1.

This is my whole code :

#define SENSOR_PIN 9 

#define GPS_RX 6
#define GPS_TX 7

#include <SoftwareSerial.h>

SoftwareSerial gpsSensor(GPS_RX, GPS_TX); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  gpsSensor.begin(9600);
  Serial.println("Welcome to home AQI station!");
  Serial.println("============================"); 
  Serial.println("GPS Module may take a while to detect your location. Please be patient"); 
  Serial.println("============================");

}

void loop() {
  // put your main code here, to run repeatedly:
  int aqi = analogRead(SENSOR_PIN); 
  
  Serial.print("Air Quality Index: ");
  Serial.print(aqi, DEC); 
  Serial.print(" PPM");

  if(aqi < 500){
    Serial.println(", Fresh Air");
    } else if(aqi > 500 && aqi <= 1000){
      Serial.println(", Poor Air");
    } else if(aqi > 1000){
      Serial.println(", Very Poor"); 
      }

  Serial.print("Location: ");
  Serial.println(gpsSensor.read());

   delay(30000); 

}

I don’t have any idea why!

P.S : Sorry if I don’t show schematics. I do not have access to a schematics software. But I made the circuit using attached image.

For a GPS module to work you must be outside of a building and have a clear view of the sky.

For a "factory fresh" cold start, be prepared to wait up to 15 minutes for the first satellite fix.

jremington:
For a GPS module to work you must be outside of a building and have a clear view of the sky.

For a "factory fresh" cold start, be prepared to wait up to 15 minutes for the first satellite fix.

Thanks. I would do that :slight_smile:

Hi.

Can you tell us what output you were expecting to get from the statement:

Serial.println(gpsSensor.read());

The GPS is emitting NMEA sentences on its TX pin as soon as you give it power. Those sentences have to be processed and the positional data extracted from them.

The blinking LED on the GPS module indicates that it has achieved a fix, so that's a major success for you.

John.

I modified your code as below. This shows the NMEA sentences coming from the GPS module.

The position data will be contained in these, if the module has a fix.

There is more work to do if you want to extract lat & long. The TinyGPS++ library is often used to do this.

It might help if you examine the code and understand just what it is doing and what yours was doing.

Hope that helps.

John.

#define SENSOR_PIN 9 

#define GPS_RX 6
#define GPS_TX 7

#include <SoftwareSerial.h>

SoftwareSerial gpsSensor(GPS_RX, GPS_TX); 

int incomingByte = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  gpsSensor.begin(9600);
  Serial.println("Welcome to home AQI station!");
  Serial.println("============================"); 
  Serial.println("GPS Module may take a while to detect your location. Please be patient"); 
  Serial.println("============================");

}

void loop() {
  // put your main code here, to run repeatedly:
  int aqi = analogRead(SENSOR_PIN); 
  
  Serial.print("Air Quality Index: ");
  Serial.print(aqi, DEC); 
  Serial.print(" PPM");

  if(aqi < 500){
    Serial.println(", Fresh Air");
    } else if(aqi > 500 && aqi <= 1000){
      Serial.println(", Poor Air");
    } else if(aqi > 1000){
      Serial.println(", Very Poor"); 
      }


/* while a byte is available at the GPS TX pin:
 *  read the byte
 *  write the byte to monitor
 */
 while (gpsSensor.available() > 0) {
    // read the incoming byte:
    incomingByte = gpsSensor.read();
    // write the byte:
    Serial.write(incomingByte);
  }

}

NissanCedric:
I modified your code as below. This shows the NMEA sentences coming from the GPS module.

The position data will be contained in these, if the module has a fix.

There is more work to do if you want to extract lat & long. The TinyGPS++ library is often used to do this.

It might help if you examine the code and understand just what it is doing and what yours was doing.

Hope that helps.

John.

#define SENSOR_PIN 9 

#define GPS_RX 6
#define GPS_TX 7

#include <SoftwareSerial.h>

SoftwareSerial gpsSensor(GPS_RX, GPS_TX);

int incomingByte = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  gpsSensor.begin(9600);
  Serial.println(“Welcome to home AQI station!”);
  Serial.println("============================");
  Serial.println(“GPS Module may take a while to detect your location. Please be patient”);
  Serial.println("============================");

}

void loop() {
  // put your main code here, to run repeatedly:
  int aqi = analogRead(SENSOR_PIN);
 
  Serial.print(“Air Quality Index: “);
  Serial.print(aqi, DEC);
  Serial.print(” PPM”);

if(aqi < 500){
    Serial.println(", Fresh Air");
    } else if(aqi > 500 && aqi <= 1000){
      Serial.println(", Poor Air");
    } else if(aqi > 1000){
      Serial.println(", Very Poor");
      }

/* while a byte is available at the GPS TX pin:
*  read the byte
*  write the byte to monitor
*/
while (gpsSensor.available() > 0) {
    // read the incoming byte:
    incomingByte = gpsSensor.read();
    // write the byte:
    Serial.write(incomingByte);
  }

}

I actually found that I had to use Serial.write() and not print. My code is now :

#define SENSOR_PIN 9 

#define GPS_RX 6
#define GPS_TX 7

#include <SoftwareSerial.h>

SoftwareSerial gpsSensor(GPS_RX, GPS_TX); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  gpsSensor.begin(9600);
  Serial.println("Welcome to home AQI station!");
  Serial.println("============================"); 
  Serial.println("GPS Module may take a while to detect your location. Please be patient"); 
  Serial.println("============================");

}

void loop() {
  // put your main code here, to run repeatedly:
  int aqi = analogRead(SENSOR_PIN); 
  
  Serial.print("Air Quality Index: ");
  Serial.print(aqi, DEC); 
  Serial.print(" PPM");

  if(aqi < 100){
    Serial.println(", Fresh Air");
    } else if(aqi > 100 && aqi <= 250){
      Serial.println(", Clean Air");
    } else if(aqi > 250 && aqi <= 500){
      Serial.println(", Getting Poor"); 
    } else if(aqi > 500 && aqi <= 750){
      Serial.println(", Poor"); 
    } else if(aqi > 750 && aqi <= 1000){
      Serial.println(", Very Poor");
    } else if(aqi > 1000) {
      Serial.println(", Evacuation is recommended");
    }

  if(gpsSensor.available() <= 0){
    Serial.println("Location not detected yet"); 
    } else {
    Serial.print("Location: "); 
    Serial.write(gpsSensor.read()); 
    }
    // This part is just for debugging the shit out of this
    Serial.println(" -> "); 
    Serial.print("Debugging mode: "); 
    Serial.println(gpsSensor.read());
   delay(3000); 

}

I have to fix the time it takes to detect a satellite and of course, I have to test my circuit outdoors. I didn’t fix these issues yet. But the output of this code is “Location not detected yet”.

This is the data that the GPS TX pin generates every second (without a fix):

$GPRMC,,V,,,,,,,,,,N53
$GPVTG,,,,,,,,,N
30
$GPGGA,,,,,,0,00,99.99,,,,,,48
$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99
30
$GPGSV,1,1,0079
$GPGLL,,,,,,V,N
64

These sentences have to be captured by the sketch in every loop -- all of them, from start to finish, not just some of them or parts of them. Then the sketch has to extract the positional data of interest.

The sketch is reading only one byte per loop, then delaying 3 seconds, then comes back to read another byte. During that 3 seconds the sentences have been replaced by a new lot. It will never get a complete data set and is not doing anything to extract the positional data anyway.

There are good tutorials on how to do this. You might like to practise with the examples that come with the TinyGPS++ library. That's where most people start.

GPS is not like other sensors. It's not really a sensor at all. It's a radio receiver.

P.S., is pin 9 an analogue or digital pin on your board?

John.

Haghiri75:
I have to fix the time it takes to detect a satellite and of course, I have to test my circuit outdoors. I didn't fix these issues yet. But the output of this code is "Location not detected yet".

And the code is always going to do that.

You seem to miss-understand what a GPS actually does.

A GPS is not a 'Sensor' that provides a location that you can get by reading a single byte\character from the GPS, which is what your code is doing.

GPSs output location information as a series of text characters in sentences, you need to read the entire sentence of maybe 64 characters to have all the information that gives you a location.

This reading of GPS sentences, and converting them into a location, is normally handled by a library, such as TinyGPS++.

I did change code to this :

#define SENSOR_PIN 9 

#define GPS_RX 6
#define GPS_TX 7

#include <SoftwareSerial.h>

SoftwareSerial gpsSensor(GPS_RX, GPS_TX); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  gpsSensor.begin(9600);
  Serial.println("Welcome to home AQI station!");
  Serial.println("============================"); 
  Serial.println("GPS Module takes around 15 minutes to detect a satellite"); 

  float wait_time = 0; 
  Serial.print(":");
  while(wait_time < 60){
    Serial.print("="); 
    delay(15000); 
    wait_time += 0.25; 
  }
  
    Serial.println(":"); 
}


void loop() {
  // put your main code here, to run repeatedly:
  int aqi = analogRead(SENSOR_PIN); 
  
  Serial.print("Air Quality Index: ");
  Serial.print(aqi, DEC); 
  Serial.print(" PPM");

  if(aqi < 100){
    Serial.println(", Fresh Air");
    } else if(aqi > 100 && aqi <= 250){
      Serial.println(", Clean Air");
    } else if(aqi > 250 && aqi <= 500){
      Serial.println(", Getting Poor"); 
    } else if(aqi > 500 && aqi <= 750){
      Serial.println(", Poor"); 
    } else if(aqi > 750 && aqi <= 1000){
      Serial.println(", Very Poor");
    } else if(aqi > 1000) {
      Serial.println(", Evacuation is recommended");
    }

  if(gpsSensor.available() <= 0){
    Serial.println("Location not detected yet"); 
    } else {
    Serial.print("Location: "); 
    Serial.write(gpsSensor.read()); 
    }
    // This part is just for debugging the shit out of this
    Serial.println(" -> "); 
    Serial.print("Debugging mode: "); 
    Serial.println(gpsSensor.read());
   //delay(3000); 

}

But I still don’t have this :

$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76

as an output.

I just have the plan to use TinyGPS++, but I wanted to see those things first.

Your ‘code’ is not going to work, and your still missing the point, GPSs put out a stream of characters, not just one.

If you want to see the output from the GPS, try this code, be sure to change to match the pins you are using for your GPS;

#define RX  2                       //pin number for data sent from GPS to Arduino   
#define TX  3                       //pin number for data sent from Arduino to GPS 

#include <SoftwareSerial.h>

SoftwareSerial ss(RX, TX);

void loop()
{
  while (ss.available() > 0)
    Serial.write(ss.read());
}

void setup()
{
  Serial.begin(115200);           //start Serial console ouput at 115200 baud
  ss.begin(9600);                 //start SoftwareSerial for serial device at 9600 baud
}

When you get it working, you will note that the GPS puts out a pile of characters, even though it does not have a fix …

srnet:
Your ‘code’ is not going to work, and your still missing the point, GPSs put out a stream of characters, not just one.

If you want to see the output from the GPS, try this code, be sure to change to match the pins you are using for your GPS;

#define RX  2                       //pin number for data sent from GPS to Arduino   

#define TX  3                      //pin number for data sent from Arduino to GPS

#include <SoftwareSerial.h>

SoftwareSerial ss(RX, TX);

void loop()
{
  while (ss.available() > 0)
    Serial.write(ss.read());
}

void setup()
{
  Serial.begin(115200);          //start Serial console ouput at 115200 baud
  ss.begin(9600);                //start SoftwareSerial for serial device at 9600 baud
}




When you get it working, you will note that the GPS puts out a pile of characters, even though it does not have a fix ........

I ran this. It seems ok to me, but still nothing on serial monitor. I obviously need to test it outdoors. Now, it’s dark and cold. I’ll test the whole circuit tomorrow.

It seems ok to me, but still nothing on serial monitor.

No, it is not OK. The GPS unit prints NMEA sentences even when it does not have a fix. Triple check your wiring.

Haghiri75:
I ran this. It seems ok to me, but still nothing on serial monitor. I obviously need to test it outdoors.

No, No and again No.

The GPS puts out characters that you sould see on the serial monitor, even if it does not have a fix.

Encase the GPS in a lead box several meteres thick, buried many kilometres underground, and the GPS should still output something.

Update : I checked the sensor. I actually did a very bad soldering, which resulted very loose pins on my sensor/module.
I changed my code a bit, in 9600 baud (GPS Sensor) I only get question marks. On 115200, I get data of air pollution module well, but not the GPS.
I think I may give up on this!

P.S : Sensor outputs some nonsesne as attached. It seems like noise to me.

Something really made me confused. My whole setup is close to power lines. May they effect the signals?

Has it occurred to you to move away from the power lines?

Power lines won't affect GPS, far too low in frequency.

You need to find what baud-rate is set in the module, just try all the standard ones in turn till you get NMEA data.

Usually a module will have serial commands you can give it to change baud-rate and select which NMEA sentences you get from it.

With TinyGPS, I didn’t get any output. This is my code BTW :

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define GPS_RX 6
#define GPS_TX 7

TinyGPSPlus gps; 

SoftwareSerial gpsSerial(GPS_RX, GPS_TX); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 
  gpsSerial.begin(9600); 

}

void loop() {
  // put your main code here, to run repeatedly:
  while(gpsSerial.available()>0){
    if(gps.encode(gpsSerial.read())){
      Serial.print("Lat: ");
      Serial.println(gps.location.lat()); 
      Serial.print("Long: "); 
      Serial.print(gps.location.lng()); 
      
      } else {
      Serial.println("No location detected");
        }

     if(millis() > 5000 && gps.charsProcessed() < 10){
      Serial.println("No GPS detected"); 
      while(true);
      }
    }

}

I don’t know what’s wrong with this module …

Most likely, nothing is wrong. It is just being used incorrectly.

Haghiri75:
I don't know what's wrong with this module ...

Wrong baud rate seems likely, as advised earlier.

Until you fix that basic problem, trying other programs, like TinyGPS, is a waste of time really.