Simple test

Ensure your device works with this simple test.

examples/bmp280_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""Simpletest Example that shows how to get temperature,
 5pressure, and altitude readings from a BMP280"""
 6
 7import time
 8
 9import board
10
11# import digitalio # For use with SPI
12import adafruit_bmp280
13
14# Create sensor object, communicating over the board's default I2C bus
15i2c = board.I2C()  # uses board.SCL and board.SDA
16# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
17bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
18
19# OR Create sensor object, communicating over the board's default SPI bus
20# spi = board.SPI()
21# bmp_cs = digitalio.DigitalInOut(board.D5)
22# bmp280 = adafruit_bmp280.Adafruit_BMP280_SPI(spi, bmp_cs)
23
24# change this to match the location's pressure (hPa) at sea level
25bmp280.sea_level_pressure = 1013.25
26
27while True:
28    print(f"\nTemperature: {bmp280.temperature:0.1f} C")
29    print(f"Pressure: {bmp280.pressure:0.1f} hPa")
30    print(f"Altitude = {bmp280.altitude:0.2f} meters")
31    time.sleep(2)

Normal Mode

Example showing how the BMP280 library can be used to set the various parameters supported by the sensor.

examples/bmp280_normal_mode.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example showing how the BMP280 library can be used to set the various
 6parameters supported by the sensor.
 7Refer to the BMP280 datasheet to understand what these parameters do
 8"""
 9
10import time
11
12import board
13
14import adafruit_bmp280
15
16# Create sensor object, communicating over the board's default I2C bus
17i2c = board.I2C()  # uses board.SCL and board.SDA
18# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
19bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
20
21# OR Create sensor object, communicating over the board's default SPI bus
22# spi = busio.SPI()
23# bmp_cs = digitalio.DigitalInOut(board.D5)
24# bmp280 = adafruit_bmp280.Adafruit_BMP280_SPI(spi, bmp_cs)
25
26# change this to match the location's pressure (hPa) at sea level
27bmp280.sea_level_pressure = 1013.25
28bmp280.mode = adafruit_bmp280.MODE_NORMAL
29bmp280.standby_period = adafruit_bmp280.STANDBY_TC_500
30bmp280.iir_filter = adafruit_bmp280.IIR_FILTER_X16
31bmp280.overscan_pressure = adafruit_bmp280.OVERSCAN_X16
32bmp280.overscan_temperature = adafruit_bmp280.OVERSCAN_X2
33# The sensor will need a moment to gather inital readings
34time.sleep(1)
35
36while True:
37    print(f"\nTemperature: {bmp280.temperature:0.1f} C")
38    print(f"Pressure: {bmp280.pressure:0.1f} hPa")
39    print(f"Altitude = {bmp280.altitude:0.2f} meters")
40    time.sleep(2)

DisplayIO Simpletest

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

examples/bmp280_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 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_bmp280
14
15# Simple demo of the BMP280 barometric pressure sensor.
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
20bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
21
22# change this to match the location's pressure (hPa) at sea level
23bmp280.sea_level_pressure = 1013.25
24
25# Create two Labels to show the readings. If you have a very small
26# display you may need to change to scale=1.
27tempandpress_output_label = Label(FONT, text="", scale=2)
28altitude_output_label = Label(FONT, text="", scale=2)
29
30# place the labels in the middle of the screen with anchored positioning
31tempandpress_output_label.anchor_point = (0, 0)
32tempandpress_output_label.anchored_position = (
33    4,
34    board.DISPLAY.height // 2 - 40,
35)
36altitude_output_label.anchor_point = (0, 0)
37altitude_output_label.anchored_position = (4, board.DISPLAY.height // 2 + 20)
38
39
40# add the label to the main_group
41main_group.append(tempandpress_output_label)
42main_group.append(altitude_output_label)
43
44# set the main_group as the root_group of the built-in DISPLAY
45board.DISPLAY.root_group = main_group
46
47# begin main loop
48while True:
49    # Update the label.text property to change the text on the display
50    tempandpress_output_label.text = (
51        f"Temperature:{bmp280.temperature:0.1f} C \nPressure:{bmp280.pressure:0.1f} hPa"
52    )
53    altitude_output_label.text = f"Altitude:{bmp280.altitude:0.2f} mts"
54    # wait for a bit
55    time.sleep(2.0)