Simple test

Ensure your device works with this simple test.

examples/mpl3115a2_simpletest.py
 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
 7
 8import board
 9
10import adafruit_mpl3115a2
11
12# Create sensor object, communicating over the board's default I2C bus
13i2c = board.I2C()  # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
15
16# Initialize the MPL3115A2.
17sensor = adafruit_mpl3115a2.MPL3115A2(i2c)
18# Alternatively you can specify a different I2C address for the device:
19# sensor = adafruit_mpl3115a2.MPL3115A2(i2c, address=0x10)
20
21# You can configure the pressure at sealevel to get better altitude estimates.
22# This value has to be looked up from your local weather forecast or meteorological
23# reports.  It will change day by day and even hour by hour with weather
24# changes.  Remember altitude estimation from barometric pressure is not exact!
25# Set this to a value in hectopascals:
26sensor.sealevel_pressure = 1022.5
27
28# Main loop to read the sensor values and print them every second.
29while True:
30    pressure = sensor.pressure
31    print(f"Pressure: {pressure:0.3f} hectopascals")
32    altitude = sensor.altitude
33    print(f"Altitude: {altitude:0.3f} meters")
34    temperature = sensor.temperature
35    print(f"Temperature: {temperature:0.3f} Celsius")
36    time.sleep(1.0)

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/mpl3115a2_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2025 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6import time
 7
 8import board
 9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_mpl3115a2
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20sensor = adafruit_mpl3115a2.MPL3115A2(i2c)
21
22
23# Create Label(s) to show the readings. If you have a very small
24# display you may need to change to scale=1.
25display_output_pressure = Label(FONT, text="", scale=2)
26display_output_altitude = Label(FONT, text="", scale=2)
27display_output_temperature = Label(FONT, text="", scale=2)
28
29# place the label(s) in the middle of the screen with anchored positioning
30display_output_pressure.anchor_point = (0, 0)
31display_output_pressure.anchored_position = (
32    4,
33    board.DISPLAY.height // 2 - 60,
34)
35display_output_altitude.anchor_point = (0, 0)
36display_output_altitude.anchored_position = (
37    4,
38    board.DISPLAY.height // 2 - 40,
39)
40display_output_temperature.anchor_point = (0, 0)
41display_output_temperature.anchored_position = (
42    4,
43    board.DISPLAY.height // 2 - 20,
44)
45
46
47# add the label(s) to the main_group
48main_group.append(display_output_pressure)
49main_group.append(display_output_altitude)
50main_group.append(display_output_temperature)
51
52# set the main_group as the root_group of the built-in DISPLAY
53board.DISPLAY.root_group = main_group
54
55# begin main loop
56while True:
57    # update the text of the label(s) to show the sensor readings
58    pressure = sensor.pressure
59    display_output_pressure.text = f"Pressure: {pressure:.2f} hPa"
60    altitude = sensor.altitude
61    display_output_altitude.text = f"Altitude: {altitude:.2f} m"
62    temperature = sensor.temperature
63    display_output_temperature.text = f"Temperature: {temperature:.2f} C"
64    # wait for a bit
65    time.sleep(2)