StratoLab

Lesson 4: GPS Coordinates

Pre-requisites

Objectives

Results

Video Walk-through

In addition to the reading below, you can watch this video for guidance!

Steps

IMPORTANT Before wiring your Pico, UNPLUG IT FROM YOUR COMPUTER. If plugged in while wiring, you risk damaging the Pico or GT-U7 module.

  1. Wire the GT-U7 to the Raspberry Pi Pico. GT-U7 Pins | Description | Pi Pico Pins ———— | ———– | ———— VCC | (Voltage In): Provides power. Connect to the 5V pin on Pico | 5V (40) GND | (Ground): Connect to the ground pin on Pico | GND (38) PPS | (Pulse Per Second): Assists with synchronization | N/A TXD | (Transmit): Transmission pin used for serial communication | UART1 RX / GP5 (7) RXD | (Receive): Receiver pin used for serial communication | UART1 TX / GP4 (6)

    gtu7-diagram

    Lesson Four

Install GT-U7 Driver

Drivers are code modules for enabling certain functionality. One such driver allows us to read data from the GTU-7 module. This driver is called gtu7.py and is located in the Raspberry Pi Pico /drivers/src/ location. The following steps will result in saving this driver to the Raspberry Pi Pico so the driver can be used by our Python code.

  1. Download the driver called gtu7.py located in the Raspberry Pi Pico /drivers/src/ location to a directory/file/location of your choice. Remember where you downloaded the driver to for a later step.

  2. Connect your Raspberry Pi Pico to your computer using the USB cable.

  3. Open the Thonny IDE. Stop/Restart the backend to refresh the connection.

    stop-restart

    You should now see Raspberry Pi Pico displayed in the left-hand navigation of Thonny.If the “Files” window is not displaying add it from the View > Files menu.

    files-menu

  4. If one does not already exist, create a new directory in Thonny on the Raspberry Pi Pic called drivers.

    drivers-directory

  5. Using Thonny, select File then Open from the menu. Choose This Computer. Navigate to the location where you downloaded gtu7.py in step 1. Select the file and click Open.

  6. Save the gtu7.py file to the Raspberry Pi Pico. This allows our code to use the driver to perform GT-U7 actions in MicroPython when running on the Pi Pico.

    Click File then Save as…. Choose Raspberry Pi Pico. Double-click the drivers folder created in a previous step. Then save the gtu7.py file being sure to name it gtu7.py.

  7. If a file called __init__.py does not already exist in the /drivers folder, create a new file in Thonny called __init__.py.

    Click File then New. Then click File then Save as…. Choose Raspberry Pi Pico and save this empty file to the same drivers location as the previous step. Name the file __init__.py. This empty file is used by Python to indicate the drivers folder is to be used for Python modules.

    Your finished folders and files should look like this:
    files-menu

GT-U7 Program

The steps in this section will use the previous hardware and driver sections to allow reading temperature, pressure, and altitude from the GT-U7 module. The code example for this lesson is located in Lesson 4: /src/main.py.

  1. Using Thonny, open the main.py file in Lesson 4: /src/main.py.

  2. Run the script.

    run-script

    Output will be generated to the console in Thonny describing the actions being taken. You will see output for latitude, longitude, number of satellites, and time returned from the GPS module.

    Example output:

     Latitude: 	 4026.774
     Longitude: 	 -8902.08
     Satellites:  6
     Time:		 18:46:14
    

Is Your GPS Module Not Connecting?

In some locations, such as the center of a building or in a basement, the GPS module cannot receive a GPS signal. To aid in these scenarios, an update was made to the GTU7 module to support “fake” data returned similar to real data. This can be useful for testing purposes, or to gain familiarity with what to expect as a response.

If you want to generate fake data, use the following syntax for either GPGGA data or GPRMC data. Note the use of fake=True being passed in.

:information_source: TypeError: unexpected keyword argument ‘fake’
The fake data feature was added to the GTU7 module in an update. If you encounter the unexpected argument error, follow the steps earlier in the lesson on installing the GTU7 driver, ensuring you are using the latest copy of the driver located in this project.

gpgga_data = gps_module.gpgga(fake=True)
gprmc_data = gps_module.gprmc(fake=True)

Congratulations! You have successfully completed Lesson 4.



Want more?

If you have finished with the base lesson, check out the items below.

Update the code to do any/all of the following:

  1. Reformat the printed output to a format of your liking
  2. Reformat the printed output to be simply a list of the information
  3. Print out another module data point (Hint: look at the driver…) 😁

Things to think about, validate, and/or try:

Challenge

Modify your code to handle exceptions.

This challenge will introduce you to using Python Try Except code blocks for exception handling. The Try Except code block is very useful in handling errors and exceptions from functions or code attempts. In its basic implementation, this code block tries something, except if something unexpected happens (like an error), try something else. If all else fails, finally take one last action.

A successful implementation of this code will result in the following:

As you think through this code, also consider how it might be applied to previous lessons. Similarly, keep this code in mind for future lessons. It is a useful approach for offering more informed responses to errors, or alternatively taking new actions if an error is encountered.

Expand to see an example using a Try Catch to handle an exception ```python # Import all needed libraries from machine import Pin, UART import time from drivers import gtu7 if __name__ == "__main__" : # Define our UART(Universal Asynchronous Receiver/Transmitter) uart = UART(1, baudrate=9600, timeout=3600, tx=Pin(4), rx=Pin(5)) # Loop for GPS coordinates while True: gps_module = gtu7.GTU7(uart) gpgga_data = gps_module.gpgga() try: # Try printing the data. print("Latitude:\t{0}\nLongitude:\t{1}\nSatellites:\t{2}\nTime:\t\t{3}\n". format(gpgga_data[1], gpgga_data[2], gpgga_data[3], gpgga_data[0])) except IndexError as e: # We are capturing the specific `IndexError` exception. However, you can capture other # exceptions as well. View the Troubleshooting section for this lesson and think # about how you might add more exception handling print("No data returned from GPS module. Ensure wiring is correct.") time.sleep(5) ```

Troubleshooting

Need help?

Watch the walk-through video for guidance!