ESP8266 NodeMCU with NEO-6M GPS Module (Arduino IDE)

Learn how to interface the NEO-6M GPS module with an ESP8266 NodeMCU board to get GPS data. You’ll learn how to get latitude, longitude, altitude, speed, and UTC time. The ESP8266 will be programmed using Arduino IDE.

ESP8266 NODEMCU with NEO-6M GPS Module Arduino IDE

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

  • Wire the NEO-6M 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…

We have a similar guide for the ESP32 board with NEO-6M GPS Module (Arduino IDE)

Table of Contents

Throughout this tutorial, we’ll cover the following subjects:

If you’re new to the ESP8266 board, you may like reading: Getting Started with ESP8266 NodeMCU Development Board.

Introducing the NEO-6M GPS Module

The NEO-6M GPS module is a GPS receiver compatible with most microcontroller boards. It can get data about location, speed, altitude, and time.

NEO-6M GPS Module

It comes with a small backup battery, external EEPROM, and an LED signal indicator. This LED will start blinking when it gets a position fix.

Usually, these modules come with a GPS ceramic antenna.

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-6M GPS Module communicates with a microcontroller using serial communication protocol.

This module 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.

NEO-6M GPS Module Features

NEO-6M GPS Module

In summary:

  • This module has an external antenna and built-in EEPROM.
  • Interface: RS232 TTL
  • Power supply: 3V to 5V
  • Default baudrate: 9600 bps
  • Works with standard NMEA sentences

Where to buy?

You can get the NEO-6M GPS module for a price between $5 to $20. We recommend checking the NEO-6M GPS module page on Maker Advisor to compare the prices in different stores and find the best one.

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-6M 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-6M GPS Module to the ESP8266 NodeMCU

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

Installing the TinyGPSPlus Library

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 Libary Manager icon at the left sidebar.

Search for TinyGPSPlus and install the library by Mikal Hart.

Installing TinyGPSPlus Library Arduino IDE

ESP8266 with the NEO-6M GPS Module – Getting GPS Data

After wiring the GPS module to the ESP8266 and installing the TinyGPSPlus library, let’s run a simple example to get latitude, longitude, altitude, data, time, and more.

ESP8266 NodeMCU with NEO-6M GPS Module

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 accurate the signal is).

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete instructions at https://RandomNerdTutorials.com/esp8266-nodemcu-neo-6m-gps-module-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 2
#define RX 12
#define TX 14

#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 12
#define TX 14

#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 board.

Arduino IDE 2 Upload Button

Then, open the Serial Monitor at a baud rate of 115200.

Make sure the antenna is connected and that the module or antenna is placed outside or next to a window so that it can get data from the satellites.

active GPS antenna for NEO-6M

The module’s blue LED will start blinking when it finds a position fix.

NEO-6M GPS Module Blue LED Blinking

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.

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

Get GPS data with ESP32 - NEO-6M module

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.

Now, you can take this project further and display the data on an OLED display, for example. To learn how to use an OLED display with the ESP8266 board, you can follow the next tutorial:

Wrapping Up

In this tutorial, you learned how to use the NEO-6M GPS Module with the ESP8266 to get GPS data: latitude, longitude, altitude, speed, date, time, number of satellites, and more.

The accuracy of the data will depend on where the module is placed and the antenna used. For better performance, the sensor should be placed outside or next to a window and away from narrow streets with tall buildings. The higher the number of visible satellites, the more accurate the position will be.

We hope you’ve found this tutorial useful. We have guides for more than 20 modules and sensors with the ESP8266, you can check them below:

If you want to learn more about the ESP8266, 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!

8 thoughts on “ESP8266 NodeMCU with NEO-6M GPS Module (Arduino IDE)”

  1. You always make nice tutorials.
    If you are interested I have published a post on the Arduino forum that explains how to optimize the settings in the Neo-6M module using the U-Center program.
    In the examples I do not use TinyGPS or Software serial but I prefer to use Hardware serial and at most the Time library in the final example
    here is the link to the post:
    forum.arduino.cc/t/esp32-optimization-of-the-neo-6m-gps-module/1291207
    Use Google translate if you do not understand what is written.
    Bye

    Reply
  2. Having a problem here. The GY-GPS6MV2 device’s light is flashlight which I understand to mean that it has locked onto GPS satellites but the app outputs no data. If I serial print gpsSerial.available() each time through the main loop hit prints a zero. Thinking something is wrong here but not sure where to look. Any suggestions?

    Reply
  3. Salve, mi chiamo Tommaso ho provato questo sketch e mi da un errore di libreria ( <AVR/INTERRUPT.H>)e non riesco a trovarla . si puo avere un aiuto. GRAZIE.

    Reply

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.