Bird
Raised Fist0
Raspberry Piprogramming~20 mins

GPS module data reading in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GPS Data Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of GPS NMEA sentence parsing
What is the output of this Python code that parses a GPS NMEA sentence to extract latitude and longitude?
Raspberry Pi
nmea_sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47"

parts = nmea_sentence.split(",")
lat_raw = parts[2]
lat_dir = parts[3]
lon_raw = parts[4]
lon_dir = parts[5]

lat_deg = int(lat_raw[:2])
lat_min = float(lat_raw[2:])
lon_deg = int(lon_raw[:3])
lon_min = float(lon_raw[3:])

latitude = lat_deg + lat_min / 60
longitude = lon_deg + lon_min / 60

if lat_dir == "S":
    latitude = -latitude
if lon_dir == "W":
    longitude = -longitude

print(f"Latitude: {latitude:.6f}, Longitude: {longitude:.6f}")
ALatitude: 48.117300, Longitude: 11.516667
BLatitude: 48.703800, Longitude: 113.100000
CLatitude: 4.807038, Longitude: 11.310000
DLatitude: 48.117300, Longitude: -11.516667
Attempts:
2 left
💡 Hint
Remember that latitude degrees are first two digits, longitude degrees are first three digits.
🧠 Conceptual
intermediate
1:00remaining
Understanding GPS data update frequency
A GPS module connected to a Raspberry Pi outputs NMEA sentences every second. What is the typical update frequency of GPS position data in Hertz (Hz)?
A1 Hz
B60 Hz
C0.1 Hz
D10 Hz
Attempts:
2 left
💡 Hint
One update per second means how many updates per second?
🔧 Debug
advanced
2:00remaining
Identify the error in GPS serial reading code
What error will this Python code raise when reading GPS data from serial port on Raspberry Pi?
Raspberry Pi
import serial

ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
line = ser.readline()
print(line.decode('utf-8'))
ser.close()
AAttributeError
Bserial.SerialException
CNo error, prints GPS data line
DUnicodeDecodeError
Attempts:
2 left
💡 Hint
Is the serial port and baud rate correct? Is decoding done properly?
❓ Predict Output
advanced
2:00remaining
Output of GPS coordinate conversion function
What is the output of this function converting NMEA latitude format to decimal degrees?
Raspberry Pi
def nmea_to_decimal(coord, direction):
    degrees = int(coord[:2])
    minutes = float(coord[2:])
    decimal = degrees + minutes / 60
    if direction in ['S', 'W']:
        decimal = -decimal
    return round(decimal, 6)

print(nmea_to_decimal('4807.038', 'N'))
A4807.038
B48.1173
C4.807038
DError: ValueError
Attempts:
2 left
💡 Hint
NMEA latitude format is DDMM.mmmm, so slice first 2 characters for degrees.
🧠 Conceptual
expert
1:30remaining
Choosing correct serial port for GPS on Raspberry Pi
Which serial port is typically used to connect a GPS module on a Raspberry Pi running Raspberry Pi OS with default settings?
A/dev/ttyAMA0
B/dev/ttyUSB0
C/dev/ttyS0
D/dev/serial0
Attempts:
2 left
💡 Hint
Raspberry Pi uses a symbolic link for the primary UART serial port.

Practice

(1/5)
1. What type of data does a GPS module typically send to a Raspberry Pi over a serial connection?
easy
A. Audio signals for navigation
B. Raw binary satellite signals
C. NMEA sentences containing location and time information
D. Image data from satellite cameras

Solution

  1. Step 1: Understand GPS data format

    GPS modules send data in text format called NMEA sentences, which include location, time, and other info.
  2. Step 2: Identify data type sent over serial

    The Raspberry Pi receives these NMEA sentences as strings over the serial port.
  3. Final Answer:

    NMEA sentences containing location and time information -> Option C
  4. Quick Check:

    GPS data = NMEA sentences [OK]
Hint: Remember GPS uses NMEA text sentences for data [OK]
Common Mistakes:
  • Thinking GPS sends raw binary signals directly
  • Confusing GPS data with audio or images
  • Assuming GPS sends only coordinates without time
2. Which Python library is commonly used on Raspberry Pi to read serial data from a GPS module?
easy
A. socket
B. serial
C. os
D. math

Solution

  1. Step 1: Identify library for serial communication

    The 'serial' library (pyserial) is used to read data from serial ports on Raspberry Pi.
  2. Step 2: Confirm other libraries' purposes

    'socket' is for network, 'os' for system operations, 'math' for calculations, so they don't handle serial data.
  3. Final Answer:

    serial -> Option B
  4. Quick Check:

    Serial data reading = serial library [OK]
Hint: Use 'serial' library for serial port reading [OK]
Common Mistakes:
  • Using 'socket' which is for network communication
  • Confusing 'os' or 'math' as serial libraries
  • Not installing pyserial before use
3. Given the Python code snippet reading GPS data, what will be printed if the GPS sends the sentence '$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47'?
import serial
ser = serial.Serial('/dev/ttyS0', 9600)
line = ser.readline().decode('ascii', errors='replace')
if line.startswith('$GPGGA'):
    print('GPS fix data:', line.strip())
else:
    print('Other data')
medium
A. GPS fix data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
B. Other data
C. SyntaxError
D. No output

Solution

  1. Step 1: Check if line starts with '$GPGGA'

    The line from GPS starts exactly with '$GPGGA', so the condition is True.
  2. Step 2: Print the GPS fix data line

    Since condition is True, it prints 'GPS fix data:' followed by the full line without trailing newline.
  3. Final Answer:

    GPS fix data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47 -> Option A
  4. Quick Check:

    Line startswith $GPGGA = print GPS fix [OK]
Hint: Check string start with $GPGGA to print GPS fix [OK]
Common Mistakes:
  • Ignoring the startswith condition
  • Expecting syntax errors from decode
  • Not stripping newline before print
4. Identify the error in this Python code snippet intended to read GPS data from serial port and fix it:
import serial
ser = serial.Serial('/dev/ttyS0', 9600)
line = ser.readline().decode('utf-8')
if line.startswith('$GPRMC'):
print('RMC data:', line)
medium
A. Indent the print statement inside the if block
B. Change decode to 'ascii' instead of 'utf-8'
C. Use ser.read() instead of ser.readline()
D. Remove the if condition

Solution

  1. Step 1: Check Python indentation rules

    The print statement must be indented inside the if block to run conditionally.
  2. Step 2: Confirm other parts are correct

    Using 'utf-8' decode and readline() is valid; removing if or changing decode is unnecessary.
  3. Final Answer:

    Indent the print statement inside the if block -> Option A
  4. Quick Check:

    Indentation error fixed by indenting print [OK]
Hint: Indent code inside if blocks properly [OK]
Common Mistakes:
  • Ignoring indentation causing syntax error
  • Changing decode unnecessarily
  • Removing important condition
5. You want to extract latitude and longitude from the $GPRMC sentence received from a GPS module on Raspberry Pi. Which Python code snippet correctly parses and prints latitude and longitude from this sentence? Example sentence: $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
hard
A. lat = parts[1] lon = parts[2] print(f'Lat: {lat}, Lon: {lon}')
B. lat = line[3:10] lon = line[11:20] print('Latitude:', lat, 'Longitude:', lon)
C. lat = line.split()[2] lon = line.split()[3] print(lat, lon)
D. parts = line.split(',') lat = parts[3] + ' ' + parts[4] lon = parts[5] + ' ' + parts[6] print(f'Latitude: {lat}, Longitude: {lon}')

Solution

  1. Step 1: Split the NMEA sentence by commas

    Splitting by ',' gives a list where latitude is at index 3 and 4 (value and N/S), longitude at 5 and 6 (value and E/W).
  2. Step 2: Combine latitude and direction, longitude and direction

    Concatenate parts[3] with parts[4] and parts[5] with parts[6] to get full coordinates.
  3. Final Answer:

    parts = line.split(',') lat = parts[3] + ' ' + parts[4] lon = parts[5] + ' ' + parts[6] print(f'Latitude: {lat}, Longitude: {lon}') -> Option D
  4. Quick Check:

    Split by comma, lat=3+4, lon=5+6 [OK]
Hint: Split sentence by commas, lat=3+4, lon=5+6 [OK]
Common Mistakes:
  • Using wrong indexes for latitude and longitude
  • Trying to slice string instead of splitting
  • Ignoring direction letters N/S and E/W
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard