The star of the SHT4x Trinkey is of course the SHT4x sensor. You can use this sensor to read temperature and humidity data with CircuitPython and the Adafruit_CircuitPython_SHT4x library. This page has example code that prints humidity and temperature data to the serial monitor and an additional example that calculates vapor-pressure deficit (VPD), which is a helpful data point for growing plants.
The SHT4x sensor is located between the capacitive touch pad and the reset button in a thermal-isolation cutout.
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_SHT4x library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folder and file:
- adafruit_bus_device/
- adafruit_sht4x.mpy
Temperature and Humidity Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: Copyright (c) 2020 ladyada for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import adafruit_sht4x i2c = board.I2C() # uses board.SCL and board.SDA sht = adafruit_sht4x.SHT4x(i2c) # overflow issue with sht41, not present with sht45 # print("Found SHT4x with serial number", hex(sht.serial_number)) sht.mode = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION print("Current mode is: ", adafruit_sht4x.Mode.string[sht.mode]) print() while True: temperature, relative_humidity = sht.measurements print(f"Temperature: {temperature:0.1f} C") print(f"Humidity: {relative_humidity:0.1f} %") print("") time.sleep(1)
First, the sensor is instantiated over I2C. Then, in the loop, the temperature and humidity readings are printed to the serial monitor.
# SPDX-FileCopyrightText: Copyright (c) 2020 ladyada for Adafruit Industries # # SPDX-License-Identifier: MIT import time import math import board import adafruit_sht4x i2c = board.I2C() # uses board.SCL and board.SDA sht = adafruit_sht4x.SHT4x(i2c) sht.mode = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION while True: temperature, relative_humidity = sht.measurements # saturation vapor pressure is calculated svp = 0.6108 * math.exp(17.27 * temperature / (temperature + 237.3)) # actual vapor pressure avp = relative_humidity / 100 * svp # VPD = saturation vapor pressure - actual vapor pressure vpd = svp - avp print(f"Vapor-Pressure Deficit: {vpd:0.1f} kPa") time.sleep(1)
First, the sensor is instantiated over I2C. Then, in the loop, you'll see the vapor-pressure deficit (VPD) print out to the serial monitor. The VPD is calculated with the temperature and humidity readings from the SHT4x sensor. First, the saturation vapor pressure is calculated:
svp = 0.6108 * math.exp(17.27 * temperature / (temperature + 237.3))
Then, the actual vapor pressure:
avp = relative_humidity / 100 * svp
And finally VPD is found by subtracting the actual vapor pressure from the saturation vapor pressure:
vpd = svp - avp
Text editor powered by tinymce.