How to Use GPS Module with Raspberry Pi: Setup and Code
To use a
GPS module with a Raspberry Pi, connect the module's TX and RX pins to the Pi's UART pins, enable the serial interface, and use a Python library like gpsd or pyserial to read GPS data. You can parse the NMEA sentences to get location, time, and speed information.Syntax
Here is the basic Python syntax to read GPS data from the serial port on Raspberry Pi:
import serial: Imports the serial communication library.serial.Serial(port, baudrate): Opens the serial port connected to the GPS module.readline(): Reads one line of GPS data (NMEA sentence).decode(): Converts bytes to string for parsing.
python
import serial # Open serial port (usually /dev/serial0 or /dev/ttyAMA0) ser = serial.Serial('/dev/serial0', 9600, timeout=1) while True: line = ser.readline().decode('ascii', errors='replace') print(line.strip())
Example
This example shows how to read and print raw GPS NMEA sentences from the GPS module connected to Raspberry Pi's UART serial port.
python
import serial import time # Setup serial connection to GPS module ser = serial.Serial('/dev/serial0', 9600, timeout=1) try: while True: line = ser.readline().decode('ascii', errors='replace').strip() if line.startswith('$GPGGA'): print('GPS Fix Data:', line) time.sleep(1) except KeyboardInterrupt: ser.close() print('Serial connection closed.')
Output
GPS Fix Data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
GPS Fix Data: $GPGGA,123520,4807.039,N,01131.001,E,1,08,0.9,545.5,M,46.9,M,,*48
... (continues every second)
Common Pitfalls
- Wrong serial port: Raspberry Pi uses
/dev/serial0or/dev/ttyAMA0for UART; check which one your Pi uses. - Serial interface disabled: Enable UART in Raspberry Pi configuration using
raspi-config. - Conflicts with Bluetooth: On some Pi models, Bluetooth uses UART; disable Bluetooth or use a USB GPS module.
- Incorrect baud rate: Most GPS modules use 9600 baud; verify your module's specs.
- Parsing errors: GPS data is in NMEA format; use libraries or carefully parse strings.
python
## Wrong way: Using wrong serial port or baud rate import serial ser = serial.Serial('/dev/ttyUSB0', 4800) # Wrong port and baud rate # Right way: ser = serial.Serial('/dev/serial0', 9600)
Quick Reference
Summary tips for using GPS module with Raspberry Pi:
- Connect GPS TX to Pi RX (GPIO15), GPS RX to Pi TX (GPIO14).
- Enable UART via
sudo raspi-config> Interface Options > Serial. - Use
/dev/serial0as the serial port in code. - Set baud rate to 9600 unless your GPS module specifies otherwise.
- Parse NMEA sentences like
$GPGGAfor location data.
Key Takeaways
Connect GPS module TX/RX pins correctly to Raspberry Pi UART pins and enable UART interface.
Use Python's serial library to read GPS NMEA sentences from /dev/serial0 at 9600 baud.
Parse NMEA sentences like $GPGGA to extract useful GPS data such as latitude and longitude.
Check and disable Bluetooth if it conflicts with UART on Raspberry Pi models with built-in Bluetooth.
Always verify the correct serial port and baud rate to avoid connection issues.