Thanks to a very versatile Input/Output matrix, it is quite simple to connect NMEA GPS modules to ESP32 MCUs. Not only ESP32 boards have 3 serial ports you can choose from, they can be assigned to almost any pin you want.

ESP32 with GPS and OLED display

In this example we will connect a popular Ublox NEO-M8N like Beitian BN-880 or BN-220 to a ESP32 development board and output current position on a USB serial port using Arduino IDE and TinyGPS++ library. Let's begin

Sketch for ESP32 and GPS

Let's include all of our libraries: TinyGPS++ and HardwareSerial

#include "types.h"
#include "TinyGPS++.h";
#include "HardwareSerial.h";

Then, let's assign variables and create TinyGPSPlus and HardwareSerial object called SerialGPS on hardware serial port 1of the ESP32.

TinyGPSPlus gps;
HardwareSerial SerialGPS(1);

Now, set's set up everything in a setup function. GPS will be connected with 9600bps and to pins:

  • Hardware Serial 1 RX - pin 16 - connect GPS TX pin to it
  • Hardware Serial 1 TX - pin 17 - connect GPS RX pin to it
void setup() {
        Serial.begin(115200); //Serial port of USB
        SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
}

When everything is configured, we can listen to data on SerialGPS and send it to TinyGPS++ and decoded data to USB serial port

void loop() {
    while (SerialGPS.available() >0) {
       gps.encode(SerialGPS.read());
    }

    Serial.print("LAT=");  Serial.println(gps.location.lat(), 6);
    Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
    Serial.print("ALT=");  Serial.println(gps.altitude.meters());
}

Full example is available here. It does other functions too!