Connect the GPS module to the Raspberry Pi Pico as follows:
- Connect the VCC pin of the GPS module to the 3.3V pin on the Pico.
- Connect the GND pin of the GPS module to the GND pin on the Pico.
- Connect the TX pin of the GPS module to any GPIO pin1 on the Pico
- Connect the RX pin of the GPS module to any GPIO pin0 on the Pico
Note: Since Raspberry Pi Pico operates at 3.3V logic level, you can directly connect the GPS module without voltage level shifting.
The module will output something like this
$GPRMC,103255.00,V,,,,,,,180523,,,N*70 $GPVTG,,,,,,,,,N*30 $GPGGA,103255.00,,,,,0,00,99.99,,,,,,*66 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30 $GPGSV,1,1,01,18,,,31*73 $GPGLL,,,,,103255.00,V,N*4A
These are the NMEA messages. by decoding these messages we get our data.
You can read about them at http://aprs.gids.nl/nmea/ $GPGGA - Global Positioning System Fix Data $GPGLL - Geographic position, latitude / longitude $GPGSA - GPS DOP and active satellites $GPGSV - GPS Satellites in view $GPRMC - Recommended minimum specific GPS/Transit data $GPVTG - Track made good and ground speed
Micropython Code
import machine
import utime
# UART configuration
uart = machine.UART(0, baudrate=9600, tx=machine.Pin(0), rx=machine.Pin(1))
date = ""
time = ""
altitude = ""
course = ""
speed = ""
sats = ""
hdop = ""
latitude = ""
latitude_dir = ""
latitude_decimal =""
longitude = ""
longitude_dir = ""
longitude_decimal = ""
fix = ""
def dms_to_decimal(degrees, minutes, seconds):
degrees = int(degrees)
minutes = int(minutes)
seconds = float(seconds)
decimal = degrees + (minutes / 60) + (seconds / 3600)
return decimal
while True:
if uart.any():
data = uart.readline()
if data:
try:
# Decode the GPS data
data_str = data.decode('utf-8').strip()
if data_str.startswith('$GPRMC'):
values = data_str.split(',')
if len(values) == 13:
date = values[9][0:2] + '/' + values[9][2:4] + '/' + values[9][4:]
#print(date)
if data_str.startswith('$GPVTG'):
values = data_str.split(',')
if len(values) >= 9:
#print(values)
course = values[1] # Course over ground
speed = values[7] # Speed over ground
if data_str.startswith('$GPGGA'):
# Split the data string into individual values
values = data_str.split(',')
#print(values)
if len(values) >= 15:
# Extract the required values
sats = values[7] if values[7] else '' # Number of satellites
hdop = values[8] if values[8] else '' # HDOP
latitude = values[2][:2] + '°' + values[2][2:4] + "'" + values[2][4:] + '"' + values[3] # Latitude
latitude_dir = values[3]
longitude = values[4][:3] + '°' + values[4][3:5] + "'" + values[4][5:] + '"' + values[5] # Longitude
longitude_dir = values[5]
latitude_decimal = dms_to_decimal((values[2][:2]),(values[2][2:4]),(values[2][4:]))
longitude_decimal = dms_to_decimal(values[4][:3],values[4][3:5],values[4][5:])
fix = values[6] if values[6] else '' # Fix status
time = values[1][0:2] + ':' + values[1][2:4] + ':' + values[1][4:6] # Time
altitude = values[9] if values[9] else '' # Altitude
except UnicodeError:
pass
utime.sleep(0.1)
# Print the data
print(f"Sats: {sats} HDOP: {hdop}")
print("Latitude:", latitude)
print("Longitude:", longitude)
print(f"Latitude: {latitude_decimal} {latitude_dir} \t Longitude: {longitude_decimal} {longitude_dir}")
print("Fix:", fix)
print(f"Time: {time} Date: {date}")
print(f"Altitude: {altitude}\n")
# Print the GPVTG data
print(f"GPVTG: Course: {course} Speed:{speed} km/h")
Output
GPVTG: Course: 49.43 Speed:2.164 km/h Sats: 06 HDOP: 1.39 Latitude: 28°36'.51385"N Longitude: 077°03'.89436"E Latitude: 28.60014 N Longitude: 77.05025 E Fix: 1 Time: 14:24:37 Date: 18/05/23 Altitude: 216.4
Leave a Reply