0.1.1 SHT2x

This commit is contained in:
rob tillaart 2021-09-29 13:21:17 +02:00
parent 6af2cb4b55
commit 2ddca893ac
9 changed files with 125 additions and 46 deletions

View File

@ -8,7 +8,7 @@
# SHT2x
Arduino library for the SHT2x temperature and humidity sensor.
Arduino library for the SHT2x and HTU2x temperature and humidity sensors.
## Description
@ -29,11 +29,20 @@ All sensors in this family of sensors have address 0x40 (64 decimal).
## Interface
#### Base interface
#### Constructors
All classes below are derived from SHT2x class.
- **SHT2x()** constructor base class.
- **SHT20()** constructor.
- **SHT21()** constructor.
- **SHT25()** constructor.
- **HTU20()** constructor.
- **HTU21()** constructor.
#### Base interface
- **bool begin(dataPin, clockPin)** begin function for ESP8266 & ESP32;
returns false if device address is incorrect or device cannot be reset.
- **bool begin(TwoWire \*wire = &Wire)** optional set the wire interface for platforms with multiple I2C buses. **begin()** calls **reset()** which can take up to 15 ms.
@ -58,18 +67,20 @@ after you've performed a new **read()**.
- **int getError()** returns last set error flag and clear it.
Be sure to clear the error flag by calling **getError()** before calling any command as the error flag could be from a previous command.
| Error | Symbolic | Description | Notes |
|:-----:|:--------------------------|:----------------------------|:---------|
| 0x00 | SHT2x_OK | no error | |
| 0x81 | SHT2x_ERR_WRITECMD | I2C write failed | |
| 0x82 | SHT2x_ERR_READBYTES | I2C read failed | |
| 0x83 | SHT2x_ERR_HEATER_OFF | Could not switch off heater | |
| 0x84 | SHT2x_ERR_NOT_CONNECT | Could not connect | |
| 0x85 | SHT2x_ERR_CRC_TEMP | CRC error in temperature | |
| 0x86 | SHT2x_ERR_CRC_HUM | CRC error in humidity | |
| 0x87 | SHT2x_ERR_CRC_STATUS | CRC error in status field | not used |
| 0x88 | SHT2x_ERR_HEATER_COOLDOWN | Heater need to cool down | |
| 0x88 | SHT2x_ERR_HEATER_ON | Could not switch on heater | |
| Value | Symbolic | Description | Notes |
|:------:|:--------------------------|:----------------------------|:---------|
| 0x00 | SHT2x_OK | no error | |
| 0x81 | SHT2x_ERR_WRITECMD | I2C write failed | |
| 0x82 | SHT2x_ERR_READBYTES | I2C read failed | |
| 0x83 | SHT2x_ERR_HEATER_OFF | Could not switch off heater | |
| 0x84 | SHT2x_ERR_NOT_CONNECT | Could not connect | |
| 0x85 | SHT2x_ERR_CRC_TEMP | CRC error in temperature | |
| 0x86 | SHT2x_ERR_CRC_HUM | CRC error in humidity | |
| 0x87 | SHT2x_ERR_CRC_STATUS | CRC error in status field | not used |
| 0x88 | SHT2x_ERR_HEATER_COOLDOWN | Heater need to cool down | |
| 0x88 | SHT2x_ERR_HEATER_ON | Could not switch on heater | |
Note: the HTU20 / HTU21 classes share the same error codes.
#### Heater interface
@ -99,15 +110,21 @@ Returns false if fails, setting error to **SHT2x_ERR_HEATER_OFF**.
#### Status fields
TODO find information as datasheet is minimal.
From HTU20 datasheet
| bits | value | meaning |
|:------:|:------:|:--------------------|
| 00 | 0 | open circuit |
| 01 | 1 | temperature reading |
| 10 | 2 | humidity reading |
| 11 | 3 | closed circuit |
## Future
- test test test test
- **getSerialNumber()**
- improve error handling / status. (all code paths)
- status bits ...
- improve error handling (all code paths)
- investigate blocking delay() in read - optimize... Q: need async interface?

View File

@ -1,7 +1,7 @@
//
// FILE: SHT2x.cpp
// AUTHOR: Rob Tillaart, Viktor Balint
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
@ -9,6 +9,8 @@
// HISTORY:
// 0.1.0 2021-09-25 initial version
// 0.1.1 2021-09-28 Add HTU2x derived classes,
// update readme.md + add some status info
#include "SHT2x.h"
@ -91,7 +93,7 @@ bool SHT2x::read()
_rawTemperature &= 0xFFFC;
_status = buffer[1] & 0x0003;
if (_status == 0xFF)
if (_status == 0xFF) // TODO != 0x01
{
_error = SHT2x_ERR_READBYTES;
return false;
@ -111,7 +113,7 @@ bool SHT2x::read()
_rawHumidity &= 0xFFFC;
_status = buffer[1] & 0x0003;
if (_status == 0xFF)
if (_status == 0xFF) // TODO != 0x02
{
_error = SHT2x_ERR_READBYTES;
return false;
@ -324,5 +326,18 @@ SHT25::SHT25()
};
////////////////////////////////////////////////////////
//
// DERIVED HTU
//
HTU20::HTU20()
{
};
HTU21::HTU21()
{
};
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: SHT2x.h
// AUTHOR: Rob Tillaart, Viktor Balint
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
@ -13,15 +13,18 @@
#include "Wire.h"
#define SHT2x_LIB_VERSION (F("0.1.0"))
#define SHT2x_LIB_VERSION (F("0.1.1"))
// fields getStatus TODO meaning?
#define SHT2x_STATUS_ 0x00
// fields getStatus
#define SHT2x_STATUS_OPEN_CIRCUIT 0x00
#define SHT2x_STATUS_TEMPERATURE 0x01
#define SHT2x_STATUS_HUMIDITY 0x02
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
// error codes
// kept in sync with SHT31 library
// error codes
// kept in sync with SHT31 library
#define SHT2x_OK 0x00
#define SHT2x_ERR_WRITECMD 0x81
#define SHT2x_ERR_READBYTES 0x82
@ -44,10 +47,10 @@ public:
#endif
bool begin(TwoWire *wire = &Wire);
// check sensor is reachable over I2C
// check sensor is reachable over I2C
bool isConnected();
// read must be called first...
// read must be called get getTemperature / getHumidity
bool read();
float getTemperature();
@ -55,20 +58,28 @@ public:
uint16_t getRawTemperature() { return _rawHumidity; };
uint16_t getRawHumidity() { return _rawTemperature; };
// might take up to 15 milliseconds.
// might take up to 15 milliseconds.
bool reset();
// details see datasheet; summary in SHT2x.cpp file
// from datasheet HTU20
//
// | bits | value | meaning |
// |:------:|:------:|:--------------------|
// | 00 | 0 | open circuit |
// | 01 | 1 | temperature reading |
// | 10 | 2 | humidity reading |
// | 11 | 3 | closed circuit |
//
uint8_t getStatus();
// lastRead is in milliSeconds since start
uint32_t lastRead() { return _lastRead; };
// HEATER
// do not use heater for long periods,
// use it for max 3 minutes to heat up
// and let it cool down at least 3 minutes.
// HEATER
// do not use heater for long periods,
// use it for max 3 minutes to heat up
// and let it cool down at least 3 minutes.
void setHeatTimeout(uint8_t seconds);
uint8_t getHeatTimeout() { return _heatTimeout; };
@ -105,7 +116,7 @@ private:
////////////////////////////////////////////////////////
//
// DERIVED
// DERIVED SHT
//
class SHT20 : public SHT2x
{
@ -113,6 +124,7 @@ public:
SHT20();
};
class SHT21 : public SHT2x
{
public:
@ -127,4 +139,22 @@ public:
};
////////////////////////////////////////////////////////
//
// DERIVED HTU
//
class HTU20 : public SHT2x
{
public:
HTU20();
};
class HTU21 : public SHT2x
{
public:
HTU21();
};
// -- END OF FILE --

View File

@ -34,7 +34,7 @@ void loop()
{
for (uint32_t I2Cfreq = 100000; I2Cfreq < 600000; I2Cfreq += 50000)
{
Serial.print(I2Cfreq/1000);
Serial.print(I2Cfreq / 1000);
Wire.setClock(I2Cfreq);
test();
}

View File

@ -6,6 +6,9 @@
// URL: https://github.com/RobTillaart/SHT2x
// WARNING: NOT TESTED FOR SHT2x - UNDER DEVELOPMENT
#include "Wire.h"
#include "SHT2x.h"
@ -54,7 +57,7 @@ void printHeaterStatus(uint8_t status)
{
Serial.print(millis());
Serial.print("\tHEATER: ");
if (status == 0x00) // todo - elaborate
if (status == 0x00) // TODO - elaborate
{
Serial.println("ON");
} else {

View File

@ -8,6 +8,7 @@
#include "Wire.h"
uint8_t status = 0;
void setup()
{
@ -25,19 +26,25 @@ void loop()
{
delay(1000);
float t = getTemperature();
Serial.println(t, 1);
Serial.print(t, 2);
Serial.print("\t");
Serial.println(status, HEX);
delay(1000);
float h = getHumidity();
Serial.println(h, 1);
Serial.print(h, 2);
Serial.print("\t");
Serial.println(status, HEX);
}
///////////////////////////////////////////////////
void testConnected()
{
Wire.beginTransmission(0x40);
int rv = Wire.endTransmission();
if (rv != 0) Serial.println("SHT2x_ERR_NOT_CONNECT");
if (rv != 0) Serial.println("CONNECT FAILED");
else Serial.println("CONNECTED");
}
@ -68,6 +75,7 @@ float getTemperature()
raw = Wire.read() << 8;
raw += Wire.read();
status = raw & 0x0003;
raw &= 0xFFFC;
Serial.print("RAW TEM: ");
@ -102,6 +110,7 @@ float getHumidity()
raw = Wire.read() << 8;
raw += Wire.read();
status = raw & 0x0003;
raw &= 0xFFFC;
Serial.print("RAW HUM: ");

View File

@ -3,6 +3,11 @@
# Data types (KEYWORD1)
SHT2x KEYWORD1
SHT20 KEYWORD1
SHT21 KEYWORD1
SHT25 KEYWORD1
HTU20 KEYWORD1
HTU21 KEYWORD1
# Methods and Functions (KEYWORD2)

View File

@ -1,7 +1,7 @@
{
"name": "SHT2x",
"keywords": "SHT2x Temperature Humidity I2C SHT20 SHT21 SHT25",
"description": "Arduino library for the I2C SHT2x series temperature and humidity sensor",
"keywords": "Temperature Humidity I2C SHT20 SHT21 SHT25 HTU20D HTU20DF HTU21D HTU21DF",
"description": "Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor. Works also for HTU2xD(F) series.",
"authors":
[
{
@ -18,7 +18,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/SHT2x.git"
},
"version": "0.1.0",
"version": "0.1.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,9 +1,9 @@
name=SHT2x
version=0.1.0
version=0.1.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT20, SHT21 and SHT25 temperature and humidity sensor.
paragraph=Supports SHT20 and SHT25 too.
sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor.
paragraph=Works also for HTU20D(F) and HTU21D(F) series.
category=Sensors
url=https://github.com/RobTillaart/SHT2x
architectures=*