Skip to content

Commit

Permalink
0.3.1 SHT2x
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Sep 11, 2023
1 parent 710c148 commit 87bbbfd
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 63 deletions.
16 changes: 13 additions & 3 deletions libraries/SHT2x/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.1] - 2023-09-10
- fix #21 example for elaborated async.
- improve async interface
- add **getRequestType()** to async interface
- add three request constants to .h file
- add **bool requestReady()** checks both.
- update readme.md
- minor edits


## [0.3.0] - 2023-03-26
- fix setResolution #13, #18
- fix heater settings
Expand Down Expand Up @@ -41,16 +51,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix getEIDB() bug.

## [0.1.3] - 2021-12-28
- update library.json
- update library.json
- update license
- minor edits

## [0.1.2] - 2021-09-29
## [0.1.2] - 2021-09-29
- Add Si70xx derived classes
- add getEIDA(), getEIDB(), getFirmwareVersion()
- update build-CI, readme.md badges

## [0.1.1] - 2021-09-28
## [0.1.1] - 2021-09-28
- Add HTU2x derived classes,
- update readme.md + add some status info

Expand Down
87 changes: 55 additions & 32 deletions libraries/SHT2x/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

[![Arduino CI](https://github.com/robtillaart/SHT2x/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![JSON check](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml)
[![Arduino CI](https://github.com/RobTillaart/SHT2x/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/SHT2x/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT2x/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/SHT2x.svg)](https://github.com/RobTillaart/SHT2x/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SHT2x/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT2x.svg?maxAge=3600)](https://github.com/RobTillaart/SHT2x/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/SHT2x.svg)](https://registry.platformio.org/libraries/robtillaart/SHT2x)


# SHT2x
Expand Down Expand Up @@ -39,6 +42,13 @@ If you want to use more on one I2C bus one needs either an I2C multiplexer
or one should switch sensors on/off like the select in SPI communication.


#### Related

- https://github.com/RobTillaart/SHT31
- https://github.com/RobTillaart/SHT85
- https://github.com/RobTillaart/tinySHT2x


## Interface

```cpp
Expand Down Expand Up @@ -89,13 +99,24 @@ Note: The raw temperature and raw humidity are ideal to minimize storage or to m
Experimental since 0.2.2 this interface can change in the future
Discussion see https://github.com/RobTillaart/SHT2x/issues/16

- **bool requestTemperature()**
- **bool requestHumidity()**
- **bool reqTempReady()**
- **bool reqHumReady()**
- **bool readTemperature()**
- **bool readHumidity()**
- **uint32_t lastRequest()**
- **bool requestTemperature()** starts new temperature request.
Overrules optional existing / running request.
- **bool requestHumidity()** starts new humidity request.
Overrules optional existing / running request.
- **bool reqTempReady()** returns true if temperature request is ready.
- **bool reqHumReady()** returns true if humidity request is ready.
- **bool requestReady()** checks if either temperature or humidity is ready.
- **bool readTemperature()** calculates the temperature from raw measurement.
- **bool readHumidity()** calculates the humidity from raw measurement.
- **uint32_t lastRequest()** timestamp of last request.
- **uint8_t getRequestType()** get current request type.


| Value | Symbolic | Description | Notes |
|:-------:|:----------------------------|:------------------------------|:-----------|
| 0x00 | SHT2x_REQ_NONE | no request pending | |
| 0x01 | SHT2x_REQ_TEMPERATURE | temperature request pending | |
| 0x02 | SHT2x_REQ_HUMIDITY | humidity request pending | |

TODO elaborate documentation.

Expand Down Expand Up @@ -157,28 +178,28 @@ To be tested.

#### Status fields

From HTU20 datasheet
From HTU20 datasheet (read for details).

| bits | value | meaning |
|:------:|:------:|:--------------------|
| 00 | 0 | open circuit |
| 01 | 1 | temperature reading |
| 10 | 2 | humidity reading |
| 11 | 3 | closed circuit |
| bits | value | Symbolic | Description |
|:------:|:-------:|:------------------------------|:----------------------|
| 00 | 0 | SHT2x_STATUS_OPEN_CIRCUIT | open circuit |
| 01 | 1 | SHT2x_STATUS_TEMPERATURE | temperature reading |
| 10 | 2 | SHT2x_STATUS_HUMIDITY | humidity reading |
| 11 | 3 | SHT2x_STATUS_CLOSED_CIRCUIT | closed circuit |


#### Resolution

**Warning experimental**
- needs more testing as results are not in line with the datasheet.
- needs more testing as the results are not in line with the datasheet.
- only tested on a HTUxx sensor.
- tested with **SHT2X_resolution.ino**

- **void setResolution(uint8_t res)** res = 0..3, other values return false.
- **uint8_t getResolution()** returns resolution set 0..3.


Datasheet SHT20 Table 8: (resolution)
Datasheet SHT20 Table 8: resolution

| RES | Humidity | Temperature |
|:-----:|:----------:|:-------------:|
Expand All @@ -187,7 +208,8 @@ Datasheet SHT20 Table 8: (resolution)
| 2 | 10 bit | 13 bit |
| 3 | 11 bit | 11 bit |

Datasheet SHT20 Table 7: (timing) and results of real measurements.

Datasheet SHT20 Table 7: timing versus results of real measurements.
( https://github.com/RobTillaart/SHT2x/pull/11 )

| RES | HUM | TEMP | TOTAL | REAL |
Expand All @@ -209,44 +231,45 @@ Timing in milliseconds.
## Future

#### Must

- improve documentation
- reorganize interface
- async documentation
- clean up code.


#### 0.4.0

- add crc8 check (need sensor to test)
- improve error handling (all code paths)
- investigate blocking delay() in read
- add offset for temperature and humidity


#### ASYNC 0.4.0
improvements for interface.

- **bool requestReady()** checks both.
- **bool requestPending()** checks.
- **uint8_t getRequestType()** returns 0, 1, 2.
- async documentation


#### Should

- test test test
- get hardware
- add examples
- test resolutions
- performance different resolutions
- test battery


#### Could

- fix TODO in code (.cpp and .h) and documentation
- update unit tests
- add type info in derived classes?


#### Wont

- add **getSerialNumber()**
**getEIDA()** and **getEIDB()** covers this


## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,

41 changes: 24 additions & 17 deletions libraries/SHT2x/SHT2x.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: SHT2x.cpp
// AUTHOR: Rob Tillaart, Viktor Balint
// VERSION: 0.3.0
// VERSION: 0.3.1
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
Expand All @@ -26,10 +26,6 @@
#define SHT2x_USRREG_BATTERY 0x20
#define SHT2x_USRREG_HEATER 0x04

#define SHT2x_REQ_NONE 0x00
#define SHT2x_REQ_TEMPERATURE 0x01
#define SHT2x_REQ_HUMIDITY 0x02


//////////////////////////////////////////////////////////////
//
Expand All @@ -39,7 +35,7 @@ SHT2x::SHT2x()
{
_lastRead = 0;
_lastRequest = 0;
_requestType = 0;
_requestType = SHT2x_REQ_NONE;
_rawTemperature = 0;
_rawHumidity = 0;
_heatTimeout = 0;
Expand Down Expand Up @@ -165,6 +161,11 @@ bool SHT2x::reqHumReady()
}


bool SHT2x::requestReady()
{
return (reqTempReady() || reqHumReady());
}

bool SHT2x::readTemperature()
{
uint8_t buffer[3];
Expand All @@ -177,7 +178,7 @@ bool SHT2x::readTemperature()
if (crc8(buffer, 2) != buffer[2])
{
_error = SHT2x_ERR_CRC_TEMP;
// return false; // do not fail yet
// return false; // do not fail yet
}
_rawTemperature = buffer[0] << 8;
_rawTemperature += buffer[1];
Expand All @@ -187,7 +188,7 @@ bool SHT2x::readTemperature()
_requestType = SHT2x_REQ_NONE;

_status = buffer[1] & 0x0003;
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
{
_error = SHT2x_ERR_READBYTES;
return false;
Expand All @@ -207,7 +208,7 @@ bool SHT2x::readHumidity()
if (crc8(buffer, 2) != buffer[2])
{
_error = SHT2x_ERR_CRC_HUM;
// return false; // do not fail yet
// return false; // do not fail yet
}
_rawHumidity = buffer[0] << 8;
_rawHumidity += buffer[1];
Expand Down Expand Up @@ -235,20 +236,26 @@ uint32_t SHT2x::lastRequest()
}


uint8_t SHT2x::getRequestType()
{
return _requestType;
}


/////////////////////////////////////////////////////////
//
// TEMPERATURE AND HUMIDTY
//
float SHT2x::getTemperature()
{
// par 6.2
// par 6.2
return -46.85 + (175.72 / 65536.0) * _rawTemperature;
}


float SHT2x::getHumidity()
{
// par 6.1
// par 6.1
return -6.0 + (125.0 / 65536.0) * _rawHumidity;
}

Expand Down Expand Up @@ -347,7 +354,7 @@ bool SHT2x::heatOff()

if (writeCmd(SHT2x_WRITE_USER_REGISTER, userReg) == false)
{
_error = SHT2x_ERR_HEATER_OFF; // can be serious!
_error = SHT2x_ERR_HEATER_OFF; // can be serious!
return false;
}
_heaterStop = millis();
Expand All @@ -362,7 +369,7 @@ bool SHT2x::isHeaterOn()
{
return false;
}
// did not exceed time out
// did not exceed time out
if (millis() - _heaterStart < (_heatTimeout * 1000UL))
{
return true;
Expand All @@ -380,7 +387,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
}

uint8_t heaterReg = 0;
writeCmd(0x11); // Read Heater Control Register
writeCmd(0x11); // Read Heater Control Register
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
{
_error = SHT2x_ERR_READBYTES;
Expand All @@ -400,7 +407,7 @@ bool SHT2x::setHeaterLevel(uint8_t level)
bool SHT2x::getHeaterLevel(uint8_t & level)
{
uint8_t heaterReg = 0;
writeCmd(0x11); // Read Heater Control Register
writeCmd(0x11); // Read Heater Control Register
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
{
_error = SHT2x_ERR_READBYTES;
Expand Down Expand Up @@ -541,8 +548,8 @@ bool SHT2x::batteryOK()
//
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
{
// CRC-8 formula from page 14 of SHT spec pdf
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
// CRC-8 formula from page 14 of SHT spec pdf
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
const uint8_t POLY = 0x31;
uint8_t crc = 0x00;

Expand Down
Loading

0 comments on commit 87bbbfd

Please sign in to comment.