|
| 1 | + |
| 2 | +[](https://github.com/marketplace/actions/arduino_ci) |
| 3 | +[](https://github.com/RobTillaart/SHT31_SW/actions/workflows/jsoncheck.yml) |
| 4 | +[](https://github.com/RobTillaart/SHT31_SW/actions/workflows/arduino-lint.yml) |
| 5 | +[](https://github.com/RobTillaart/SHT31_SW/blob/master/LICENSE) |
| 6 | +[](https://github.com/RobTillaart/SHT31_SW/releases) |
| 7 | + |
| 8 | + |
| 9 | +# SHT31_SW |
| 10 | + |
| 11 | +Arduino library for the SHT31 temperature and humidity sensor - using **SoftWire**. |
| 12 | + |
| 13 | + |
| 14 | +## Description |
| 15 | + |
| 16 | +This library features the class SHT31_SW derived from - https://github.com/RobTillaart/SHT31 |
| 17 | +It has the same interface as the SHT31 class so please use that documentation |
| 18 | +as it will be the "leading class". |
| 19 | + |
| 20 | +The main difference is that this SHT31_SW class uses the **SoftWire** |
| 21 | +library for communication instead of the **TwoWire** based **Wire** class. |
| 22 | + |
| 23 | +**SoftWire** is a software I2C bus library to be able to select other pins |
| 24 | +than the default (AVR) hardware I2C pins (SDA and SCL). |
| 25 | +An important reason to use this version is when you want more than two |
| 26 | +devices on one Arduino. |
| 27 | + |
| 28 | + |
| 29 | +#### Status |
| 30 | + |
| 31 | +- experimental |
| 32 | +- created as alternative for SHT31 PR #35. |
| 33 | + |
| 34 | + |
| 35 | +#### SHT sensors |
| 36 | + |
| 37 | +The SHT3x family of sensors should work up to 1 MHz I2C (although |
| 38 | + |
| 39 | +This library should also work for SHT30/35/85 but these are not tested yet. |
| 40 | + |
| 41 | +Accuracy table |
| 42 | + |
| 43 | +| SENSOR | Temperature | Humidity | |
| 44 | +|:--------:|:-------------:|:----------:| |
| 45 | +| SHT30 | ~0.3 | 2.0 | |
| 46 | +| SHT31 | ~0.3 | 1.5 | |
| 47 | +| SHT35 | ~0.2 | 1.5 | |
| 48 | +| SHT85 | ~0.2 | 1.5 | |
| 49 | + |
| 50 | + |
| 51 | +#### Links |
| 52 | + |
| 53 | +These libraries need to be installed to get SHT31_SW working: |
| 54 | + |
| 55 | +- https://github.com/stevemarple/SoftWire |
| 56 | +- https://github.com/stevemarple/AsyncDelay |
| 57 | +- https://github.com/RobTillaart/SHT31 |
| 58 | + |
| 59 | + |
| 60 | +## Interface |
| 61 | + |
| 62 | +```cpp |
| 63 | +#include "SHT31_SW.h" |
| 64 | +``` |
| 65 | + |
| 66 | +Note: The interface is mostly inherited from SHT31 but presented here for completeness. |
| 67 | + |
| 68 | + |
| 69 | +#### Base interface |
| 70 | + |
| 71 | +- **SHT31_SW()** constructor. |
| 72 | +- **bool begin(uint8_t address, SoftWire \*wire = &Wire)** for platforms with multiple I2C buses. |
| 73 | +- **bool begin(SoftWire \*wire = &Wire)** same as above. With default SHT_DEFAULT_ADDRESS. |
| 74 | +- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math. |
| 75 | +Does read both the temperature and humidity. |
| 76 | +- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected. |
| 77 | +- **uint16_t readStatus()** details see datasheet and **Status fields** below. |
| 78 | +- **uint32_t lastRead()** in milliSeconds since start of program. |
| 79 | +- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if it fails. |
| 80 | +- **float getHumidity()** computes the relative humidity in % based on the latest raw reading, and returns it. |
| 81 | +- **float getTemperature()** computes the temperature in °C based on the latest raw reading, and returns it. |
| 82 | +- **float getFahrenheit()** computes the temperature in °F based on the latest raw reading, and returns it. |
| 83 | +- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor. |
| 84 | +- **uint16_t getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor. |
| 85 | + |
| 86 | +Note that the temperature and humidity values are recalculated on every call to **getHumidity()** and **getTemperature()**. |
| 87 | +If you're worried about the extra cycles, you should make sure to cache these values or only request them after |
| 88 | +you've performed a new reading. |
| 89 | + |
| 90 | + |
| 91 | +#### Error interface |
| 92 | + |
| 93 | +- **int getError()** returns last set error flag and clear it. |
| 94 | +Be sure to clear the error flag by calling **getError()** before calling any command as the error flag could be from a previous command. |
| 95 | + |
| 96 | +| Error | Symbolic | Description | |
| 97 | +|:-------:|:----------------------------|:------------------------------| |
| 98 | +| 0x00 | SHT31_OK | no error | |
| 99 | +| 0x81 | SHT31_ERR_WRITECMD | I2C write failed | |
| 100 | +| 0x82 | SHT31_ERR_READBYTES | I2C read failed | |
| 101 | +| 0x83 | SHT31_ERR_HEATER_OFF | Could not switch off heater | |
| 102 | +| 0x84 | SHT31_ERR_NOT_CONNECT | Could not connect | |
| 103 | +| 0x85 | SHT31_ERR_CRC_TEMP | CRC error in temperature | |
| 104 | +| 0x86 | SHT31_ERR_CRC_HUM | CRC error in humidity | |
| 105 | +| 0x87 | SHT31_ERR_CRC_STATUS | CRC error in status field | |
| 106 | +| 0x88 | SHT31_ERR_HEATER_COOLDOWN | Heater need to cool down | |
| 107 | +| 0x88 | SHT31_ERR_HEATER_ON | Could not switch on heater | |
| 108 | + |
| 109 | + |
| 110 | +#### Heater interface |
| 111 | + |
| 112 | +**WARNING:** Do not use heater for long periods. |
| 113 | + |
| 114 | +Use the heater for max **180** seconds, and let it cool down **180** seconds = 3 minutes. |
| 115 | +SHT31 version 0.3.3 and up guards the cool down time by preventing switching the heater on |
| 116 | +within **180** seconds of the last switch off. Note: this guarding is not reboot persistent. |
| 117 | + |
| 118 | +**WARNING:** The user is responsible to switch the heater off manually! |
| 119 | + |
| 120 | +The class does **NOT** do this automatically. |
| 121 | +Switch off the heater by directly calling **heatOff()** or indirectly by calling **isHeaterOn()**. |
| 122 | + |
| 123 | +- **void setHeatTimeout(uint8_t seconds)** Set the time out of the heat cycle. |
| 124 | +This value is truncated to max 180 seconds. |
| 125 | +- **uint8_t getHeatTimeout |
| 126 | +- **bool heatOn()** switches heat cycle on if not already on. |
| 127 | +Returns false if fails, setting error to **SHT31_ERR_HEATER_COOLDOWN** |
| 128 | +or to **SHT31_ERR_HEATER_ON**. |
| 129 | +- **bool heatOff()** switches heat cycle off. |
| 130 | +Returns false if fails, setting error to **SHT31_ERR_HEATER_OFF**. |
| 131 | +- **bool isHeaterOn()** is the sensor still in heating cycle? replaces **heatUp()**. |
| 132 | +Will switch the heater off if max heating time has passed. |
| 133 | +- **bool heatUp()** will be obsolete in the future. replaced by **isHeaterOn()** |
| 134 | + |
| 135 | + |
| 136 | +#### Async interface |
| 137 | + |
| 138 | +See async example for usage |
| 139 | + |
| 140 | +- **bool requestData()** requests a new measurement. Returns false if this fails. |
| 141 | +- **bool dataReady()** checks if enough time has passed to read the data. (15 milliseconds) |
| 142 | +- **bool readData(bool fast = true)** fast = true skips the CRC check. |
| 143 | +Returns false if reading fails or in case of a CRC failure. |
| 144 | + |
| 145 | + |
| 146 | +## Status fields |
| 147 | + |
| 148 | +| BIT | Description | value | notes | |
| 149 | +|:------|:-----------------------------|:--------|:--------| |
| 150 | +| 15 | Alert pending status | 0 | no pending alerts |
| 151 | +| | | 1 | at least one pending alert - default |
| 152 | +| 14 | Reserved | 0 | |
| 153 | +| 13 | Heater status | 0 | Heater OFF - default |
| 154 | +| | | 1 | Heater ON |
| 155 | +| 12 | Reserved | 0 | |
| 156 | +| 11 | Humidity tracking alert | 0 | no alert - default |
| 157 | +| | | 1 | alert |
| 158 | +| 10 | Temperature tracking alert | 0 | no alert - default |
| 159 | +| | | 1 | alert |
| 160 | +| 9-5 | Reserved | 00000 | |
| 161 | +| 4 | System reset detected | 0 | no reset since last ‘clear status register’ command |
| 162 | +| | | 1 | reset detected (hard or soft reset command or supply fail) - default |
| 163 | +| 3-2 | Reserved | 00 | |
| 164 | +| 1 | Command status | 0 | last command executed successfully |
| 165 | +| | | 1 | last command not processed. Invalid or failed checksum |
| 166 | +| 0 | Write data checksum status | 0 | checksum of last write correct |
| 167 | +| | | 1 | checksum of last write transfer failed |
| 168 | + |
| 169 | + |
| 170 | +## Future |
| 171 | + |
| 172 | +- keep in sync with (leading) SHT31 library |
| 173 | + |
| 174 | + |
0 commit comments