|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2024 Jose D. Montoya |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +# Simple demo of the ENS160 air quality sensor using a built-in display. |
| 7 | +import time |
| 8 | +import board |
| 9 | +from adafruit_display_text.bitmap_label import Label |
| 10 | +from terminalio import FONT |
| 11 | +from displayio import Group |
| 12 | +import adafruit_ens160 |
| 13 | + |
| 14 | + |
| 15 | +# create a main_group to hold anything we want to show on the display. |
| 16 | +main_group = Group() |
| 17 | +# Initialize I2C bus and sensor. |
| 18 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 19 | +ens = adafruit_ens160.ENS160(i2c) |
| 20 | + |
| 21 | +# Set the temperature compensation variable to the ambient temp |
| 22 | +# for best sensor calibration |
| 23 | +ens.temperature_compensation = 25 |
| 24 | +# Same for ambient relative humidity |
| 25 | +ens.humidity_compensation = 50 |
| 26 | + |
| 27 | +# Create a Label to show the readings. If you have a very small |
| 28 | +# display you may need to change to scale=1. |
| 29 | +display_output_label = Label(FONT, text="", scale=2) |
| 30 | + |
| 31 | +# place the label in the middle of the screen with anchored positioning |
| 32 | +display_output_label.anchor_point = (0, 0) |
| 33 | +display_output_label.anchored_position = (4, board.DISPLAY.height // 2) |
| 34 | + |
| 35 | +# add the label to the main_group |
| 36 | +main_group.append(display_output_label) |
| 37 | + |
| 38 | +# set the main_group as the root_group of the built-in DISPLAY |
| 39 | +board.DISPLAY.root_group = main_group |
| 40 | + |
| 41 | +# begin main loop |
| 42 | +while True: |
| 43 | + # Update the label.text property to change the text on the display |
| 44 | + display_output_label.text = ( |
| 45 | + f"AQI:{ens.AQI} (1-5), TVOC:{ens.TVOC} ppb, eCO2:{ens.eCO2} ppm" |
| 46 | + ) |
| 47 | + # wait for a bit |
| 48 | + time.sleep(1.0) |
0 commit comments