Raspberry Pi - GPS
This tutorial instructs you how to use Raspberry Pi with a NEO-6M GPS module. In detail, we will learn:
- How to connect Raspberry Pi to a NEO-6M GPS module
- How to program Raspberry Pi to read GPS coordinates (longitude, latitude, and altitude) from a NEO-6M GPS module
- How to program Raspberry Pi to calculate the distance from the current GPS position to a predefined GPS coordinate (e.g. coordinates of London).
Apart from longitude, latitude, and altitude, Raspberry Pi is also able to read GPS speed (km/h), and date time from a NEO-6M GPS module.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of NEO-6M GPS module
NEO-6M GPS module is a GPS module that can provide the following information:
- Longitude
- Latitude
- Altitude
- GPS speed (km/h)
- Date time
The NEO-6M GPS Module Pinout
The NEO-6M GPS module has four pins:
- VCC pin: This should be connected to the VCC (3.3V or 5V)
- GND pin: This should be connected to GND (0V)
- TX pin: This is used for serial communication and should be connected to the Serial RX pin on Raspberry Pi.
- RX pin: This is used for serial communication and should be connected to the Serial TX pin on Raspberry Pi.

Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Code
Reading GPS coordinates, speed (km/h), and date time
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Enable the Serial interface on Raspberry Pi by following the instruction on Raspberry Pi - how to enable Serial inteface
- Install the pyserial library for communication with GPS module:
- Create a Python script file gps.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the result on the Terminal.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Calculating the distance from current location to a predefined location
The below code computes the distance between the current spot and London (latitude: 51.508131, longitude: -0.128002).
Detailed Instructions
- Install the geopy library for the distance calculation:
- Create a Python script file gps_distance.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the result on the Terminal.