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