Example Python3 Scripts for Adafruit MPL115a2 and the si7021

Next on my list is the Altimeter, which I’m just using for a barometer in my basement. I’ve got the part wired to my Raspberry as you can see in my last post. I scraped this script off Adafruit:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
import adafruit_mpl115a2

i2c = busio.I2C(board.SCL, board.SDA)

mpl = adafruit_mpl115a2.MPL115A2(i2c)

while True:
    print("Pressure: {}   Temperature: {}".format(mpl.pressure, mpl.temperature))
    time.sleep(1)

Give it a quick run:

pi@basement:~ $ python3 alt.py
Traceback (most recent call last):
  File "/home/pi/alt.py", line 7, in <module>
    import adafruit_mpl115a2
ModuleNotFoundError: No module named 'adafruit_mpl115a2'

… and immediately go down in flames. But wait, maybe I can just install the missing module!

pi@basement:~ $ pip3 install Adafruit_MPl115a2
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
ERROR: Could not find a version that satisfies the requirement Adafruit_MPl115a2
ERROR: No matching distribution found for Adafruit_MPl115a2

Nope. Wait a minute! We’ve been here before! Ya’ gotta give the right name for what you want! Do over!

pi@basement:~ $ pip3 install adafruit-circuitpython-mpl115a2
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting adafruit-circuitpython-mpl115a2
  Downloading https://www.piwheels.org/simple/adafruit-circuitpython-mpl115a2/adafruit_circuitpython_mpl115a2-1.1.8-py3-none-any.whl (5.7 kB)
Requirement already satisfied: adafruit-circuitpython-busdevice in /usr/local/lib/python3.9/dist-packages (from adafruit-circuitpython-mpl115a2) (5.1.2)
Requirement already satisfied: Adafruit-Blinka in /usr/local/lib/python3.9/dist-packages (from adafruit-circuitpython-mpl115a2) (6.20.1)
Requirement already satisfied: Adafruit-PlatformDetect>=3.13.0 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (3.19.5)
Requirement already satisfied: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (4.3.2)
Requirement already satisfied: pyftdi>=0.40.0 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (0.53.3)
Requirement already satisfied: RPi.GPIO in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (0.7.1)
Requirement already satisfied: sysv-ipc>=1.1.0 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (1.1.0)
Requirement already satisfied: Adafruit-PureIO>=1.1.7 in /usr/local/lib/python3.9/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (1.1.9)
Requirement already satisfied: pyusb!=1.2.0,>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (1.2.1)
Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-mpl115a2) (3.5b0)
Installing collected packages: adafruit-circuitpython-mpl115a2
Successfully installed adafruit-circuitpython-mpl115a2-1.1.8

The cool thing about pip3 (once you get the name right) is that it makes sure you’ve got everything else installed (prerequisites) before it installs what you asked for. Now, let’s run our little program again.

pi@basement:~ $ python3 alt.py
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271
Pressure: 500.0   Temperature: 118.08411214953271

OK. I’m thinking that this sensor is fried. Let’s move on to the si7021. First, let’s install the library, NOW THAT WE KNOW HOW TO NAME IT!

pip3 install adafruit-circuitpython-si7021

And then, we’ll run this sample python3 script to poll the si7021:

#!/usr/bin/python3

import time
import board
import adafruit_si7021

sensor = adafruit_si7021.SI7021(board.I2C())

while True:
    print("\nTemperature: %0.1f C" % sensor.temperature)
    print("Humidity: %0.1f %%" % sensor.relative_humidity)
    time.sleep(2)

And give it a go:

pi@basement:~ $ python3 temp.py

Temperature: 20.0 C
Humidity: 49.5 %

Temperature: 20.0 C
Humidity: 49.5 %

Temperature: 20.0 C
Humidity: 49.5 %

Looks good! Those readings seem a little more accurate for the basement.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.