Simple test

Ensure your device works with this simple test.

examples/sht31d_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import board
 7
 8import adafruit_sht31d
 9
10# Create sensor object, communicating over the board's default I2C bus
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
13sensor = adafruit_sht31d.SHT31D(i2c)
14
15loopcount = 0
16while True:
17    print(f"\nTemperature: {sensor.temperature:0.1f} C")
18    print(f"Humidity: {sensor.relative_humidity:0.1f} %")
19    loopcount += 1
20    time.sleep(2)
21    # every 10 passes turn on the heater for 1 second
22    if loopcount == 10:
23        loopcount = 0
24        sensor.heater = True
25        print("Sensor Heater status =", sensor.heater)
26        time.sleep(1)
27        sensor.heater = False
28        print("Sensor Heater status =", sensor.heater)

Simple Mode

Example in how to use the sensor in simple mode

examples/sht31d_simple_mode.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5
 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")
15
16for i in range(3):
17    if i == 0:
18        sensor.repeatability = adafruit_sht31d.REP_LOW
19        print("\033[1m\033[36mLow Repeatability:\033[0m\n")
20    if i == 1:
21        sensor.repeatability = adafruit_sht31d.REP_MED
22        print("\n\033[1m\033[36mMedium Repeatability:\033[0m\n")
23    if i == 2:
24        sensor.repeatability = adafruit_sht31d.REP_HIGH
25        sensor.clock_stretching = True
26        print("\n\033[1m\033[36mHigh Repeatability:\033[0m")
27        print("\033[1m\033[95mClock Stretching:\033[0m \033[92mEnabled\033[0m\n")
28    for itr in range(3):
29        print(f"\033[1mTemperature:\033[0m {sensor.temperature:0.3f} ºC")
30        print(f"\033[1mHumidity:\033[0m {sensor.relative_humidity:0.2f} %", "\n")

Periodic Mode

Example in how to use the sensor in periodic mode

examples/sht31d_periodic_mode.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import board
 7
 8import adafruit_sht31d
 9
10# Create sensor object, communicating over the board's default I2C bus
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
13sensor = adafruit_sht31d.SHT31D(i2c)
14
15print("\033[1mSensor\033[0m = SHT31-D")
16print("\033[1mSerial Number\033[0m = ", sensor.serial_number, "\n")
17sensor.frequency = adafruit_sht31d.FREQUENCY_1
18sensor.mode = adafruit_sht31d.MODE_PERIODIC
19for i in range(3):
20    print("Please wait...", end="\r")
21    if i == 2:
22        sensor.heater = True
23    if i == 1:
24        time.sleep(4)
25        print("\033[91mCache half full.\033[0m")
26    else:
27        time.sleep(8)
28    if sensor.heater:
29        print("\033[1mHeater:\033[0m On    ")
30        sensor.heater = False
31    print("\033[1mTemperature:\033[0m ", sensor.temperature)
32    if not sensor.heater:
33        print("\033[1mHeater:\033[0m Off")
34    print("\033[1mHumidity:\033[0m ", sensor.relative_humidity, "\n")
35sensor.mode = adafruit_sht31d.MODE_SINGLE