StratoLab

Lesson 3: Temperature, Pressure, and Altitude

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 BMP-180 module.

  1. Wire the BMP-180 to the Raspberry Pi Pico. BMP-180 Pins | Description | Pi Pico Pins ———— | ———– | ———— VIN | (Voltage In): Provides power to the BMP-180. Connect to the 5V pin on Pico | 5V (40) GND | (Ground): Connect to the ground pin on Pico | GND (38) SCL | (Serial Clock): Accepts clock pulses from the Pico to synchronize data transmission | GP17 (22) SDA | (Serial Data): Used for data exchange | GP16 (21)

    bmp-180-diagram

    Lesson Three

Install BMP-180 Driver

Drivers are code modules for enabling certain functionality. One such driver allows us to read data from the BMP-180 module. This driver is called bmp180.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 bmp180.py located in the Raspberry Pi Pico /drivers/src/ location.

  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 bmp180.py in a previous step. Select the file and click Open.

  6. Save the bmp180.py file to the Raspberry Pi Pico. This allows our code to use the driver to perform BMP-180 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 bmp180.py file being sure to name it bmp180.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

BMP-180 Program

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

  1. Using Thonny, open the main.py file in Lesson 3: /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 the temperature, pressure, and altutide captured by the BMP-180.

    Example output:

     Temperature (C):	 21
     Pressure (Pa):  	 98420
     Altitude (m):   	 232
    

Congratulations! You have successfully completed Lesson 3.

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:

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

Challenge

Modify your code to use a function to initialize the BPM180 module.

This challenge will introduce you to using Python Functions. Using functions can simplify your code, offer greater readability, and align to the “DRY” (Don’t Repeat Yourself) concepts mentioned in earlier challenges. A good function is one with an obvious name based on its action and with predictable inputs and outputs. For example, a function named multiply_integer_by_10 would likely take in an integer, multiply it by 10, and output the result.

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

As you think through introducing this function into your code, think about how this same function can be used in later lessons. If the function is re-usable, can it be shared across more of our code base? Is the function “obvious” in its intent and what it outputs?

Expand to see an example using a Function to initialize the BMP180 module ```python from machine import Pin, I2C from drivers import bmp180 import time def initialize_bmp180_module(scl_pin_location, sda_pin_location): # Initialize I2C using available I2C pins bus = I2C(0, scl=Pin(scl_pin_location), # take SCL as input into the function and use it here sda=Pin(sda_pin_location), # take SDA as input into the function and use it here freq=100000) # Initialize BMP180 with previously defined I2C config new_bmp180 = bmp180.BMP180(bus) # Create a new bmp180 object. new_bmp180.oversample_sett = 2 # Accuracy, as defined in driver docs: https://github.com/micropython-IMU/micropython-bmp180 new_bmp180.baseline = 101325 # Baseline pressure, as defined in driver docs: https://github.com/micropython-IMU/micropython-bmp180 return new_bmp180 # return the bmp180 object. Once returned, we assign the object to a new value in our main function if __name__ == "__main__" : # Main entrypoint. Primary code functions start here. bmp180 = initialize_bmp180_module(17,16) # initialize the BMP180 using our function. # Assign it to a variable of your choosing. In this example we call it `bmp180` while True: temp = bmp180.temperature # Capture temperature, assign to `temp` variable p = bmp180.pressure # Capture pressure, assign to `p` variable altitude = bmp180.altitude # Capture altitude, assign to `altitude` variable # Print results to console print("Temperature (C):\t %d" % temp) print("Pressure (Pa): \t %d" % p) print("Altitude (m): \t %d" % altitude) print("") time.sleep(1) ```

Troubleshooting

Reference Material

Need help?

Watch the walk-through video for guidance!