0
0
AutocadHow-ToBeginner · 4 min read

Arduino GPS Tracker Project: Simple Guide and Code Example

To create a GPS tracker with Arduino, connect a GPS module (like NEO-6M) to the Arduino's serial pins and use a library such as TinyGPS++ to read location data. The Arduino reads GPS signals and outputs coordinates via serial or display.
📐

Syntax

Here is the basic syntax to read GPS data using the TinyGPS++ library with Arduino:

  • #include <TinyGPS++.h>: Includes the GPS library.
  • TinyGPSPlus gps;: Creates a GPS object to parse data.
  • Serial1.begin(9600);: Starts communication with the GPS module (usually at 9600 baud).
  • while (Serial1.available() > 0) gps.encode(Serial1.read());: Reads incoming GPS data byte by byte.
  • gps.location.lat() and gps.location.lng(): Get latitude and longitude.
arduino
#include <TinyGPS++.h>

TinyGPSPlus gps;

void setup() {
  Serial.begin(115200);       // Serial monitor
  Serial1.begin(9600);        // GPS module serial
}

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

  if (gps.location.isUpdated()) {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
  }
}
💻

Example

This example reads GPS coordinates from a NEO-6M GPS module connected to Arduino's serial pins and prints latitude and longitude to the Serial Monitor.

arduino
#include <TinyGPS++.h>

TinyGPSPlus gps;

void setup() {
  Serial.begin(115200);       // Open serial monitor
  Serial1.begin(9600);        // GPS module connected to Serial1
  Serial.println("GPS Tracker Starting");
}

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

  if (gps.location.isUpdated()) {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
    Serial.println();
  }
}
Output
GPS Tracker Starting Latitude: 37.774929 Longitude: -122.419416 Latitude: 37.774930 Longitude: -122.419417 ... (updates as GPS data arrives)
⚠️

Common Pitfalls

  • Incorrect wiring: GPS modules usually use 3.3V or 5V power and specific RX/TX pins; swapping these can cause no data.
  • Wrong baud rate: GPS modules commonly use 9600 baud; setting a different rate prevents reading data.
  • Not waiting for GPS fix: GPS needs time to get satellite signals; reading data too soon returns invalid coordinates.
  • Using wrong serial port: Some Arduino boards have only one hardware serial; use SoftwareSerial if needed.
arduino
// Wrong way (no SoftwareSerial on Uno):
// Using Serial for both GPS and monitor causes conflict

void setup() {
  Serial.begin(9600);
  // Serial.begin(9600); // GPS also on Serial - conflict!
}

void loop() {
  if (Serial.available()) {
    // Data conflict, no GPS data
  }
}

// Right way (using SoftwareSerial):
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

SoftwareSerial gpsSerial(4, 3); // RX, TX pins
TinyGPSPlus gps;

void setup() {
  Serial.begin(115200);
  gpsSerial.begin(9600);
}

void loop() {
  while (gpsSerial.available()) {
    gps.encode(gpsSerial.read());
  }
  if (gps.location.isUpdated()) {
    Serial.print("Lat: ");
    Serial.println(gps.location.lat(), 6);
  }
}
📊

Quick Reference

Summary tips for Arduino GPS tracker:

  • Use TinyGPS++ library for easy GPS data parsing.
  • Connect GPS module RX to Arduino TX and TX to Arduino RX (or use SoftwareSerial pins).
  • Set GPS baud rate to 9600 unless your module specifies otherwise.
  • Wait for GPS fix before trusting coordinates.
  • Use Serial Monitor at 115200 baud to view output clearly.

Key Takeaways

Connect your GPS module correctly and use the right serial pins or SoftwareSerial.
Use the TinyGPS++ library to easily read and parse GPS data.
Set the GPS baud rate to 9600 and wait for a satellite fix before reading coordinates.
Print latitude and longitude to the Serial Monitor for easy tracking.
Avoid using the same serial port for GPS and debugging to prevent conflicts.