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() # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11sensor = adafruit_sht31d.SHT31D(i2c)
12
13loopcount = 0
14while True:
15 print("\nTemperature: %0.1f C" % sensor.temperature)
16 print("Humidity: %0.1f %%" % sensor.relative_humidity)
17 loopcount += 1
18 time.sleep(2)
19 # every 10 passes turn on the heater for 1 second
20 if loopcount == 10:
21 loopcount = 0
22 sensor.heater = True
23 print("Sensor Heater status =", sensor.heater)
24 time.sleep(1)
25 sensor.heater = False
26 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() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10sensor = adafruit_sht31d.SHT31D(i2c)
11
12print("\033[1mSensor\033[0m = SHT31-D")
13print("\033[1mSerial Number\033[0m = ", sensor.serial_number, "\n")
14
15for i in range(3):
16 if i == 0:
17 sensor.repeatability = adafruit_sht31d.REP_LOW
18 print("\033[1m\033[36mLow Repeatability:\033[0m\n")
19 if i == 1:
20 sensor.repeatability = adafruit_sht31d.REP_MED
21 print("\n\033[1m\033[36mMedium Repeatability:\033[0m\n")
22 if i == 2:
23 sensor.repeatability = adafruit_sht31d.REP_HIGH
24 sensor.clock_stretching = True
25 print("\n\033[1m\033[36mHigh Repeatability:\033[0m")
26 print("\033[1m\033[95mClock Stretching:\033[0m \033[92mEnabled\033[0m\n")
27 for itr in range(3):
28 print("\033[1mTemperature:\033[0m %0.3f ºC" % sensor.temperature)
29 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() # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11sensor = adafruit_sht31d.SHT31D(i2c)
12
13print("\033[1mSensor\033[0m = SHT31-D")
14print("\033[1mSerial Number\033[0m = ", sensor.serial_number, "\n")
15sensor.frequency = adafruit_sht31d.FREQUENCY_1
16sensor.mode = adafruit_sht31d.MODE_PERIODIC
17for i in range(3):
18 print("Please wait...", end="\r")
19 if i == 2:
20 sensor.heater = True
21 if i == 1:
22 time.sleep(4)
23 print("\033[91mCache half full.\033[0m")
24 else:
25 time.sleep(8)
26 if sensor.heater:
27 print("\033[1mHeater:\033[0m On ")
28 sensor.heater = False
29 print("\033[1mTemperature:\033[0m ", sensor.temperature)
30 if not sensor.heater:
31 print("\033[1mHeater:\033[0m Off")
32 print("\033[1mHumidity:\033[0m ", sensor.relative_humidity, "\n")
33sensor.mode = adafruit_sht31d.MODE_SINGLE