mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.2 SHT2x
This commit is contained in:
parent
83184d1900
commit
c842fff9d7
@ -1,7 +1,11 @@
|
||||
compile:
|
||||
# Choosing to run compilation tests on different Arduino platforms
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
- leonardo
|
||||
- due
|
||||
- zero
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
#- mega2560
|
||||
|
@ -8,23 +8,33 @@
|
||||
|
||||
# SHT2x
|
||||
|
||||
Arduino library for the SHT2x and HTU2x temperature and humidity sensors.
|
||||
Arduino library for the SHT2x, HTU2x and Si70xx temperature and humidity sensors.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
This library is not tested extensively yet.
|
||||
It should work for SHT20, SHT21 and SHT25 but these are not tested yet.
|
||||
The SHT2x family of sensors should work up to 400 KHz I2C.
|
||||
|
||||
This library should work for SHT20, SHT21 and SHT25 but these are
|
||||
not tested yet.
|
||||
Furthermore there are a number of compatible sensors, these are not tested either.
|
||||
|
||||
|
||||
| Sensor | Temperature accuracy | Humidity accuracy |
|
||||
|:-------:|:--------------------:|:-----------------:|
|
||||
| SHT20 | ~0.3 | ±3.0 |
|
||||
| SHT21 | ~0.3 | ±3.0 |
|
||||
| SHT25 | ~0.3 | ±1.8 |
|
||||
| HTU20 | | | to-do
|
||||
| HTU21 | | | to-do
|
||||
| Si7013 | | | to-do
|
||||
| Si7020 | | | to-do
|
||||
| Si7021 | | | to-do
|
||||
|
||||
| Sensor | Temperature accuracy | Humidity accuracy |
|
||||
|:------:|:------:|:------:|
|
||||
| SHT20 | ~0.3 | ±3.0 |
|
||||
| SHT21 | ~0.3 | ±3.0 |
|
||||
| SHT25 | ~0.3 | ±1.8 |
|
||||
|
||||
All sensors in this family of sensors have address 0x40 (64 decimal).
|
||||
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.
|
||||
|
||||
|
||||
## Interface
|
||||
@ -39,13 +49,17 @@ All classes below are derived from SHT2x class.
|
||||
- **SHT25()** constructor.
|
||||
- **HTU20()** constructor.
|
||||
- **HTU21()** constructor.
|
||||
- **Si7013()** constructor.
|
||||
- **Si7020()** constructor.
|
||||
- **Si7021()** constructor.
|
||||
|
||||
|
||||
#### Base interface
|
||||
|
||||
- **bool begin(dataPin, clockPin)** begin function for ESP8266 & ESP32;
|
||||
- **bool begin(uint8_t dataPin, uint8_t 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.
|
||||
- **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.
|
||||
- **bool read()** Reads both the temperature and humidity.
|
||||
Initial release has a blocking delay.
|
||||
- **bool isConnected()** check if sensor is reachable over I2C. Returns false if not connected.
|
||||
@ -108,6 +122,13 @@ Returns false if fails, setting error to **SHT2x_ERR_HEATER_OFF**.
|
||||
- **bool isHeaterOn()** is the sensor still in heating cycle?
|
||||
|
||||
|
||||
#### Electronic ID
|
||||
|
||||
- **uint32_t getEIDA()** not tested yet.
|
||||
- **uint32_t getEIDB()** not tested yet.
|
||||
- **uint8_t getFirmwareVersion()** not tested yet.
|
||||
|
||||
|
||||
#### Status fields
|
||||
|
||||
From HTU20 datasheet
|
||||
@ -120,15 +141,16 @@ From HTU20 datasheet
|
||||
| 11 | 3 | closed circuit |
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- test test test test
|
||||
- **getSerialNumber()**
|
||||
- improve error handling (all code paths)
|
||||
- investigate blocking delay() in read - optimize... Q: need async interface?
|
||||
|
||||
|
||||
## Operation
|
||||
|
||||
See examples
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- test test test test test test test and ...
|
||||
- improve documentation
|
||||
- **getSerialNumber()**
|
||||
- improve error handling (all code paths)
|
||||
- investigate blocking delay() in read - optimize... Q: need async interface?
|
||||
- check TODO in code
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: SHT2x.cpp
|
||||
// AUTHOR: Rob Tillaart, Viktor Balint
|
||||
// VERSION: 0.1.1
|
||||
// VERSION: 0.1.2
|
||||
// DATE: 2021-09-25
|
||||
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
|
||||
// URL: https://github.com/RobTillaart/SHT2x
|
||||
@ -11,6 +11,10 @@
|
||||
// 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
|
||||
// 0.1.2 2021-09-29 Add Si70xx derived classes
|
||||
// add getEIDA(), getEIDB(), getFirmwareVersion()
|
||||
// update build-CI, readme.md badges
|
||||
//
|
||||
|
||||
|
||||
#include "SHT2x.h"
|
||||
@ -110,7 +114,7 @@ bool SHT2x::read()
|
||||
// _error = SHT2x_ERR_CRC_HUM;
|
||||
_rawHumidity = buffer[0] << 8;
|
||||
_rawHumidity += buffer[1];
|
||||
_rawHumidity &= 0xFFFC;
|
||||
_rawHumidity &= 0xFFFC;
|
||||
|
||||
_status = buffer[1] & 0x0003;
|
||||
if (_status == 0xFF) // TODO != 0x02
|
||||
@ -149,10 +153,12 @@ bool SHT2x::reset()
|
||||
|
||||
uint8_t SHT2x::getStatus()
|
||||
{
|
||||
return _status; // TODO meaning
|
||||
return _status;
|
||||
}
|
||||
|
||||
|
||||
// HEATER
|
||||
|
||||
void SHT2x::setHeatTimeout(uint8_t seconds)
|
||||
{
|
||||
_heatTimeout = seconds;
|
||||
@ -226,6 +232,45 @@ bool SHT2x::isHeaterOn()
|
||||
}
|
||||
|
||||
|
||||
bool SHT2x::setHeaterLevel(uint8_t level)
|
||||
{
|
||||
if (level > 15)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t heaterReg = 0;
|
||||
writeCmd(0x11); // Read Heater Control Register
|
||||
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
}
|
||||
heaterReg &= 0xF0;
|
||||
heaterReg |= level;
|
||||
if (writeCmd(0x51, heaterReg) == false)
|
||||
{
|
||||
_error = -1;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SHT2x::getHeaterLevel(uint8_t & level)
|
||||
{
|
||||
uint8_t heaterReg = 0;
|
||||
writeCmd(0x11); // Read Heater Control Register
|
||||
if (readBytes(1, (uint8_t *) &heaterReg, 5) == false)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
}
|
||||
level = heaterReg & 0x0F;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int SHT2x::getError()
|
||||
{
|
||||
int rv = _error;
|
||||
@ -234,21 +279,72 @@ int SHT2x::getError()
|
||||
}
|
||||
|
||||
|
||||
uint32_t SHT2x::getEIDA()
|
||||
{
|
||||
uint32_t id = 0;
|
||||
uint8_t buffer[8];
|
||||
writeCmd(0xFA, 0x0F);
|
||||
if (readBytes(8, (uint8_t *) buffer, 10) == false)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
}
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
id <<= 8;
|
||||
id |= buffer[i*2];
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
uint32_t SHT2x::getEIDB()
|
||||
{
|
||||
uint32_t id = 0;
|
||||
uint8_t buffer[8];
|
||||
writeCmd(0xFC, 0xC9);
|
||||
if (readBytes(8, (uint8_t *) buffer, 10) == false)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
}
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
id <<= 8;
|
||||
id |= buffer[i*2];
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
uint8_t SHT2x::getFirmwareVersion()
|
||||
{
|
||||
uint8_t version = 0;
|
||||
writeCmd(0x84, 0xB8);
|
||||
if (readBytes(1, (uint8_t *) &version, 10) == false)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
|
||||
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
|
||||
{
|
||||
// CRC-8 formula from page 14 of SHT spec pdf
|
||||
const uint8_t POLY(0x31);
|
||||
uint8_t crc(0xFF);
|
||||
|
||||
for (uint8_t j = len; j; --j)
|
||||
for (uint8_t j = len; j; --j)
|
||||
{
|
||||
crc ^= *data++;
|
||||
|
||||
for (uint8_t i = 8; i; --i)
|
||||
for (uint8_t i = 8; i; --i)
|
||||
{
|
||||
crc = (crc & 0x80) ? (crc << 1) ^ POLY : (crc << 1);
|
||||
}
|
||||
@ -290,7 +386,7 @@ bool SHT2x::readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration)
|
||||
uint32_t start = millis();
|
||||
while (_wire->available() < n)
|
||||
{
|
||||
if (millis() - start > maxDuration)
|
||||
if (millis() - start > maxDuration)
|
||||
{
|
||||
_error = SHT2x_ERR_READBYTES;
|
||||
return false;
|
||||
@ -309,7 +405,7 @@ bool SHT2x::readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration)
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED
|
||||
// DERIVED SHT
|
||||
//
|
||||
SHT20::SHT20()
|
||||
{
|
||||
@ -340,4 +436,24 @@ HTU21::HTU21()
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED Si70xx
|
||||
//
|
||||
Si7013::Si7013()
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
Si7020::Si7020()
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
Si7021::Si7021()
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: SHT2x.h
|
||||
// AUTHOR: Rob Tillaart, Viktor Balint
|
||||
// VERSION: 0.1.1
|
||||
// VERSION: 0.1.2
|
||||
// DATE: 2021-09-25
|
||||
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
|
||||
// URL: https://github.com/RobTillaart/SHT2x
|
||||
@ -13,7 +13,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define SHT2x_LIB_VERSION (F("0.1.1"))
|
||||
#define SHT2x_LIB_VERSION (F("0.1.2"))
|
||||
|
||||
|
||||
// fields getStatus
|
||||
@ -23,7 +23,7 @@
|
||||
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
|
||||
|
||||
|
||||
// error codes
|
||||
// error codes
|
||||
// kept in sync with SHT31 library
|
||||
#define SHT2x_OK 0x00
|
||||
#define SHT2x_ERR_WRITECMD 0x81
|
||||
@ -87,8 +87,17 @@ public:
|
||||
bool heatOff();
|
||||
bool isHeaterOn(); // is the sensor still heating up?
|
||||
|
||||
bool setHeaterLevel(uint8_t level); // level = 0..15
|
||||
bool getHeaterLevel(uint8_t & level); // 0..15
|
||||
|
||||
|
||||
int getError(); // clears error flag
|
||||
|
||||
// Electronic ID bytes
|
||||
uint32_t getEIDA();
|
||||
uint32_t getEIDB();
|
||||
uint8_t getFirmwareVersion();
|
||||
|
||||
private:
|
||||
uint8_t crc8(const uint8_t *data, uint8_t len);
|
||||
|
||||
@ -142,7 +151,7 @@ public:
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED HTU
|
||||
//
|
||||
//
|
||||
class HTU20 : public SHT2x
|
||||
{
|
||||
public:
|
||||
@ -157,4 +166,29 @@ public:
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED Si70xx
|
||||
//
|
||||
class Si7013 : public SHT2x
|
||||
{
|
||||
public:
|
||||
Si7013();
|
||||
};
|
||||
|
||||
|
||||
class Si7020 : public SHT2x
|
||||
{
|
||||
public:
|
||||
Si7020();
|
||||
};
|
||||
|
||||
|
||||
class Si7021 : public SHT2x
|
||||
{
|
||||
public:
|
||||
Si7021();
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -0,0 +1,49 @@
|
||||
//
|
||||
// FILE: SHT2x_EID.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/SHT2x
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "SHT2x.h"
|
||||
|
||||
uint32_t start;
|
||||
uint32_t stop;
|
||||
|
||||
SHT2x sht;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("SHT2x_LIB_VERSION: \t");
|
||||
Serial.println(SHT2x_LIB_VERSION);
|
||||
|
||||
sht.begin();
|
||||
|
||||
uint32_t EIDA = sht.getEIDA();
|
||||
Serial.print(EIDA, HEX);
|
||||
Serial.println();
|
||||
|
||||
uint32_t EIDB = sht.getEIDB();
|
||||
Serial.print(EIDB, HEX);
|
||||
Serial.println();
|
||||
|
||||
uint8_t firmware = sht.getFirmwareVersion();
|
||||
Serial.print(firmware, HEX);
|
||||
Serial.println();
|
||||
|
||||
Serial.println("\n done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -45,4 +45,6 @@ void loop()
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -0,0 +1,41 @@
|
||||
//
|
||||
// FILE: SHT2x_heaterLevel.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo heaterLevel functions
|
||||
// URL: https://github.com/RobTillaart/SHT2x
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "SHT2x.h"
|
||||
|
||||
SHT2x sht;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("SHT2x_LIB_VERSION: \t");
|
||||
Serial.println(SHT2x_LIB_VERSION);
|
||||
|
||||
sht.begin();
|
||||
|
||||
for (int i = 0 ; i < 16; i++)
|
||||
{
|
||||
sht.setHeaterLevel(i);
|
||||
uint8_t level;
|
||||
sht.getHeaterLevel(level);
|
||||
Serial.println(level);
|
||||
}
|
||||
|
||||
Serial.println("\n Done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -44,3 +44,4 @@ void loop()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
uint8_t status = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
@ -29,9 +29,15 @@ getHeatTimeout KEYWORD2
|
||||
heatOn KEYWORD2
|
||||
heatOff KEYWORD2
|
||||
isHeaterOn KEYWORD2
|
||||
setHeaterLevel KEYWORD2
|
||||
getHeaterLevel KEYWORD2
|
||||
|
||||
getError KEYWORD2
|
||||
|
||||
getEIDA KEYWORD2
|
||||
getEIDB KEYWORD2
|
||||
getFirmwareVersion KEYWORD2
|
||||
|
||||
|
||||
# Instances (KEYWORD2)
|
||||
|
||||
@ -39,6 +45,7 @@ getError KEYWORD2
|
||||
# Constants (LITERAL1)
|
||||
SHT2x_LIB_VERSION LITERAL1
|
||||
SHT2x_OK LITERAL1
|
||||
|
||||
SHT2x_ERR_WRITECMD LITERAL1
|
||||
SHT2x_ERR_READBYTES LITERAL1
|
||||
SHT2x_ERR_HEATER_OFF LITERAL1
|
||||
@ -49,3 +56,8 @@ SHT2x_ERR_CRC_STATUS LITERAL1
|
||||
SHT2x_ERR_HEATER_COOLDOWN LITERAL1
|
||||
SHT2x_ERR_HEATER_ON LITERAL1
|
||||
|
||||
SHT2x_STATUS_OPEN_CIRCUIT LITERAL1
|
||||
SHT2x_STATUS_TEMPERATURE LITERAL1
|
||||
SHT2x_STATUS_HUMIDITY LITERAL1
|
||||
SHT2x_STATUS_CLOSED_CIRCUIT LITERAL1
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "SHT2x",
|
||||
"keywords": "Temperature Humidity I2C SHT20 SHT21 SHT25 HTU20D HTU20DF HTU21D HTU21DF",
|
||||
"keywords": "SHT20 SHT21 SHT25 HTU20D HTU21D",
|
||||
"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.1",
|
||||
"version": "0.1.2",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=SHT2x
|
||||
version=0.1.1
|
||||
version=0.1.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor.
|
||||
|
@ -49,6 +49,17 @@ unittest_teardown()
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
fprintf(stderr, "SHT2x_LIB_VERSION: %s \n", (char *) SHT2x_LIB_VERSION);
|
||||
SHT2x sht;
|
||||
|
||||
bool b = sht.begin();
|
||||
assertEqual(b, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest(test_begin)
|
||||
{
|
||||
SHT2x sht;
|
||||
|
Loading…
Reference in New Issue
Block a user