GPS tracker module – GPS tracking with the Raspberry Pi

Raspberry Pi GPS Tracking – NEO-6M Setup, gpsd and Python Logger

With a GPS module, a Raspberry Pi, and a power bank you can build a compact GPS tracker – perfect for your car, bike rides, or hikes. In this guide I’ll walk you through wiring the NEO-6M/NEO-8M GPS module, setting up gpsd on Raspberry Pi OS, and reading and logging GPS data. For the full setup, including the Raspberry Pi 5 and USB GPS receivers, see our guide How to Use a GPS Module with a Raspberry Pi.

Materials

Links marked with * are affiliate links, which makes them advertising. If you buy through one, I earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Learn more

Wire the GPS Module

The GPS module communicates via UART (serial interface). The GY-NEO6MV2 has just four pins, and the wiring is the same on every Raspberry Pi model:

GPS Module Pin Raspberry Pi Pin Description
VCC PIN 1 (3.3V) Power supply
GND PIN 6 (GND) Ground
TX (GPS) PIN 10 (RX / GPIO15) Data GPS → Pi
RX (GPS) PIN 8 (TX / GPIO14) Data Pi → GPS (optional)

Enable UART and Disable Serial Console

The easiest way is raspi-config:

sudo raspi-config

Go to 3 Interface Options → I6 Serial Port. Answer No when asked about a login shell over serial and Yes when asked whether the serial port hardware should be enabled. Then reboot:

sudo reboot

The GPS module is now available at /dev/serial0. On a Pi 5 or Pi 500, use /dev/ttyAMA0 instead, because /dev/serial0 points to the dedicated debug connector there. At 9600 baud, the mini UART on the Pi 3, Pi 4 and Zero 2 W is perfectly fine for GPS, so there’s no need to disable Bluetooth. For background on the UARTs of each model, see Using UART on the Raspberry Pi.

Install and Configure gpsd

gpsd is the standard daemon for GPS devices on Linux:

sudo apt update
sudo apt install gpsd gpsd-clients -y

Edit the configuration:

sudo nano /etc/default/gpsd

Change these two lines (on a Pi 5 or Pi 500, use DEVICES="/dev/ttyAMA0"):

DEVICES="/dev/serial0"
GPSD_OPTIONS="-n"

Enable gpsd permanently and restart it with the new settings:

sudo systemctl enable gpsd
sudo systemctl restart gpsd

Test GPS Reception

The text interface cgps shows current GPS data:

cgps -s

On first start it can take up to 5 minutes for the GPS module to get a fix (line of sight to at least 4 satellites is required – best done outdoors). The module will then show latitude, longitude, speed, and altitude.

Alternatively, use gpspipe for simple NMEA output:

gpspipe -r | head -20

Read GPS Data with Python

The gps Python library from the python3-gps package reads the data straight from gpsd:

sudo apt install python3-gps -y

Simple Python script to read GPS data:

#!/usr/bin/env python3
import gps

session = gps.gps(mode=gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

try:
    while True:
        report = session.next()
        if report['class'] == 'TPV':
            lat = getattr(report, 'lat', 'n/a')
            lon = getattr(report, 'lon', 'n/a')
            speed = getattr(report, 'speed', 'n/a')
            print(f"Lat: {lat}, Lon: {lon}, Speed: {speed} m/s")
except KeyboardInterrupt:
    print("Stopped")
finally:
    session.close()

The loop doesn’t need a sleep: session.next() waits for the next report from gpsd.

Record a GPS Track (GPX Format)

gpxlogger from the gpsd-clients package writes GPS tracks directly in GPX format:

gpxlogger -f ~/track-$(date +%Y%m%d).gpx

You can then visualize GPX files in OpenStreetMap, Google Maps, or tools like GPXSee.

Get Time from GPS (Offline Operation)

Without an internet connection, the Pi can take its time from the GPS data instead. The GY-NEO6MV2 sends the time over the same UART link as the position. The board has no PPS pin for the pulse-per-second signal, so the clock is only accurate to a fraction of a second. That’s fine for timestamps in logs and recorded data. If you need microsecond accuracy, use a module with a PPS output. Our guide to building a GPS time server shows how.

Install the chrony time service:

sudo apt install chrony -y

Open its configuration file:

sudo nano /etc/chrony/chrony.conf

Add this line at the end:

refclock SHM 0 refid GPS precision 1e-1 delay 0.2

gpsd passes the time to chrony through this shared memory interface. Then restart chrony:

sudo systemctl restart chrony

Once the module has a fix, check whether chrony is using the GPS time:

chronyc sources -v

Offline, the GPS line should be marked with * after a few minutes. With an internet connection, chrony usually prefers the more accurate network time servers and only falls back to GPS when the connection drops.

Autostart the GPS Logger

Use a cron job to start recording automatically at boot. First create a folder for the tracks, then open your user’s crontab:

mkdir -p ~/tracks
crontab -e
@reboot sleep 15 && gpxlogger -r -f $HOME/tracks/track-$(date +\%Y\%m\%d_\%H\%M).gpx

The sleep 15 gives gpsd time to initialize the GPS module. The backslashes before the percent signs are required because cron otherwise treats % as a newline. The -r option keeps gpxlogger running when gpsd goes quiet for a few seconds, for example after gpsd restarts. Without it, the logger exits and records nothing until the next reboot.

Frequently asked questions about a Raspberry Pi GPS tracker

Can I track the position live on a map?

Not with this setup: the Pi saves the track as a GPX file, and you look at it after the trip. For live tracking, the Pi needs an internet connection on the road, for example through a USB mobile dongle, plus a server that receives the positions. The open-source Traccar server accepts positions through a simple HTTP request (OsmAnd protocol, port 5055).

How accurate is the NEO-6M?

The u-blox datasheet specifies 2.5 m CEP with a clear view of the sky: half of all fixes land within 2.5 m of the true position. Between tall buildings, under trees or inside a car, expect much larger errors.

How often does gpxlogger save a point?

Every time gpsd reports a fix. Out of the box, the NEO-6M delivers one position per second. To log fewer points, set a minimum distance in meters with -m, for example gpxlogger -m 5 -f track.gpx. Waiting at a red light then no longer fills your track with points.

Why doesn’t the module get a fix in my car?

Many newer cars have a windshield with a heat-reflecting metal coating that blocks much of the GPS signal. Place the module behind the uncoated area the manufacturer leaves for this, usually near the rear-view mirror and marked by a black dot pattern. To check, try it at a side window.

Similar Posts