2021-09-27 14:51:23 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: SHT2x.h
|
2023-11-27 03:44:42 -05:00
|
|
|
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
|
2023-12-07 11:29:13 -05:00
|
|
|
// VERSION: 0.5.0
|
2023-11-27 03:44:42 -05:00
|
|
|
// DATE: 2023-11-25
|
2021-09-27 14:51:23 -04:00
|
|
|
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
|
|
|
|
// URL: https://github.com/RobTillaart/SHT2x
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
2023-12-07 11:29:13 -05:00
|
|
|
#define SHT2x_LIB_VERSION (F("0.5.0"))
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
|
2021-09-29 07:21:17 -04:00
|
|
|
// fields getStatus
|
|
|
|
#define SHT2x_STATUS_OPEN_CIRCUIT 0x00
|
|
|
|
#define SHT2x_STATUS_TEMPERATURE 0x01
|
|
|
|
#define SHT2x_STATUS_HUMIDITY 0x02
|
|
|
|
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
|
2021-11-17 12:59:50 -05:00
|
|
|
// error codes
|
2021-09-29 07:21:17 -04:00
|
|
|
// kept in sync with SHT31 library
|
2021-09-27 14:51:23 -04:00
|
|
|
#define SHT2x_OK 0x00
|
|
|
|
#define SHT2x_ERR_WRITECMD 0x81
|
|
|
|
#define SHT2x_ERR_READBYTES 0x82
|
|
|
|
#define SHT2x_ERR_HEATER_OFF 0x83
|
|
|
|
#define SHT2x_ERR_NOT_CONNECT 0x84
|
|
|
|
#define SHT2x_ERR_CRC_TEMP 0x85
|
|
|
|
#define SHT2x_ERR_CRC_HUM 0x86
|
2023-09-11 04:13:31 -04:00
|
|
|
#define SHT2x_ERR_CRC_STATUS 0x87 // not used
|
2021-09-27 14:51:23 -04:00
|
|
|
#define SHT2x_ERR_HEATER_COOLDOWN 0x88
|
|
|
|
#define SHT2x_ERR_HEATER_ON 0x89
|
2022-07-13 09:46:33 -04:00
|
|
|
// 0.2.0
|
|
|
|
#define SHT2x_ERR_RESOLUTION 0x8A
|
|
|
|
|
2023-09-11 04:13:31 -04:00
|
|
|
// Request types
|
|
|
|
#define SHT2x_REQ_NONE 0x00
|
|
|
|
#define SHT2x_REQ_TEMPERATURE 0x01
|
|
|
|
#define SHT2x_REQ_HUMIDITY 0x02
|
|
|
|
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
SHT2x(TwoWire *wire = &Wire);
|
2021-09-27 14:51:23 -04:00
|
|
|
|
2023-09-21 10:37:38 -04:00
|
|
|
bool begin();
|
2021-09-29 07:21:17 -04:00
|
|
|
// check sensor is reachable over I2C
|
2021-09-27 14:51:23 -04:00
|
|
|
bool isConnected();
|
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TEMPERATURE AND HUMIDTY
|
|
|
|
//
|
2023-11-27 03:44:42 -05:00
|
|
|
// read must be called before calling getTemperature / getHumidity
|
2023-03-26 16:08:27 -04:00
|
|
|
bool read();
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
float getTemperature();
|
|
|
|
float getHumidity();
|
2023-03-26 16:08:27 -04:00
|
|
|
uint16_t getRawTemperature();
|
|
|
|
uint16_t getRawHumidity();
|
2021-09-27 14:51:23 -04:00
|
|
|
|
2021-09-29 07:21:17 -04:00
|
|
|
// might take up to 15 milliseconds.
|
2021-09-27 14:51:23 -04:00
|
|
|
bool reset();
|
|
|
|
|
2021-09-29 07:21:17 -04:00
|
|
|
// from datasheet HTU20
|
|
|
|
//
|
|
|
|
// | bits | value | meaning |
|
|
|
|
// |:------:|:------:|:--------------------|
|
|
|
|
// | 00 | 0 | open circuit |
|
|
|
|
// | 01 | 1 | temperature reading |
|
|
|
|
// | 10 | 2 | humidity reading |
|
|
|
|
// | 11 | 3 | closed circuit |
|
|
|
|
//
|
2021-09-27 14:51:23 -04:00
|
|
|
uint8_t getStatus();
|
|
|
|
|
2023-09-11 04:13:31 -04:00
|
|
|
// lastRead is in milliSeconds since start
|
2023-03-26 16:08:27 -04:00
|
|
|
uint32_t lastRead();
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
2021-09-29 07:21:17 -04:00
|
|
|
// HEATER
|
2023-03-26 16:08:27 -04:00
|
|
|
//
|
2021-09-29 07:21:17 -04:00
|
|
|
// 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.
|
2021-09-27 14:51:23 -04:00
|
|
|
void setHeatTimeout(uint8_t seconds);
|
2023-03-26 16:08:27 -04:00
|
|
|
uint8_t getHeatTimeout();
|
2021-09-27 14:51:23 -04:00
|
|
|
|
|
|
|
bool heatOn();
|
|
|
|
bool heatOff();
|
2023-09-11 04:13:31 -04:00
|
|
|
bool isHeaterOn(); // is the sensor still heating up?
|
2021-09-27 14:51:23 -04:00
|
|
|
|
2023-09-11 04:13:31 -04:00
|
|
|
bool setHeaterLevel(uint8_t level); // level = 0..15
|
|
|
|
bool getHeaterLevel(uint8_t & level); // 0..15
|
2021-11-17 12:59:50 -05:00
|
|
|
|
|
|
|
|
2023-09-11 04:13:31 -04:00
|
|
|
// reading clears error flag
|
|
|
|
int getError();
|
2021-09-27 14:51:23 -04:00
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Electronic Identification Code
|
|
|
|
//
|
|
|
|
// Sensirion_Humidity_SHT2x_Electronic_Identification_Code_V1.1.pdf
|
2021-11-17 12:59:50 -05:00
|
|
|
// Electronic ID bytes
|
2022-12-18 04:41:37 -05:00
|
|
|
uint32_t getEIDA();
|
|
|
|
uint32_t getEIDB();
|
|
|
|
uint8_t getFirmwareVersion();
|
2021-11-17 12:59:50 -05:00
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// RESOLUTION
|
|
|
|
//
|
2022-07-13 09:46:33 -04:00
|
|
|
// experimental 0.2.0 - needs testing.
|
|
|
|
// table 8 SHT20 datasheet
|
|
|
|
// table 7 shows different timing per resolution
|
|
|
|
// every level is roughly factor 2 in time.
|
|
|
|
// RES HUM TEMP
|
|
|
|
// 0 12 bit 14 bit
|
|
|
|
// 1 08 bit 12 bit
|
|
|
|
// 2 10 bit 13 bit
|
|
|
|
// 3 11 bit 11 bit
|
|
|
|
// 4..255 returns false
|
2022-12-18 04:41:37 -05:00
|
|
|
bool setResolution(uint8_t res = 0);
|
2022-07-13 09:46:33 -04:00
|
|
|
// returns RES set (cached value)
|
2022-12-18 04:41:37 -05:00
|
|
|
uint8_t getResolution();
|
2022-07-13 09:46:33 -04:00
|
|
|
|
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ASYNCHRONOUS INTERFACE (experimental)
|
|
|
|
//
|
2022-12-18 04:41:37 -05:00
|
|
|
bool requestTemperature();
|
|
|
|
bool requestHumidity();
|
|
|
|
bool reqTempReady();
|
|
|
|
bool reqHumReady();
|
2023-09-11 04:13:31 -04:00
|
|
|
bool requestReady();
|
2022-12-18 04:41:37 -05:00
|
|
|
bool readTemperature();
|
|
|
|
bool readHumidity();
|
|
|
|
uint32_t lastRequest();
|
2023-09-11 04:13:31 -04:00
|
|
|
uint8_t getRequestType();
|
2022-07-13 09:46:33 -04:00
|
|
|
|
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// OTHER
|
|
|
|
//
|
|
|
|
bool batteryOK();
|
|
|
|
|
|
|
|
|
2022-11-24 08:09:52 -05:00
|
|
|
protected:
|
2021-09-27 14:51:23 -04:00
|
|
|
uint8_t crc8(const uint8_t *data, uint8_t len);
|
|
|
|
|
|
|
|
bool writeCmd(uint8_t cmd);
|
|
|
|
bool writeCmd(uint8_t cmd, uint8_t value);
|
|
|
|
bool readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration);
|
2023-12-04 05:37:43 -05:00
|
|
|
|
|
|
|
/** can be called after requesting temperature or humidity */
|
|
|
|
bool readCachedTemperature();
|
|
|
|
|
2021-09-27 14:51:23 -04:00
|
|
|
TwoWire* _wire;
|
|
|
|
|
|
|
|
uint32_t _lastRead;
|
2022-12-18 04:41:37 -05:00
|
|
|
|
|
|
|
// for async interface
|
|
|
|
uint32_t _lastRequest;
|
|
|
|
// 0 = none 1 = temp 2 = hum
|
|
|
|
uint8_t _requestType;
|
|
|
|
|
|
|
|
uint8_t _heatTimeout; // seconds
|
2021-09-27 14:51:23 -04:00
|
|
|
uint32_t _heaterStart;
|
|
|
|
uint32_t _heaterStop;
|
|
|
|
bool _heaterOn;
|
|
|
|
|
|
|
|
uint16_t _rawHumidity;
|
|
|
|
uint16_t _rawTemperature;
|
|
|
|
|
|
|
|
uint8_t _status;
|
|
|
|
|
|
|
|
uint8_t _error;
|
2022-07-13 09:46:33 -04:00
|
|
|
uint8_t _resolution;
|
2021-09-27 14:51:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2023-03-26 16:08:27 -04:00
|
|
|
// DERIVED SHT classes
|
2021-09-27 14:51:23 -04:00
|
|
|
//
|
|
|
|
class SHT20 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
SHT20(TwoWire *wire = &Wire);
|
2021-09-27 14:51:23 -04:00
|
|
|
};
|
|
|
|
|
2021-09-29 07:21:17 -04:00
|
|
|
|
2021-09-27 14:51:23 -04:00
|
|
|
class SHT21 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
SHT21(TwoWire *wire = &Wire);
|
2021-09-27 14:51:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SHT25 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
SHT25(TwoWire *wire = &Wire);
|
2021-09-27 14:51:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-09-29 07:21:17 -04:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2023-03-26 16:08:27 -04:00
|
|
|
// DERIVED HTU classes
|
2021-11-17 12:59:50 -05:00
|
|
|
//
|
2021-09-29 07:21:17 -04:00
|
|
|
class HTU20 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
HTU20(TwoWire *wire = &Wire);
|
2021-09-29 07:21:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class HTU21 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
HTU21(TwoWire *wire = &Wire);
|
2021-09-29 07:21:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-11-17 12:59:50 -05:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2023-03-26 16:08:27 -04:00
|
|
|
// DERIVED Si70xx classes
|
2021-11-17 12:59:50 -05:00
|
|
|
//
|
|
|
|
class Si7013 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
Si7013(TwoWire *wire = &Wire);
|
2023-12-04 05:37:43 -05:00
|
|
|
using SHT2x::readCachedTemperature;
|
2021-11-17 12:59:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Si7020 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
Si7020(TwoWire *wire = &Wire);
|
2023-12-04 05:37:43 -05:00
|
|
|
using SHT2x::readCachedTemperature;
|
2021-11-17 12:59:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Si7021 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
Si7021(TwoWire *wire = &Wire);
|
2023-12-04 05:37:43 -05:00
|
|
|
using SHT2x::readCachedTemperature;
|
2021-11-17 12:59:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-03-26 16:08:27 -04:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// DERIVED GY21 classes
|
|
|
|
//
|
|
|
|
class GY21 : public SHT2x
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:37:38 -04:00
|
|
|
GY21(TwoWire *wire = &Wire);
|
2023-03-26 16:08:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|