In addition to the reading below, you can watch this video for guidance!
IMPORTANT Before wiring your Pico, UNPLUG IT FROM YOUR COMPUTER. If plugged in while wiring, you risk damaging the Pico or BMP-180 module.
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)
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.
Download the driver called bmp180.py
located in the Raspberry Pi Pico /drivers/src/ location.
Connect your Raspberry Pi Pico to your computer using the USB cable.
Open the Thonny IDE. Stop/Restart the backend to refresh the connection.
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.
If one does not already exist, create a new directory in Thonny on the Raspberry Pi Pic called drivers
.
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.
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
.
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:
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.
Using Thonny, open the main.py
file in Lesson 3: /src/main.py.
Run the 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.
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:
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:
GP
pin locations for SCL
and SDA
.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?
ERROR: No module named (drivers, bmp180, ...)
If you see this error it means Python is not able to locate a module to be imported. This can occur because the version of MicroPyhon you are using does not support the module you are trying to import. Specifically for this lesson it likely applies to the drivers
step. Ensure the drivers
folder and its contents, bmp180.py
and __init__.py
, are saved to the Raspberry Pi Pico device and not your computer.
Example error message:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ImportError: no module named 'drivers'
OSError: [Errno 5] EIO
If you see this error it means you likely have incorrect or loose wiring. Double-check the location of your wires whether those wires are loose.
Example error message:
Traceback (most recent call last):
File "<stdin>", line 15, in <module>
File "drivers/bmp180.py", line 48, in __init__
OSError: [Errno 5] EIO
OValueError: bad [SCL | SDA] pin
If you see this error it means you likely have incorrectly defined your SCL or SDA pin location in your code. You may have your module wired to a location on your Pico, but define the GP pins in your code to point to a different location. Verify your code matches with the GP
pin locations in your Pi Pico pinout diagram.
Example error message:
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
ValueError: bad [SDA | SCL] pin
Watch the walk-through video for guidance!