Projects / Read GPS sentences over UART

Read GPS sentences over UART

Beginner45 minGuided buildReviewed

Build it step by step

0 of 7 done
  1. UNO

    Wire the module

    Connect VCC, GND and TX to D4.

  2. UNO

    Upload with RAW true

    Watch the raw sentences scroll on the serial monitor.

  3. UNO

    Find the GGA line

    It starts with GPGGA or GNGGA.

  4. UNO

    Split by commas

    Compare the fields with the code.

  5. UNO

    Switch RAW to false

    See the readable time and position.

  6. UNO

    Check the position

    Paste the latitude and longitude into a map.

  7. UNO

    Wait for a fix

    Move outdoors if the satellite count stays at zero.

In short

How do you build a read GPS sentences over UART?

Read raw NMEA sentences from a GPS module and pull out time, latitude and longitude. You need Arduino Uno R3, GPS module NEO-6M, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 45 minutes.

Level
Beginner
Time
About 45 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
GPS VCCUno 5VPower
GPS GNDUno GNDGround
GPS TXUno D4Receives NMEA text
GPS RXUno D3Optional, only to configure the module

Code

NMEA sentences are text lines starting with a dollar sign and a talker code, with comma-separated fields. In the GGA sentence field 1 is the time, 2 and 3 are latitude and its hemisphere, 4 and 5 longitude and hemisphere, 6 the fix quality and 7 the number of satellites. Latitude is in degrees and minutes (ddmm.mmmm), so the code converts to decimal degrees you can paste into a map. The module needs a clear view of the sky and the first fix can take several minutes, so test by a window or outdoors. The blue LED on the module blinks once a fix is found. Position data is personal information, so avoid sharing logs that show your home. Arduino IDE, board "Arduino Uno" (or "Arduino Nano" with processor ATmega328P).

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Parse GGA sentences from a NEO-6M GPS without a library, to see how NMEA works.
#include <SoftwareSerial.h>

SoftwareSerial gps(4, 3);   // RX, TX
String line = "";
const bool RAW = false;     // true: print every sentence as received

String field(const String &s, int n) {
  int start = 0;
  for (int i = 0; i < n; i++) { start = s.indexOf(',', start) + 1; if (start == 0) return ""; }
  int end = s.indexOf(',', start);
  if (end < 0) end = s.length();
  return s.substring(start, end);
}

float toDegrees(const String &v, const String &hemi) {   // ddmm.mmmm to decimal degrees
  if (v.length() < 4) return 0;
  float x = v.toFloat();
  int deg = (int)(x / 100);
  float d = deg + (x - deg * 100) / 60.0;
  return (hemi == "S" || hemi == "W") ? -d : d;
}

void handle(const String &s) {
  if (RAW) Serial.println(s);
  if (s.startsWith("$GPGGA") || s.startsWith("$GNGGA")) {
    String fix = field(s, 6);
    if (fix == "0" || fix == "") { Serial.println("No fix yet, look at the sky"); return; }
    String t = field(s, 1);
    Serial.print("Time UTC ");
    Serial.print(t.substring(0, 2)); Serial.print(":");
    Serial.print(t.substring(2, 4)); Serial.print(":");
    Serial.println(t.substring(4, 6));
    Serial.print("Lat "); Serial.println(toDegrees(field(s, 2), field(s, 3)), 6);
    Serial.print("Lon "); Serial.println(toDegrees(field(s, 4), field(s, 5)), 6);
    Serial.print("Satellites "); Serial.println(field(s, 7));
  }
}

void setup() {
  Serial.begin(9600);
  gps.begin(9600);
}

void loop() {
  while (gps.available()) {
    char c = gps.read();
    if (c == '\n') { handle(line); line = ""; }
    else if (c != '\r' && line.length() < 120) line += c;
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Indoors at firstRaw sentences but no fix
Outdoors after a few minutesTime, latitude, longitude and satellites
Latitude and longitudeMatch your place on a map
SatellitesFour or more for a good fix

If it does not work

Nothing printed

Check the TX wire goes to D4 and baud is 9600.

Garbage

Baud mismatch; check 9600.

No fix

Go outdoors with a clear sky and wait.

Time is not local

It is UTC; add 5 hours 30 minutes for India.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the read GPS sentences over UART?

Arduino Uno R3, GPS module NEO-6M, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the read GPS sentences over UART take to build?

About 45 minutes for a beginner maker, in 7 steps.

How do I know the read GPS sentences over UART works?

Indoors at first: Raw sentences but no fix. Outdoors after a few minutes: Time, latitude, longitude and satellites. Latitude and longitude: Match your place on a map.

Nothing printed: what should I check?

Check the TX wire goes to D4 and baud is 9600.

Garbage: what should I check?

Baud mismatch; check 9600.

No fix: what should I check?

Go outdoors with a clear sky and wait.

Time is not local: what should I check?

It is UTC; add 5 hours 30 minutes for India.