|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2024 Jose D. Montoya |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +from adafruit_display_text.bitmap_label import Label |
| 9 | +from terminalio import FONT |
| 10 | +from displayio import Group |
| 11 | +import adafruit_bmp280 |
| 12 | + |
| 13 | +# Simple demo of the BMP280 barometric pressure sensor. |
| 14 | +# create a main_group to hold anything we want to show on the display. |
| 15 | +main_group = Group() |
| 16 | +# Initialize I2C bus and sensor. |
| 17 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 18 | +bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c) |
| 19 | + |
| 20 | +# change this to match the location's pressure (hPa) at sea level |
| 21 | +bmp280.sea_level_pressure = 1013.25 |
| 22 | + |
| 23 | +# Create two Labels to show the readings. If you have a very small |
| 24 | +# display you may need to change to scale=1. |
| 25 | +tempandpress_output_label = Label(FONT, text="", scale=2) |
| 26 | +altitude_output_label = Label(FONT, text="", scale=2) |
| 27 | + |
| 28 | +# place the labels in the middle of the screen with anchored positioning |
| 29 | +tempandpress_output_label.anchor_point = (0, 0) |
| 30 | +tempandpress_output_label.anchored_position = ( |
| 31 | + 4, |
| 32 | + board.DISPLAY.height // 2 - 40, |
| 33 | +) |
| 34 | +altitude_output_label.anchor_point = (0, 0) |
| 35 | +altitude_output_label.anchored_position = (4, board.DISPLAY.height // 2 + 20) |
| 36 | + |
| 37 | + |
| 38 | +# add the label to the main_group |
| 39 | +main_group.append(tempandpress_output_label) |
| 40 | +main_group.append(altitude_output_label) |
| 41 | + |
| 42 | +# set the main_group as the root_group of the built-in DISPLAY |
| 43 | +board.DISPLAY.root_group = main_group |
| 44 | + |
| 45 | +# begin main loop |
| 46 | +while True: |
| 47 | + # Update the label.text property to change the text on the display |
| 48 | + tempandpress_output_label.text = ( |
| 49 | + f"Temperature:{bmp280.temperature:0.1f} C \nPressure:{bmp280.pressure:0.1f} hPa" |
| 50 | + ) |
| 51 | + altitude_output_label.text = f"Altitude:{bmp280.altitude:0.2f} mts" |
| 52 | + # wait for a bit |
| 53 | + time.sleep(2.0) |
0 commit comments