ESP32 - GPS

by DIYables in Circuits > Microcontrollers

12 Views, 0 Favorites, 0 Comments

ESP32 - GPS

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-gps

This build reads location, speed, and date and time from a NEO-6M GPS module using an ESP32. The ESP32 parses the raw NMEA data stream from the module with the TinyGPS++ library and prints latitude, longitude, altitude, speed in km/h, and the current date and time to the Serial Monitor. It also shows how to calculate the distance from the current GPS position to a fixed set of coordinates, such as London.

Supplies

Wiring

The NEO-6M GPS module has four pins. Connect VCC to 5V and GND to GND. The module's TX pin carries data to the ESP32, so wire it to the ESP32 pin used as Serial RX (GPIO16 in the sketch). The module's RX pin receives data from the ESP32, so wire it to the ESP32 pin used as Serial TX (GPIO17 in the sketch).

How It Works

The GPS module continuously outputs NMEA sentences over serial at 9600 baud. The ESP32 reads these bytes on Serial2 and feeds them one at a time into a TinyGPSPlus object with gps.encode(). Once a full sentence is decoded, the code checks whether each value (location, altitude, speed, date, time) is valid before printing it, since a cold GPS module may not have a fix yet.

Code

The sketch starts Serial at 9600 baud for the Serial Monitor and Serial2 at the GPS module's default 9600 baud on GPIO16/GPIO17. In the loop, it reads any available bytes from the module and passes them to gps.encode(). When a new sentence is parsed, it prints latitude, longitude, altitude, speed in km/h, and date and time, each marked INVALID if not yet available. A separate version of the code adds a distance calculation from the current fix to London's coordinates (51.508131, -0.128002).

esp32-gps.ino - the sketch from the tutorial, unchanged.

/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-gps
*/

#include <TinyGPS++.h>

#define GPS_BAUDRATE 9600 // The default baudrate of NEO-6M is 9600

TinyGPSPlus gps; // the TinyGPS++ object

void setup() {
Serial.begin(9600);
Serial2.begin(GPS_BAUDRATE, SERIAL_8N1, 16, 17);

Serial.println(F("ESP32 - GPS module"));
}

void loop() {
if (Serial2.available() > 0) {
if (gps.encode(Serial2.read())) {
if (gps.location.isValid()) {
Serial.print(F("- latitude: "));
Serial.println(gps.location.lat());

Serial.print(F("- longitude: "));
Serial.println(gps.location.lng());

Serial.print(F("- altitude: "));
if (gps.altitude.isValid())
Serial.println(gps.altitude.meters());
else
Serial.println(F("INVALID"));
} else {
Serial.println(F("- location: INVALID"));
}

Serial.print(F("- speed: "));
if (gps.speed.isValid()) {
Serial.print(gps.speed.kmph());
Serial.println(F(" km/h"));
} else {
Serial.println(F("INVALID"));
}

Serial.print(F("- GPS date&time: "));
if (gps.date.isValid() && gps.time.isValid()) {
Serial.print(gps.date.year());
Serial.print(F("-"));
Serial.print(gps.date.month());
Serial.print(F("-"));
Serial.print(gps.date.day());
Serial.print(F(" "));
Serial.print(gps.time.hour());
Serial.print(F(":"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
Serial.println(gps.time.second());
} else {
Serial.println(F("INVALID"));
}

Serial.println();
}
}

if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}

Things That Go Wrong

If no GPS data is received within 5 seconds of startup, the sketch prints a warning to check the wiring, which usually means TX and RX are swapped or the baud rate doesn't match the module. Location, altitude, speed, and date/time can each read INVALID for a while after power-up since the module needs a clear view of the sky to get a satellite fix, which can take a minute or more outdoors and longer or never indoors.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-gps