How to connect GPS to ESP32

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.

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 header and setup routine

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 serial port 1.

TinyGPSPlus gps;
HardwareSerial SerialGPS(1);

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

  • Serial 1 RX – pin 16 – connect GPS TX pin to it
  • 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!

3 thoughts to “How to connect GPS to ESP32”

  1. Hi,
    I am running the given code but even after blinking of LED on the GPS module only zeros are printed on the serial monitor.

  2. Hi Pawel, I am attempting to transfer a working ESP32 DEV Module sketch that receives GPS data and shares (Tx & RX) their coordinates via LoRa RN2903 P2P Radios utilizing Arduino’s SoftwareSerial to HardwareSerial for efficiency.

    As a slow step in transfering to Arduino HardwareSerial, I started working with your simple example sketch. When I compile your ESP32 with GPS using HardwareSerial Sketch, Arduino IDE 1.8.12 returns the following error:

    In file included from C:\Users\Paul\Documents\Arduino\Sketches\ESP32_GPS_HardwareSerial\ESP32_GPS_HardwareSerial.ino:3:0:

    C:\Users\Paul\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4/tools/sdk/include/micro-ecc/types.h:59:56: note: #pragma message: uECC_WORD_SIZE must be 4 for ARM

    #pragma message (“uECC_WORD_SIZE must be 4 for ARM”)

    What is the reference to uECC_Word_Size and where do I set it to the required value of 4?

    1. Hello Pawel, my mistake, I assumed the error message would not allow the sketch to work????? You guessed it, it does work!!!! However, how do I correct the error message to clean things up????

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.