Built-in project · Arduino Uno R3

GPS position on an Arduino Uno

A NEO-6M on an Uno with SoftwareSerial and TinyGPS++. It prints latitude, longitude, satellites and time once a second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $36. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino NEO-6M GPS: Wiring, TinyGPS++ and Code

GPS position · Arduino Uno R3live0.000 s 0.00x
Click the GPS to lose the fix, and again to get it back
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Serial text from a GPS module (NMEA sentences)
  • SoftwareSerial for a second serial port
  • Parsing data one character at a time with a library
  • Detecting missing data with timeouts
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The NEO-6M GPS module sends text sentences at 9600 baud, about once a second. The Uno listens on pin 4 using SoftwareSerial, so the hardware serial port stays free for the monitor.

Each character goes into the TinyGPS++ library, which decodes position, satellites, altitude and time. When a fresh position and altitude arrive together, the sketch prints one line.

Mokxi has no real sky. You move the module by changing its latitude and longitude in the properties panel. Clicking the module on the canvas drops and restores the fix.

Parts list

3 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
GPS module, NEO-6M pin TXArduino Uno R3 pin 4
GPS module, NEO-6M pin RXArduino Uno R3 pin 3
GPS module, NEO-6M pin VCCArduino Uno R3 pin 5V
GPS module, NEO-6M pin GNDArduino Uno R3 pin GND

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// GPS position: a NEO-6M on an Uno, the way the tutorials wire it.
//
//   NEO-6M TX  -> pin 4   (the sketch's RX)
//   NEO-6M RX  <- pin 3   (the sketch's TX; the module ignores it here)
//   NEO-6M VCC -> 5V, GND -> GND
//
// The module sends NMEA sentences at 9600 baud, once a second. TinyGPS++
// reads them one character at a time, and the sketch prints the position
// each time a new one arrives.
//
// The serial monitor runs at 115200, as in the TinyGPS++ examples. Mokxi's
// Serial has no send buffer: it waits for each byte to leave. At 9600 a
// printed line takes about 70 ms, the GPS keeps talking meanwhile, and
// SoftwareSerial's 64-byte buffer overflows: the sentence with the satellite
// count and the altitude is the one that gets lost. A real Uno buffers its
// Serial output, so a short line costs far less there; a long print or a long
// delay() can still make it miss bytes.
//
// There is no sky here. Mokxi does not model satellites or a radio. The
// position is a property of the module: click it and change lat and lon in
// the properties panel to move it. Click the module on the canvas to lose the
// fix, and click again to get it back.

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);

void setup() {
  Serial.begin(115200);
  gpsSerial.begin(GPSBaud);
  Serial.println("Waiting for the GPS...");
}

void twoDigits(int value) {
  if (value < 10) Serial.print('0');
  Serial.print(value);
}

void showPosition() {
  Serial.print("Lat: ");
  Serial.print(gps.location.lat(), 6);
  Serial.print("  Lng: ");
  Serial.print(gps.location.lng(), 6);
  Serial.print("  Sats: ");
  Serial.print(gps.satellites.value());
  Serial.print("  Alt: ");
  Serial.print(gps.altitude.meters(), 1);
  Serial.print(" m  UTC ");
  twoDigits(gps.time.hour());
  Serial.print(':');
  twoDigits(gps.time.minute());
  Serial.print(':');
  twoDigits(gps.time.second());
  Serial.println();
}

// Every character the module sent since last time, into the parser.
void readGps() {
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
  }
}

unsigned long lastLine = 0;
bool warned = false;

void loop() {
  readGps();
  // The GGA sentence brings the position, the satellites and the altitude
  // together, once a second. (RMC updates the position too, a moment
  // earlier, but carries no altitude.)
  if (gps.location.isUpdated() && gps.altitude.isUpdated()) {
    showPosition();
    lastLine = millis();
  }
  // Sentences but no position: the module has no fix.
  if (gps.charsProcessed() > 10 && millis() - lastLine > 3000) {
    Serial.println("No fix yet: waiting for satellites.");
    lastLine = millis();
  }
  // Nothing at all after five seconds is a wiring fault, not a sky fault.
  if (!warned && millis() > 5000 && gps.charsProcessed() < 10) {
    Serial.println("No data from the GPS: check TX goes to pin 4.");
    warned = true;
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts the monitor at 115200 and the GPS port at GPSBaud (9600). Then it prints "Waiting for the GPS...".

void setup() {
  Serial.begin(115200);
  gpsSerial.begin(GPSBaud);
  Serial.println("Waiting for the GPS...");
}

readGps sketch.ino

Reads every character waiting in the GPS buffer and passes each one to gps.encode. This must run often. If the sketch is slow, the small SoftwareSerial buffer overflows and data is lost.

// Every character the module sent since last time, into the parser.
void readGps() {
  while (gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
  }
}

showPosition sketch.ino

Prints latitude and longitude with 6 decimals, the satellite count, altitude in meters, and the UTC time. It uses twoDigits so a time like 7:05 prints as 07:05.

void showPosition() {
  Serial.print("Lat: ");
  Serial.print(gps.location.lat(), 6);
  Serial.print("  Lng: ");
  Serial.print(gps.location.lng(), 6);
  Serial.print("  Sats: ");
  Serial.print(gps.satellites.value());
  Serial.print("  Alt: ");
  Serial.print(gps.altitude.meters(), 1);
  Serial.print(" m  UTC ");
  twoDigits(gps.time.hour());
  Serial.print(':');
  twoDigits(gps.time.minute());
  Serial.print(':');
  twoDigits(gps.time.second());
  Serial.println();
}

loop sketch.ino

Reads the GPS, then prints a line when both location and altitude have updated. If characters arrive but no position for 3 seconds, it reports no fix. If almost nothing arrives after 5 seconds, it points to a wiring fault.

void loop() {
  readGps();
  // The GGA sentence brings the position, the satellites and the altitude
  // together, once a second. (RMC updates the position too, a moment
  // earlier, but carries no altitude.)
  if (gps.location.isUpdated() && gps.altitude.isUpdated()) {
    showPosition();
    lastLine = millis();
  }
  // Sentences but no position: the module has no fix.
  if (gps.charsProcessed() > 10 && millis() - lastLine > 3000) {
    Serial.println("No fix yet: waiting for satellites.");
    lastLine = millis();
  }
  // Nothing at all after five seconds is a wiring fault, not a sky fault.
  if (!warned && millis() > 5000 && gps.charsProcessed() < 10) {
    Serial.println("No data from the GPS: check TX goes to pin 4.");
    warned = true;
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • "No data from the GPS: check TX goes to pin 4." This project
    The module's TX pin must go to Arduino pin 4. TX and RX are labeled from the module's side, so they cross over.
  • Text in the monitor is garbled This project
    Set the serial monitor to 115200 to match Serial.begin.
  • "No fix yet" keeps repeating This project
    The module is sending data but has no position. In Mokxi, click the module to restore the fix.
  • Garbage text or no data from the module. GPS module
    The default is 9600 baud. Connect module TX to the board RX, and RX to TX. Set the serial speed to match. Use SoftwareSerial pins on an Uno.
  • No fix is ever reported. GPS module
    A real NEO-6M needs a clear sky view and up to several minutes for a cold start. Indoors it usually never fixes. Keep it powered so it stays warm.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.