Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2019 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of the MPL3115A2 sensor.
5# Will read the pressure and temperature and print them out every second.
6import time
7import board
8import adafruit_mpl3115a2
9
10
11# Create sensor object, communicating over the board's default I2C bus
12i2c = board.I2C() # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14
15# Initialize the MPL3115A2.
16sensor = adafruit_mpl3115a2.MPL3115A2(i2c)
17# Alternatively you can specify a different I2C address for the device:
18# sensor = adafruit_mpl3115a2.MPL3115A2(i2c, address=0x10)
19
20# You can configure the pressure at sealevel to get better altitude estimates.
21# This value has to be looked up from your local weather forecast or meteorological
22# reports. It will change day by day and even hour by hour with weather
23# changes. Remember altitude estimation from barometric pressure is not exact!
24# Set this to a value in hectopascals:
25sensor.sealevel_pressure = 1022.5
26
27# Main loop to read the sensor values and print them every second.
28while True:
29 pressure = sensor.pressure
30 print("Pressure: {0:0.3f} hectopascals".format(pressure))
31 altitude = sensor.altitude
32 print("Altitude: {0:0.3f} meters".format(altitude))
33 temperature = sensor.temperature
34 print("Temperature: {0:0.3f} Celsius".format(temperature))
35 time.sleep(1.0)