Pantech.AI

How to Interface GPS Module with Arduino UNO

Overview of GPS

GPS Module

NEO-6M GPS Receiver Module

The Global Positioning System (GPS) uses signals from satellites in space and ground stations on Earth to determine precise locations on the Earth’s surface.

The NEO-6M GPS receiver module communicates with a microcontroller or PC terminal through USART. It receives data such as latitude, longitude, altitude, and UTC time from satellites in the form of NMEA strings. These strings need to be parsed to extract specific information for use.

For more details on GPS and its usage, refer to the “GPS Receiver Module” topic in the Sensors and Modules section.

Connection Diagram of GPS Module with Arduino Uno

Interfacing NEO-6M GPS Receiver Module With Arduino UNO

Interfacing NEO-6M GPS Receiver Module With Arduino UNO

We will display the data (latitude, longitude, altitude, and time) received from the GPS receiver module on the Arduino’s serial monitor.

For this, we’ll use Mikal Hart’s TinyGPSPlus library, available on GitHub.

Download the library from here, extract it, and add it to the Arduino IDE’s libraries folder.

For guidance on adding custom libraries to the Arduino IDE and using example sketches, see “Adding Library to Arduino IDE” in the Basics section.

We’ve created a simple sketch utilizing the functions and header files provided in this library.

Note: If most of the displayed data appears as “*,” move the GPS module connected to the Arduino to an open area (such as a balcony). The GPS receiver may take around 20-30 seconds to lock onto satellites initially. In open spaces, it usually takes less than 5 seconds, but if fewer than 3 satellites are visible, it may take longer.

GPS Code for Arduino Uno

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/* Create object named bt of the class SoftwareSerial */
SoftwareSerial GPS_SoftSerial(4, 3);/* (Rx, Tx) */
/* Create an object named gps of the class TinyGPSPlus */
TinyGPSPlus gps;			

volatile float minutes, seconds;
volatile int degree, secs, mins;

void setup() {
  Serial.begin(9600);	/* Define baud rate for serial communication */
  GPS_SoftSerial.begin(9600);	/* Define baud rate for software serial communication */
}

void loop() {
        smartDelay(1000);	/* Generate precise delay of 1ms */
        unsigned long start;
        double lat_val, lng_val, alt_m_val;
        uint8_t hr_val, min_val, sec_val;
        bool loc_valid, alt_valid, time_valid;
        lat_val = gps.location.lat();	/* Get latitude data */
        loc_valid = gps.location.isValid();	/* Check if valid location data is available */
        lng_val = gps.location.lng();	/* Get longtitude data */
        alt_m_val = gps.altitude.meters();	/* Get altitude data in meters */
        alt_valid = gps.altitude.isValid();	/* Check if valid altitude data is available */
        hr_val = gps.time.hour();	/* Get hour */
        min_val = gps.time.minute(); 	/* Get minutes */
        sec_val = gps.time.second();	/* Get seconds */
        time_valid = gps.time.isValid();	/* Check if valid time data is available */
        if (!loc_valid)
        {          
          Serial.print("Latitude : ");
          Serial.println("*****");
          Serial.print("Longitude : ");
          Serial.println("*****");
        }
        else
        {
          DegMinSec(lat_val);
          Serial.print("Latitude in Decimal Degrees : ");
          Serial.println(lat_val, 6);
          Serial.print("Latitude in Degrees Minutes Seconds : ");
          Serial.print(degree);
          Serial.print("\t");
          Serial.print(mins);
          Serial.print("\t");
          Serial.println(secs);
          DegMinSec(lng_val);	/* Convert the decimal degree value into degrees minutes seconds form */
          Serial.print("Longitude in Decimal Degrees : ");
          Serial.println(lng_val, 6);
          Serial.print("Longitude in Degrees Minutes Seconds : ");
          Serial.print(degree);
          Serial.print("\t");
          Serial.print(mins);
          Serial.print("\t");
          Serial.println(secs);
        }
        if (!alt_valid)
        {
          Serial.print("Altitude : ");
          Serial.println("*****");
        }
        else
        {
          Serial.print("Altitude : ");
          Serial.println(alt_m_val, 6);    
        }
        if (!time_valid)
        {
          Serial.print("Time : ");
          Serial.println("*****");
        }
        else
        {
          char time_string[32];
          sprintf(time_string, "Time : %02d/%02d/%02d \n", hr_val, min_val, sec_val);
          Serial.print(time_string);    
        }
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (GPS_SoftSerial.available())	/* Encode data read from GPS while data is available on serial port */
      gps.encode(GPS_SoftSerial.read());
/* Encode basically is used to parse the string received by the GPS and to store it in a buffer so that information can be extracted from it */
  } while (millis() - start < ms);
}

void DegMinSec( double tot_val)		/* Convert data in decimal degrees into degrees minutes seconds form */
{  
  degree = (int)tot_val;
  minutes = tot_val - degree;
  seconds = 60 * minutes;
  minutes = (int)seconds;
  mins = (int)minutes;
  seconds = seconds - minutes;
  seconds = 60 * seconds;
  secs = (int)seconds;
}

Leave a Comment

Your email address will not be published. Required fields are marked *