Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_sht31d
7
8# Create sensor object, communicating over the board's default I2C bus
9i2c = board.I2C()
10sensor = adafruit_sht31d.SHT31D(i2c)
11
12loopcount = 0
13while True:
14 print("\nTemperature: %0.1f C" % sensor.temperature)
15 print("Humidity: %0.1f %%" % sensor.relative_humidity)
16 loopcount += 1
17 time.sleep(2)
18 # every 10 passes turn on the heater for 1 second
19 if loopcount == 10:
20 loopcount = 0
21 sensor.heater = True
22 print("Sensor Heater status =", sensor.heater)
23 time.sleep(1)
24 sensor.heater = False
25 print("Sensor Heater status =", sensor.heater)
Simple Mode¶
Example in how to use the sensor in simple mode
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import adafruit_sht31d
6
7# Create sensor object, communicating over the board's default I2C bus
8i2c = board.I2C()
9sensor = adafruit_sht31d.SHT31D(i2c)
10
11print("\033[1mSensor\033[0m = SHT31-D")
12print("\033[1mSerial Number\033[0m = ", sensor.serial_number, "\n")
13
14for i in range(3):
15 if i == 0:
16 sensor.repeatability = adafruit_sht31d.REP_LOW
17 print("\033[1m\033[36mLow Repeatability:\033[0m\n")
18 if i == 1:
19 sensor.repeatability = adafruit_sht31d.REP_MED
20 print("\n\033[1m\033[36mMedium Repeatability:\033[0m\n")
21 if i == 2:
22 sensor.repeatability = adafruit_sht31d.REP_HIGH
23 sensor.clock_stretching = True
24 print("\n\033[1m\033[36mHigh Repeatability:\033[0m")
25 print("\033[1m\033[95mClock Stretching:\033[0m \033[92mEnabled\033[0m\n")
26 for itr in range(3):
27 print("\033[1mTemperature:\033[0m %0.3f ºC" % sensor.temperature)
28 print("\033[1mHumidity:\033[0m %0.2f %%" % sensor.relative_humidity, "\n")
Periodic Mode¶
Example in how to use the sensor in periodic mode
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6import adafruit_sht31d
7
8# Create sensor object, communicating over the board's default I2C bus
9i2c = board.I2C()
10sensor = adafruit_sht31d.SHT31D(i2c)
11
12loopcount = 0
13while True:
14 print("\nTemperature: %0.1f C" % sensor.temperature)
15 print("Humidity: %0.1f %%" % sensor.relative_humidity)
16 loopcount += 1
17 time.sleep(2)
18 # every 10 passes turn on the heater for 1 second
19 if loopcount == 10:
20 loopcount = 0
21 sensor.heater = True
22 print("Sensor Heater status =", sensor.heater)
23 time.sleep(1)
24 sensor.heater = False
25 print("Sensor Heater status =", sensor.heater)