|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +import time |
| 4 | +import board |
| 5 | +from adafruit_display_text.bitmap_label import Label |
| 6 | +from displayio import Group |
| 7 | +from terminalio import FONT |
| 8 | + |
| 9 | +import adafruit_sht4x |
| 10 | + |
| 11 | +# Create sensor object, communicating over the board's default I2C bus |
| 12 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 13 | +# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector |
| 14 | +sht = adafruit_sht4x.SHT4x(i2c) |
| 15 | + |
| 16 | +print("Found SHT4x with serial number", hex(sht.serial_number)) |
| 17 | + |
| 18 | +sht.mode = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION |
| 19 | +# Can also set the mode to enable heater |
| 20 | +# sht.mode = adafruit_sht4x.Mode.LOWHEAT_100MS |
| 21 | +# print("Current mode is: ", adafruit_sht4x.Mode.string[sht.mode]) |
| 22 | + |
| 23 | + |
| 24 | +# Example written for boards with built-in displays |
| 25 | +display = board.DISPLAY |
| 26 | + |
| 27 | +# Create a main_group to hold anything we want to show on the display. |
| 28 | +main_group = Group() |
| 29 | + |
| 30 | +# Create a Label to show the readings. If you have a very small |
| 31 | +# display you may need to change to scale=1. |
| 32 | +display_output_label = Label(FONT, text="", scale=2) |
| 33 | + |
| 34 | +# Place the label near the top left corner with anchored positioning |
| 35 | +display_output_label.anchor_point = (0, 0) |
| 36 | +display_output_label.anchored_position = (4, 4) |
| 37 | + |
| 38 | +# Add the label to the main_group |
| 39 | +main_group.append(display_output_label) |
| 40 | + |
| 41 | +# Set the main_group as the root_group of the display |
| 42 | +display.root_group = main_group |
| 43 | + |
| 44 | +# Begin main loop |
| 45 | +while True: |
| 46 | + temperature, relative_humidity = sht.measurements |
| 47 | + # Update the label.text property to change the text on the display |
| 48 | + display_output_label.text = ( |
| 49 | + f"Temperature: {temperature:.1f} C \nHumidity: {relative_humidity:.1f} %" |
| 50 | + ) |
| 51 | + # Wait for a bit |
| 52 | + time.sleep(1) |
0 commit comments