Giter Club home page Giter Club logo

adafruit_circuitpython_sht31d's Introduction

Introduction

Documentation Status Discord Build Status Code Style: Black

CircuitPython module for the SHT31-D temperature and humidity sensor.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-sht31d

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-sht31d

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-sht31d

Usage Example

You must import the library to use it:

import adafruit_sht31d

This driver takes an instantiated and active I2C object (from the busio or the bitbangio library) as an argument to its constructor. The way to create an I2C object depends on the board you are using.

import board

i2c = board.I2C()

Once you have created the I2C interface object, you can use it to instantiate the sensor object:

sensor = adafruit_sht31d.SHT31D(i2c)

And then you can start measuring the temperature and humidity:

print(sensor.temperature)
print(sensor.relative_humidity)

You can instruct the sensor to periodically measure the temperature and humidity, storing the result in its internal cache:

sensor.mode = adafruit_sht31d.MODE_PERIODIC

You can adjust the frequency at which the sensor periodically gathers data to: 0.5, 1, 2, 4 or 10 Hz. The following adjusts the frequency to 2 Hz:

sensor.frequency = adafruit_sht31d.FREQUENCY_2

The sensor is capable of storing eight results. The sensor stores these results in an internal FILO cache. Retrieving these results is simlilar to taking a measurement. The sensor clears its cache once the stored data is read. The sensor always returns eight data points. The list of results is backfilled with the maximum output values of 130.0 ÂēC and 100.01831417975366 % RH:

print(sensor.temperature)
print(sensor.relative_humidity)

The sensor will continue to collect data at the set interval until it is returned to single shot data acquisition mode:

sensor.mode = adafruit_sht31d.MODE_SINGLE

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_sht31d's People

Contributors

brennen avatar caternuson avatar chriswwind avatar dhalbert avatar evaherrada avatar foamyguy avatar jerryneedell avatar jposada202020 avatar kattni avatar keiththeee avatar ladyada avatar sommersoft avatar tannewt avatar tcfranks avatar tekktrik avatar thekitty avatar woefulderelict avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_sht31d's Issues

Reading out SHT31D causes SHT40 temperature+humidity to spike

I hooked up both a SHT31D and a SHT40 to the same RPi. But I kept getting these weird spikes in the SHT40 values if I also read the SH31D just before the SHT40, and worst of all if I read them both at the same time.

If I read the SHT40 before the SHT31D the values seem fine. But if I read the SHT40 too soon after the SHT31D it returns very high temperature and very low humidity.

I initially thought it was heat from the wires or some kind of interference. But now after having tried nearly all possible wiring alternatives, both with 3V and 5V, I think something might be happening due to the python code in some way.

Pasting two different tets. One where the SHT40 is read first and one where the SHT31D is read first.

import time
import board
import adafruit_sht31d
import adafruit_sht4x

i2c = board.I2C()

sht40 = adafruit_sht4x.SHT4x(i2c)
sht40.mode = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION

sht31d = adafruit_sht31d.SHT31D(i2c)

while True:
    sht40_temperature, sht40_humidity = sht40.measurements
    sht31d_temperature = sht31d.temperature
    sht31d_humidity = sht31d.relative_humidity
    print(round(sht31d_temperature,4), round(sht31d_humidity,4), round(sht40_temperature,4),round(sht40_humidity,4))
    time.sleep(1)
23.3871 61.1612 -45.0 58.2023
23.4138 61.1643 24.4072 56.1099
23.3871 61.1368 24.6075 53.4148
23.4005 61.1521 24.7356 51.332
23.3871 61.146 24.8371 49.7259
23.3711 61.0605 24.9039 48.5853
23.4005 61.088 24.9866 47.7118
23.3871 61.0971 25.0587 47.0766
23.4005 61.0086 25.1095 46.5406
23.4138 61.0407 25.1602 46.1229
23.4005 61.0269 25.2243 45.7548
23.4138 61.0178 25.251 45.5354
23.4005 61.0315 25.3151 45.2856
23.4138 61.0193 25.3552 45.1501
23.4138 60.9873 25.3632 44.9728
23.4005 61.0101 25.3899 44.8698
23.4138 61.0147 25.4299 44.7095
23.4138 60.9903 25.4433 44.6371
23.4272 60.9766 25.4833 44.5627
import time
import board
import adafruit_sht31d
import adafruit_sht4x

i2c = board.I2C()

sht40 = adafruit_sht4x.SHT4x(i2c)
sht40.mode = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION

sht31d = adafruit_sht31d.SHT31D(i2c)

while True:
    sht31d_temperature = sht31d.temperature
    sht31d_humidity = sht31d.relative_humidity
    time.sleep(0.1)
    sht40_temperature, sht40_humidity = sht40.measurements
    print(round(sht31d_temperature,4), round(sht31d_humidity,4), round(sht40_temperature,4),round(sht40_humidity,4))
    time.sleep(1)
23.4699 60.7202 32.541 56.9129
23.4699 60.7248 32.9736 54.2655
23.4565 60.7355 33.1338 51.8222
23.4699 60.7263 33.2086 50.0769
23.4859 60.7126 33.294 48.8066
23.4565 60.6928 33.3742 47.8643
23.4859 60.7126 33.4089 47.1987
23.4565 60.6928 33.4943 46.7142
23.4565 60.7126 33.537 46.3213
23.4699 60.6928 33.5744 46.0371
23.4565 60.708 33.6225 45.8654
23.4565 60.6836 33.6358 45.6384
23.4565 60.6973 33.6572 45.5431
23.4699 60.6851 33.6839 45.3943

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_sht31d.py:115
  • adafruit_sht31d.py:128
  • adafruit_sht31d.py:179
  • adafruit_sht31d.py:193
  • adafruit_sht31d.py:272
  • adafruit_sht31d.py:291
  • adafruit_sht31d.py:309
  • adafruit_sht31d.py:321
  • adafruit_sht31d.py:340
  • adafruit_sht31d.py:385

Humidity calculation does not match formula given in datasheet

I suspect a bug in the conversion from raw sensor data into relative humidity.
I would appreciate one of the maintainers reviewing this issue to determine if it is in fact a bug.

The conversion in question is here:

humidity[i] = 100 * (word[(i*2)+1] / 65523)

Note division by 65523. Based upon my review of datasheets and other libraries, I believe this value should in fact be 65535 (i.e., 2^16 -1)

Here are two versions of the Sensirion datasheet:
Sensirion currently links to this one (rev. February 2019)
Adafruit's tutorial links to this one (rev. May 2015)

Both give the following formulas in section 4.13 Conversion of Signal Output:
image

Note the divisor is 65535 (i.e., 2^16 -1). Further, it appears that the Arduino equivalent library uses that value:
https://github.com/adafruit/Adafruit_SHT31/blob/c735e72e027caa230ca126bd0b49569b9c5b81ea/Adafruit_SHT31.cpp#L146

As a final reference point, the formula given in the datasheet for the HTU21D-F is nearly identical, and again the Adafruit Arduino library appears to use it verbatim:
https://github.com/adafruit/Adafruit_HTU21DF_Library/blob/4c90679095303e19938997ba5c624f846c9e2205/Adafruit_HTU21DF.cpp#L137

Being fairly new to Python and microcontrollers I could very well be missing something. If that is the case, I would humbly suggest a comment in the code to explain why 65523 was chosen, in order to inform other users like myself, and avoid taking up more of your attention on this question.

Thank you for your review and for an excellent library!
It will soon be at work helping to grow crops and save water in California.

DisplayIO Example Wanted

Add a Basic DisplayIO Based Example

We would like to have a basic displayio example for this library. The example should be written for microcontrollers with a built-in display. At a minimum it should show a Label on the display and update it with live readings from the sensor.

The example should not be overly complex, it's intended to be a good starting point for displayio based projects that utilize this sensor library. Try to keep all visual content as near to the top left corner as possible in order to best fascilitate devices with small built-in display resolutions.

The new example should follow the naming convention examples/libraryname_displayio_simpletest.py with "libraryname" being replaced by the actual name of this library.

You can find an example of a Pull Request that adds this kind of example here: adafruit/Adafruit_CircuitPython_BME680#72

We have a guide that covers the process of contributing with git and github: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

If you're interested in contributing but need additional help, or just want to say hi, feel free to join the Discord server to ask questions: https://adafru.it/discord

CRC mismatch

CRC mismatch

python3 sht31d_simpletest.py Traceback (most recent call last): File "/home/pi/enose/Adafruit_CircuitPython_SHT31D/examples/sht31d_simpletest.py", line 14, in <module> print("\nTemperature: %0.1f C" % sensor.temperature) File "/usr/local/lib/python3.9/dist-packages/adafruit_circuitpython_sht31d-2.3.8.dev3+g028731e-py3.9.egg/adafruit_sht31d.py", line 359, in temperature File "/usr/local/lib/python3.9/dist-packages/adafruit_circuitpython_sht31d-2.3.8.dev3+g028731e-py3.9.egg/adafruit_sht31d.py", line 255, in _read File "/usr/local/lib/python3.9/dist-packages/adafruit_circuitpython_sht31d-2.3.8.dev3+g028731e-py3.9.egg/adafruit_sht31d.py", line 236, in _data File "/usr/local/lib/python3.9/dist-packages/adafruit_circuitpython_sht31d-2.3.8.dev3+g028731e-py3.9.egg/adafruit_sht31d.py", line 136, in _unpack RuntimeError: CRC mismatch

FR: Control LED

It would be nice to disable the green LED during the night or to only turn it on when the heater is on.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.