Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: Copyright (c) 2020 ladyada for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7import adafruit_htu31d
8
9i2c = board.I2C() # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11htu = adafruit_htu31d.HTU31D(i2c)
12print("Found HTU31D with serial number", hex(htu.serial_number))
13
14htu.heater = True
15print("Heater is on?", htu.heater)
16htu.heater = False
17print("Heater is on?", htu.heater)
18
19while True:
20 temperature, relative_humidity = htu.measurements
21 print("Temperature: %0.1f C" % temperature)
22 print("Humidity: %0.1f %%" % relative_humidity)
23 print("")
24 time.sleep(1)
Setting Resolution Example¶
Show how to setup Relative Humidity and Temperature Sensor Resolutions
1# SPDX-FileCopyrightText: Copyright (c) 2021 Jose David M.
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7import adafruit_htu31d
8
9
10# import htu31d_setting_resolutions
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)
14
15
16print("Temperature Resolution: ", htu.temp_resolution)
17print("Humidity Resolution: ", htu.humidity_resolution)
18
19# Setting the temperature resolution.
20# Possible values are "0.040", "0.025", "0.016" and "0.012"
21htu.temp_resolution = "0.040"
22
23# Setting the Relative Humidity resolution.
24# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
25htu.humidity_resolution = "0.020%"
26
27print("Temperature Resolution: ", htu.temp_resolution)
28print("Humidity Resolution: ", htu.humidity_resolution)
29
30hum_res = ["0.020%", "0.014%", "0.010%", "0.007%"]
31temp_res = ["0.040", "0.025", "0.016", "0.012"]
32
33while True:
34 for humidity_resolution in hum_res:
35 htu.humidity_resolution = humidity_resolution
36 print(f"Current Humidity Resolution: {humidity_resolution}")
37 for _ in range(2):
38 print(f"Humidity: {htu.relative_humidity:.2f}")
39 print(f"Temperature: {htu.temperature:.2f}")
40 print("")
41 time.sleep(0.5)
42 for temperature_resolution in temp_res:
43 htu.temp_resolution = temperature_resolution
44 print(f"Current Temperature Resolution: {temperature_resolution}")
45 for _ in range(2):
46 print(f"Humidity: {htu.relative_humidity:.2f}")
47 print(f"Temperature: {htu.temperature:.2f}")
48 print("")
49 time.sleep(0.5)