How to use GPS with arduino – GPS NEO 6M tutorial

miliohm
4 Mei 2020 11:43
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

GPS is kind popular right now. Many modern devices use this for tracking purposes or just help you navigate when you are lost.

GPS Neo 6M is one of most popular GPS module that we can use easily with arduino. This module use serial to communicate with another device. 

NEO 6M GPS Wiring with arduino

Wire the GPS module with arduino just like picture below


NEO 6M GPS and arduino Wiring

NEO 6M GPS arduino Code

First you have to download the library. You can download the library here. We will use TinyGPS++ library in this tutorial.

Once you download the library and install that, restart the arduino and we are ready to start the code.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) 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).
*/
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  Serial.println(F("DeviceExample.ino"));
  Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by Mikal Hart"));
  Serial.println();
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();

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

void displayInfo()
{
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.println();
}

Upload the code and open the serial monitor. You should see the result.

Serial monitor result

If you get the invalid location, it means that you haven’t get the signal yet. It can happen if you are indoor. The GPS signal cannot go through the concrete wall. Try go outside, and if you still got invalid location you should wait until the signal is stabilized.

Now we have successfully extract the location and time from satellites. You can directly copy the longitude and latitude to Google Map and see your location. And by the way, the time is GMT.

You can watch full tutorial on my youtube channel :

https://youtu.be/CZP6HolC0ng

NEO 6M GPS Youtube Tutorial

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •