ESP8266 NodeMCU with NEO-M8N GPS Module (Arduino IDE)

Learn to interface the NEO-M8N GPS Module with the ESP8266 NodeMCU board programmed with Arduino IDE to get GPS data: latitude, longitude, altitude, UTC time, number of visible satellites, and more.

ESP8266 NodeMCU with NEO-M8N GPS Module Arduino IDE

In summary, in this tutorial you’ll learn how to:

  • Wire the NEO-M8N GPS module to the ESP8266 via serial;
  • Parse raw data to obtain selected and readable GPS information;
  • Get your current location, date, time, and more…

Table of Contents

We’ll cover the following subjects:

Introducing the NEO-M8N GPS Module

The NEO-M8N GPS module is one of the most popular GPS receivers used with microcontrollers in navigation and tracking projects. It can get data about latitude, longitude, altitude, and time.

It supports multiple satellite systems, including GPS, Galileo, GLONASS, and BeiDou. It offers better satellite tracking than the NEO-6M, making it more reliable in challenging conditions.

NEO-M8N-GPS-Module

According to the datasheet, it has a horizontal position accuracy of 2.5 to 4 meters and quick startup times (1 second for hot start, 26–57 seconds for cold start—expect longer times if you’re close to buildings).

The module includes a backup battery, built-in EEPROM, and an LED indicator that blinks when a position fix is achieved.

This module typically comes with a ceramic GPS antenna. But, you can change it to any other compatible antenna that might suit your project better. For example, I like to use the one at the right in the picture below because it is waterproof, and the antenna comes with a long cable which allows for more flexibility.

antennas for GPS modules

The NEO-M8N GPS Module communicates with a microcontroller using Serial communication protocol, and works with standard NMEA sentences. NMEA stands for National Marine Electronics Association, and in the world of GPS, it is a standard data format supported by GPS manufacturers.

Where to buy?

You can check our Maker Advisor Tools page to compare the NEO-M8N GPS receiver module price in different stores:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring the NEO-M8N GPS Module to the ESP8266 NodeMCU

To communicate with the NEO-6M GPS module, we’ll use software serial, so you can use any available GPIOs. We’ll use: GPIO 14 (D5), and GPIO 12 (D6).

Wiring the NEO-M8N GPS Module to the ESP8266 NodeMCU circuit diagram

Recommended reading: ESP8266 Pinout Reference: Which GPIO pins should you use?

NEO-6M GPS ModuleESP8266 NodeMCU
VCC3V3
RXGPIO 12 (D6)
TXGPIO 14 (D5)
GNDGND

Installing the TinyGPSPlus Library

We’ll program the ESP8266 NodeMCU using Arduino IDE. Make sure you have the ESP8266 boards installed by following this guide: Installing ESP8266 Board in Arduino IDE 2 (Windows, Mac OS X, Linux).

To decode the NMEA sentences received by the GPS module, we’ll use the TinyGPSPlus library. Install it before proceeding.

In the Arduino IDE, go to Sketch > Include Library > Manage Libraries or click on the Library Manager icon at the left sidebar.

Search for TinyGPSPlus and install the library by Mikal Hart.

Installing TinyGPSPlus Library Arduino IDE

ESP8266 NodeMCU with the NEO-M8N GPS Module – Getting GPS Data

ESP8266 with the NEO-M8N GPS Module - Getting GPS Data

The following code shows how to get GPS data using the TinyGPSPlus library. We’ll get date, time, speed, altitude, number of visible satellites, and HDOP (a measurement of how precise the signal is).

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete instructions at https://RandomNerdTutorials.com/esp8266-nodemcu-neo-m8n-gps-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

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

// Define the RX and TX pins for Software Serial
#define RX 14
#define TX 12

#define GPS_BAUD 9600

// The TinyGPS++ object
TinyGPSPlus gps;

// Create an instance of Software Serial
SoftwareSerial gpsSerial(RX, TX);

void setup() {
  // Serial Monitor
  Serial.begin(115200);
  
  // Start Serial 2 with the defined RX and TX pins and a baud rate of 9600
  gpsSerial.begin(GPS_BAUD);
  Serial.println("Software Serial started at 9600 baud rate");
}

void loop() {
  // This sketch displays information every time a new sentence is correctly encoded.
  unsigned long start = millis();

  while (millis() - start < 1000) {
    while (gpsSerial.available() > 0) {
      gps.encode(gpsSerial.read());
    }
    if (gps.location.isUpdated()) {
      Serial.print("LAT: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("LONG: "); 
      Serial.println(gps.location.lng(), 6);
      Serial.print("SPEED (km/h) = "); 
      Serial.println(gps.speed.kmph()); 
      Serial.print("ALT (min)= "); 
      Serial.println(gps.altitude.meters());
      Serial.print("HDOP = "); 
      Serial.println(gps.hdop.value() / 100.0); 
      Serial.print("Satellites = "); 
      Serial.println(gps.satellites.value()); 
      Serial.print("Time in UTC: ");
      Serial.println(String(gps.date.year()) + "/" + String(gps.date.month()) + "/" + String(gps.date.day()) + "," + String(gps.time.hour()) + ":" + String(gps.time.minute()) + ":" + String(gps.time.second()));
      Serial.println("");
    }
  }
}

View raw code

How Does the Code Work?

You start by importing the TinyGPSPlus and SoftwareSerial library.

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

Then, you define the GPIOs you want to use for Software Serial and the GPS module baud rate.

// Define the RX and TX pins for Software Serial 2
#define RX 14
#define TX 12

#define GPS_BAUD 9600

Then, you create a TinyGPS++ object:

TinyGPSPlus gps;

Create SoftwareSerial instance on the pins you defined earlier.

SoftwareSerial gpsSerial(RX, TX);

In the setup(), initialize the Serial Monitor and the serial communication with the GPS module.

void setup() {
  // Serial Monitor
  Serial.begin(115200);
  
  // Start Serial 2 with the defined RX and TX pins and a baud rate of 9600
  gpsSerial.begin(GPS_BAUD);
  Serial.println("Software Serial started at 9600 baud rate");
}

In the loop() is where you request the information. Parse the data from the GPS module into the TinyGPS++ object using the encode() method as follows.

while (gpsSerial.available() > 0) {
  gps.encode(gpsSerial.read());
}

Then, you can query the gps object to see if any data fields have been updated:

if (gps.location.isUpdated()) {

If there is new data, we can get it as follows:

Latitudegps.location.lat()
Longitudegps.location.lng()
Speed (km/h)gps.speed.kmph()
Altitude (meters)gps.altitude.meters()
HDOPgps.hdop.value()
The number of visible satellitesgps.satellites.value()
Yeargps.date.year()
Monthgps.date.month()
Daygps.date.day()
Hourgps.time.hour()
Minutesgps.time.minute()
Secondsgps.time.second()

In the code, we get the data and print all the information in the Arduino IDE Serial Monitor.

Serial.print("LAT: ");
Serial.println(gps.location.lat(), 6);
Serial.print("LONG: "); 
Serial.println(gps.location.lng(), 6);
Serial.print("SPEED (km/h) = "); 
Serial.println(gps.speed.kmph()); 
Serial.print("ALT (min)= "); 
Serial.println(gps.altitude.meters());
Serial.print("HDOP = "); 
Serial.println(gps.hdop.value() / 100.0); 
Serial.print("Satellites = "); 
Serial.println(gps.satellites.value()); 
Serial.print("Time in UTC: ");
Serial.println(String(gps.date.year()) + "/" + String(gps.date.month()) + "/" + String(gps.date.day()) + "," + String(gps.time.hour()) + ":" + String(gps.time.minute()) + ":" + String(gps.time.second()));
Serial.println("");

Testing the Code

Upload the code to your ESP8266 board. Open the Serial Monitor at a baud rate of 115200. Make sure your GPS module is placed outside or next to a window to get data from satellites.

Note: you may need to wait a few seconds or minutes until the module can get a position fix.

ESP8266 Get GPS Data from NEO M8N

You’ll get data on the Serial Monitor about your current location, speed, altitude, number of visible satellites HDOP, and time.

HDOP stands for Horizontal Dilution of Precision. This is a measurement of position-fixing accuracy. The higher the HDOP value is, the less accurate the position fix will be. Ideally, you should get a value lower than 2. A lower value means a better accuracy.

Wrapping Up

In this guide, you learned how to use the NEO-M8N GPS module with the ESP8266 and how to get information about location, time, altitude, speed, number of satellites, and more.

We hope you’ve found this guide useful. We have guides for other modules you may find useful:

If you’d like to learn more about the ESP8266, make sure to check out our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.