Last mod: 2024.10.25

Raspberry Pi - GPS receiver

If you're looking for a simple and low-cost solution, combining a Raspberry Pi Zero with a GPS module is a great option. The Raspberry Pi Zero is super affordable and compact, making it perfect for projects where space and budget are important. Paired with a GPS module, it can be used for location tracking, navigation, or even as a DIY geolocating tool. The setup is pretty straightforward too.

Hardware

We need two devices:

  1. Raspberry Pi Zero (or other version)

Raspberry Pi Zero

  1. GPS USB receiver

GPS USB receiver - side A GPS USB receiver - side B

Raspberry Pi Zero OS

Select Raspberry Pi OS Lite (32-bit) on microSD card:

Select Raspberry Pi OS Lite

and write:

Write Raspberry Pi OS Lite

When we can connect over SSH to Raspberry we can go to next step.

GPS Receiver

Connect GPS receiver to Raspberry and list USB devices:

lsusb

and result:

Write Raspberry Pi OS Lite

On line:

Bus 001 Device 002: ID 1546:01a8 U-Blox AG [u-blox 8]

we can see our GPS signal receiver device.

Upgrade and install software

We need libraries to read and decode GPS signals, NMEA codes.

Verify Python version:

python --version

Python version

Upgrade and install software:

sudo apt update && sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update
sudo apt install python3-full python3-nmea2 python3-serial -y

Receive GPS commmands

Our GPS receiver must be placed outside the building, possibly in a window, but so that a minimum of 4 GPS satellites can be seen.

Raspberry and Gps receiver

From console list devices:

ls /dev/tty*

The list displayed should include the device:

/dev/ttyACM0

If the device is available, we can display NMEA messages received from GPS satellites on the console:

sudo cat /dev/ttyACM0

NMEA messages

It has to be said that these messages are not very clear to humans.

Using the installed libraries, let's write a simple script to decode position and height. Create file:

vi nmea-receiver.py

with body:

import io, serial, pynmea2

ser = serial.Serial('/dev/ttyACM0', 9600, timeout=10.0)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

while True:
    try:
        line = sio.readline()
        msg = pynmea2.parse(line)
        if isinstance(msg, pynmea2.types.talker.GGA):
           print('lat={}, lng={}, alt={}'.format(msg.latitude, msg.longitude, msg.altitude))

    except serial.SerialException as e:
        print('Exception: {}'.format(e))
        break

and run:

python3 nmea-receiver.py

After a few seconds, we should see latitude, longitude and altitude data:

Decoded NMEA data