Simple test
Ensure your device works with this simple test.
examples/htu31d_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2020 ladyada for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5import time
6
7import board
8
9import adafruit_htu31d
10
11i2c = board.I2C() # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
13htu = adafruit_htu31d.HTU31D(i2c)
14print("Found HTU31D with serial number", hex(htu.serial_number))
15
16htu.heater = True
17print("Heater is on?", htu.heater)
18htu.heater = False
19print("Heater is on?", htu.heater)
20
21while True:
22 temperature, relative_humidity = htu.measurements
23 print(f"Temperature: {temperature:0.1f} C")
24 print(f"Humidity: {relative_humidity:0.1f} %")
25 print("")
26 time.sleep(1)
Setting Resolution Example
Show how to setup Relative Humidity and Temperature Sensor Resolutions
examples/htu31d_setting_resolutions.py
1# SPDX-FileCopyrightText: Copyright (c) 2021 Jose David M.
2#
3# SPDX-License-Identifier: MIT
4
5import time
6
7import board
8
9import adafruit_htu31d
10
11# import htu31d_setting_resolutions
12i2c = board.I2C() # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14htu = adafruit_htu31d.HTU31D(i2c)
15
16
17print("Temperature Resolution: ", htu.temp_resolution)
18print("Humidity Resolution: ", htu.humidity_resolution)
19
20# Setting the temperature resolution.
21# Possible values are "0.040", "0.025", "0.016" and "0.012"
22htu.temp_resolution = "0.040"
23
24# Setting the Relative Humidity resolution.
25# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
26htu.humidity_resolution = "0.020%"
27
28print("Temperature Resolution: ", htu.temp_resolution)
29print("Humidity Resolution: ", htu.humidity_resolution)
30
31hum_res = ["0.020%", "0.014%", "0.010%", "0.007%"]
32temp_res = ["0.040", "0.025", "0.016", "0.012"]
33
34while True:
35 for humidity_resolution in hum_res:
36 htu.humidity_resolution = humidity_resolution
37 print(f"Current Humidity Resolution: {humidity_resolution}")
38 for _ in range(2):
39 print(f"Humidity: {htu.relative_humidity:.2f}")
40 print(f"Temperature: {htu.temperature:.2f}")
41 print("")
42 time.sleep(0.5)
43 for temperature_resolution in temp_res:
44 htu.temp_resolution = temperature_resolution
45 print(f"Current Temperature Resolution: {temperature_resolution}")
46 for _ in range(2):
47 print(f"Humidity: {htu.relative_humidity:.2f}")
48 print(f"Temperature: {htu.temperature:.2f}")
49 print("")
50 time.sleep(0.5)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/htu31d_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_htu31d
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
20htu = adafruit_htu31d.HTU31D(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label(s) in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (
29 4,
30 board.DISPLAY.height // 2 - 60,
31)
32
33# add the label(s) to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41 # update the text of the label(s) to show the sensor readings
42 temperature, relative_humidity = htu.measurements
43 display_output_label.text = (
44 f"Temperature: {temperature:.1f} C" + "\n" + f"Humidity: {relative_humidity:.1f} %"
45 )
46 # wait for a bit
47 time.sleep(0.5)