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.
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:
- Introducing the NEO-6M GPS Module
- Wiring the NEO-6M GPS Module to the ESP8266
- ESP8266 with the NEO-6M GPS Module – Getting GPS Data
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.
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.
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.
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
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).
NEO-6M GPS Module | ESP8266 |
VCC | 3V3 |
RX | GPIO 12 (D6) |
TX | GPIO 14 (D5) |
GND | GND |
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.
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.
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("");
}
}
}
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:
Latitude | gps.location.lat() |
Longitude | gps.location.lng() |
Speed (km/h) | gps.speed.kmph() |
Altitude (meters) | gps.altitude.meters() |
HDOP | gps.hdop.value() |
The number of visible satellites | gps.satellites.value() |
Year | gps.date.year() |
Month | gps.date.month() |
Day | gps.date.day() |
Hour | gps.time.hour() |
Minutes | gps.time.minute() |
Seconds | gps.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.
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.
The module’s blue LED will start blinking when it finds a position fix.
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.
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.
Nice Tutorial!
Thank you and keep up the good work!
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
OOPS i have translate also link…..
forum.arduino.cc/t/esp32-ottimizzazione-del-modulo-neo-6m-gps/1291207
Hi.
Great.
Thank you for sharing.
Regards,
Sara
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?
Hi.
Double-check the wiring of your serial connection.
Regards,
Sara
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.
Hi.
Can you share the complete error code?
Regards,
Sara