0.2.0 CHT8310

This commit is contained in:
Rob Tillaart 2024-02-19 10:52:05 +01:00
parent 009b1f40b4
commit 28bce3a2fd
19 changed files with 1177 additions and 110 deletions

View File

@ -6,6 +6,28 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - 2024-02-16
- redo humidity, Kudos to YouCanNotBeSerious for testing!
- disabled conversion delay handling for now
- add **uint16_t getConfiguration()**
- add **void setConfiguration(uint16_t mask)**
- add several examples
- update readme.md
- minor edits.
## [0.1.1] - 2024-02-14 (not released)
- handle overflow bit in humidity
- add **ALERT** temperature and humidity limit functions
- add **getStatusRegister()**
- add **softwareReset()**
- add **setConvertRate(uint8_t rate = 4)**
- add **getConvertRate()**
- add **oneShotConversion()**
- update readme.md
- update keywords.txt
- minor edits
## [0.1.0] - 2024-02-04
- initial version.

View File

@ -1,7 +1,7 @@
//
// FILE: CHT8310.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: Arduino library for CHT8310 temperature and humidity sensor
// URL: https://github.com/RobTillaart/CHT8310
@ -9,6 +9,23 @@
#include "CHT8310.h"
// REGISTERS
#define CHT8310_REG_TEMPERATURE 0x00
#define CHT8310_REG_HUMIDITY 0x01
#define CHT8310_REG_STATUS 0x02
#define CHT8310_REG_CONFIG 0x03
#define CHT8310_REG_CONVERT_RATE 0x04
#define CHT8310_REG_TEMP_HIGH_LIMIT 0x05
#define CHT8310_REG_TEMP_LOW_LIMIT 0x06
#define CHT8310_REG_HUM_HIGH_LIMIT 0x07
#define CHT8310_REG_HUM_LOW_LIMIT 0x08
#define CHT8310_REG_ONESHOT 0x0F
#define CHT8310_REG_SWRESET 0xFC
#define CHT8310_REG_MANUFACTURER 0xFF
/////////////////////////////////////////////////////
//
// PUBLIC
@ -23,7 +40,7 @@ CHT8310::CHT8310(const uint8_t address, TwoWire *wire)
int CHT8310::begin()
{
// address = 0x40, 0x44, 0x48, 0x4C
if ((_address != 0x40) && (_address != 0x44) && (_address != 0x48) && (_address != 0x4C))
if ((_address != 0x40) && (_address != 0x44) && (_address != 0x48) && (_address != 0x4C))
{
return CHT8310_ERROR_ADDR;
}
@ -58,8 +75,13 @@ int CHT8310::read()
}
_lastRead = millis();
uint8_t data[4] = { 0, 0, 0, 0 };
_readRegister(CHT8310_REG_TEMPERATURE, &data[0], 4);
// TEMPERATURE PART
uint8_t data[2] = { 0, 0 };
int status = _readRegister(CHT8310_REG_TEMPERATURE, &data[0], 2);
if (status != CHT8310_OK)
{
return status;
}
// DATASHEET P13
int16_t tmp = (data[0] << 8 | data[1]);
@ -71,14 +93,30 @@ int CHT8310::read()
{
_temperature = (tmp >> 2) * 0.03125;
}
// DATASHEET P14
tmp = data[2] << 8 | data[3];
_humidity = tmp * (1.0 / 327.67); // == / 32767 * 100%
// Handle temperature offset.
if (_tempOffset != 0.0) _temperature += _tempOffset;
// HUMIDITY PART
status = _readRegister(CHT8310_REG_HUMIDITY, &data[0], 2);
if (status != CHT8310_OK)
{
return status;
}
// DATASHEET P14
tmp = data[0] << 8 | data[1];
if (tmp & 0x8000) // test overflow bit
{
_humidity = 100.0;
return CHT8310_ERROR_HUMIDITY;
}
tmp &= 0x7FFF;
_humidity = tmp * (1.0 / 327.67); // == / 32767 * 100%
// Handle humidity offset.
if (_humOffset != 0.0)
{
_humidity += _humOffset;
// handle out of range.
if (_humidity < 0.0) _humidity = 0.0;
if (_humidity > 100.0) _humidity = 100.0;
}
@ -89,18 +127,10 @@ int CHT8310::read()
int CHT8310::readTemperature()
{
// do not read too fast
if (millis() - _lastRead < 1000)
{
return CHT8310_ERROR_LASTREAD;
}
_lastRead = millis();
uint8_t data[2] = { 0, 0 };
_readRegister(CHT8310_REG_TEMPERATURE, &data[0], 2);
int16_t tmp = readRegister(CHT8310_REG_TEMPERATURE);
// DATASHEET P13
int16_t tmp = (data[0] << 8 | data[1]);
if (_resolution == 13)
{
_temperature = (tmp >> 3) * 0.03125;
@ -121,23 +151,23 @@ int CHT8310::readTemperature()
int CHT8310::readHumidity()
{
// do not read too fast
if (millis() - _lastRead < 1000)
{
return CHT8310_ERROR_LASTREAD;
}
_lastRead = millis();
uint8_t data[2] = { 0, 0 };
_readRegister(CHT8310_REG_HUMIDITY, &data[0], 2);
int16_t tmp = readRegister(CHT8310_REG_HUMIDITY);
// DATASHEET P14
int16_t tmp = data[0] << 8 | data[1];
if (tmp & 0x8000) // test overflow bit
{
_humidity = 100.0;
return CHT8310_ERROR_HUMIDITY;
}
tmp &= 0x7FFF;
_humidity = tmp * (1.0 / 327.67); // == / 32767 * 100%
if (_humOffset != 0.0)
// Handle humidity offset.
if (_humOffset != 0.0)
{
_humidity += _humOffset;
// handle out of range.
if (_humidity < 0.0) _humidity = 0.0;
if (_humidity > 100.0) _humidity = 100.0;
}
@ -178,6 +208,10 @@ uint8_t CHT8310::getConversionDelay()
}
////////////////////////////////////////////////
//
// OFFSET
//
void CHT8310::setHumidityOffset(float offset)
{
_humOffset = offset;
@ -202,16 +236,107 @@ float CHT8310::getTemperatureOffset()
}
////////////////////////////////////////////////
//
// CONFIGURATION
//
void CHT8310::setConfiguration(uint16_t mask)
{
writeRegister(CHT8310_REG_CONFIG, mask);
}
uint16_t CHT8310::getConfiguration()
{
return readRegister(CHT8310_REG_CONFIG);
}
////////////////////////////////////////////////
//
// CONVERT RATE
//
void CHT8310::setConvertRate(uint8_t rate)
{
if (rate > 7) rate = 7;
writeRegister(CHT8310_REG_CONVERT_RATE, ((uint16_t)rate) << 8);
}
uint8_t CHT8310::getConvertRate()
{
return (readRegister(CHT8310_REG_CONVERT_RATE) >> 8) & 0x07;
}
////////////////////////////////////////////////
//
// ALERT
//
void CHT8310::setTemperatureHighLimit(float temperature)
{
int16_t tmp = round(temperature * (1.0 / 0.03125));
tmp <<= 3;
writeRegister(CHT8310_REG_TEMP_HIGH_LIMIT, tmp);
}
void CHT8310::setTemperatureLowLimit(float temperature)
{
int16_t tmp = round(temperature * (1.0 / 0.03125));
tmp <<= 3;
writeRegister(CHT8310_REG_TEMP_LOW_LIMIT, tmp);
}
void CHT8310::setHumidityHighLimit(float humidity)
{
int16_t hum = round(humidity * 327.67);
writeRegister(CHT8310_REG_HUM_HIGH_LIMIT, hum);
}
void CHT8310::setHumidityLowLimit(float humidity)
{
int16_t hum = round(humidity * 327.67);
writeRegister(CHT8310_REG_HUM_LOW_LIMIT, hum);
}
////////////////////////////////////////////////
//
// STATUS
//
uint16_t CHT8310::getStatusRegister()
{
return readRegister(CHT8310_REG_STATUS);
}
////////////////////////////////////////////////
//
// ONE SHOT
//
void CHT8310::oneShotConversion()
{
writeRegister(CHT8310_REG_ONESHOT, 0xFFFF);
}
////////////////////////////////////////////////
//
// SOFTWARE RESET
//
void CHT8310::softwareReset()
{
writeRegister(CHT8310_REG_SWRESET, 0xFFFF);
}
////////////////////////////////////////////////
//
// META DATA
//
uint16_t CHT8310::getManufacturer()
{
uint8_t data[2] = { 0, 0 };
_readRegister(CHT8310_REG_MANUFACTURER, &data[0], 2);
uint16_t tmp = data[0] << 8 | data[1];
return tmp;
return readRegister(CHT8310_REG_MANUFACTURER);
}
@ -231,8 +356,8 @@ uint16_t CHT8310::readRegister(uint8_t reg)
int CHT8310::writeRegister(uint8_t reg, uint16_t value)
{
uint8_t data[2];
data[0] = value & 0xFF;
data[1] = value >> 8;
data[1] = value & 0xFF;
data[0] = value >> 8;
return _writeRegister(reg, data, 2);
}
@ -246,20 +371,20 @@ int CHT8310::_readRegister(uint8_t reg, uint8_t * buf, uint8_t size)
_wire->beginTransmission(_address);
_wire->write(reg);
int n = _wire->endTransmission();
if (n != 0) return CHT8310_ERROR_I2C;
if (reg == CHT8310_REG_TEMPERATURE) // wait for conversion...
if (n != 0)
{
delay(_conversionDelay); // 2x 6.5 ms @ 14 bit = 14 (10 works).
return CHT8310_ERROR_I2C;
}
n = _wire->requestFrom(_address, size);
if (n == size)
if (n != size)
{
for (uint8_t i = 0; i < size; i++)
{
buf[i] = _wire->read();
}
return CHT8310_ERROR_I2C;
}
for (uint8_t i = 0; i < size; i++)
{
buf[i] = _wire->read();
}
return CHT8310_OK;
}
@ -274,7 +399,10 @@ int CHT8310::_writeRegister(uint8_t reg, uint8_t * buf, uint8_t size)
_wire->write(buf[i]);
}
int n = _wire->endTransmission();
if (n != 0) return CHT8310_ERROR_I2C;
if (n != 0)
{
return CHT8310_ERROR_I2C;
}
return CHT8310_OK;
}

View File

@ -2,7 +2,7 @@
//
// FILE: CHT8310.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: Arduino library for CHT8310 temperature and humidity sensor
// URL: https://github.com/RobTillaart/CHT8310
//
@ -12,42 +12,22 @@
#include "Wire.h"
#define CHT8310_LIB_VERSION (F("0.1.0"))
#define CHT8310_LIB_VERSION (F("0.2.0"))
// DEFAULT ADDRESS
#ifndef CHT8310_DEFAULT_ADDRESS
#define CHT8310_DEFAULT_ADDRESS 0x40
#endif
// ERRORS
#define CHT8310_OK 0
#define CHT8310_ERROR_ADDR -10
#define CHT8310_ERROR_I2C -11
#define CHT8310_ERROR_CONNECT -12
#define CHT8310_ERROR_LASTREAD -20
// REGISTERS
#define CHT8310_REG_TEMPERATURE 0x00
#define CHT8310_REG_HUMIDITY 0x01
#define CHT8310_REG_SWRESET 0xFC
#define CHT8310_REG_MANUFACTURER 0xFF
// not implemented in 0.1.0
#define CHT8310_REG_STATUS 0x02
#define CHT8310_REG_CONFIG 0x03
#define CHT8310_REG_CONVERT_RATE 0x04
#define CHT8310_REG_TEMP_HIGH_LIMIT 0x05
#define CHT8310_REG_TEMP_LOW_LIMIT 0x06
#define CHT8310_REG_HUM_HIGH_LIMIT 0x07
#define CHT8310_REG_HUM_LOW_LIMIT 0x08
#define CHT8310_REG_ONESHOT 0x0F
// REGISTER MASKS
// not implemented in 0.1.0
#define CHT8310_ERROR_HUMIDITY -30
class CHT8310
@ -70,29 +50,59 @@ public:
// lastRead is in MilliSeconds since start sketch
uint32_t lastRead();
float getHumidity(); // get cached value
float getTemperature(); // get cached value
float getHumidity(); // get cached value
// for read function
// (not functional yet)
void setConversionDelay(uint8_t cd = 14);
uint8_t getConversionDelay();
// adding offsets works well in normal range
void setHumidityOffset(float offset);
// might introduce under- or overflow at the ends of the sensor range
void setHumidityOffset(float offset);
void setTemperatureOffset(float offset);
float getHumidityOffset();
float getTemperatureOffset();
// CONFIGURATION (elementary, see datasheet for bits);
void setConfiguration(uint16_t mask);
uint16_t getConfiguration(); // returns mask
// CONVERT RATE
void setConvertRate(uint8_t rate = 4);
uint8_t getConvertRate();
// ALERT (not tested, under development)
void setTemperatureHighLimit(float temperature);
void setTemperatureLowLimit(float temperature);
void setHumidityHighLimit(float humidity);
void setHumidityLowLimit(float humidity);
// STATUS (not tested, under development)
uint16_t getStatusRegister();
// ONE SHOT (not tested, under development)
void oneShotConversion();
// SOFTWARE RESET (not tested, under development)
void softwareReset();
// META DATA
uint16_t getManufacturer(); // expect 0x5959
uint16_t getManufacturer(); // expect 0x5959
// PATCH TO ACCESS REGISTERS
uint16_t readRegister(uint8_t reg);
int writeRegister(uint8_t reg, uint16_t value);
private:
float _humOffset = 0.0;

View File

@ -39,12 +39,13 @@ If you are able to test this library, please share your experiences.
#### Tests
- not done yet.
Many initial tests performed by YouCanNotBeSerious. Thanks.
#### Related
- https://github.com/RobTillaart/CHT8305
- https://github.com/RobTillaart/Temperature (e.g. heatIndex)
- https://github.com/RobTillaart/CHT8305
## Hardware
@ -77,12 +78,36 @@ Pull ups are needed on SDA, SCL.
#### performance
I2C bus speeds is supported up to 400 KHz.
I2C bus speeds is supported up to 1000 KHz (datasheet P4).
An indicative table of times in micros on an ESP32-C3 for various I2C clock speeds.
Note that the performance gain of higher clock speeds become less and less.
At the same time the robustness of the signal decreases (not visible in the table).
Times in micros on an ESP32-C3 (See #3 for more)
| Version | Speed | READ | READ T | READ H | getManufacturer |
|:---------:|:--------:|:--------:|:--------:|:--------:|:----------------:|
| 0.2.0 | 50000 | 2165 | 1060 | 1058 | 1051 |
| 0.2.0 | 100000 | 1164 | 582 | 582 | 578 |
| 0.2.0 | 150000 | 855 | 427 | 427 | 424 |
| 0.2.0 | 200000 | 654 | 327 | 327 | 323 |
| 0.2.0 | 250000 | 561 | 281 | 281 | 277 |
| 0.2.0 | 300000 | 491 | 245 | 245 | 241 |
| 0.2.0 | 400000 | 417 | 209 | 209 | 204 |
| 0.2.0 | 500000 | 371 | 186 | 186 | 181 |
| 0.2.0 | 600000 | 344 | 172 | 172 | 169 |
The **read()** call uses two I2C calls so it makes sense that it takes twice
as long as **readTemperature()**, **readHumidity()** and **getManufacturer()**
which only use one I2C call to the device.
**getManufacturer()** is a bit faster as **readTemperature()** as the latter
needs to do conversion math.
#### Addresses
Check datasheet
The CHT8310 supports up to 4 devices on the I2C bus.
| AD0 | Address | Notes |
|:-----:|:----------:|:--------|
@ -130,23 +155,29 @@ Returns error status.
#### Core
- **int read()** reads both the temperature and humidity.
Can be called once per second.
- **int readTemperature()** read only temperature (slightly faster than read)
- **int readHumidity()** read only humidity (slightly faster than read)
- **int read()** reads both the temperature and humidity from the sensor.
Can be called at most once per second, otherwise it will return **CHT8310_ERROR_LASTREAD**
Return should be tested and be **CHT8310_OK**.
- **int readTemperature()** read only temperature from the sensor, therefore faster than read().
This function does not check the lastRead flag, but does set it.
- **int readHumidity()** read only humidity from the sensor, therefore faster than read().
This function does not check the lastRead flag, but does set it.
- **uint32_t lastRead()** returns lastRead in MilliSeconds since start sketch.
Useful to check when it is time to call **read()** again, or for logging.
- **float getHumidity()** returns last humidity read.
Will return the same value until **read()** or **readTemperature()** is called again.
- **float getTemperature()** returns last temperature read.
Will return the same value until **read()** or **readTemperature()** is called again.
- **float getHumidity()** returns last humidity read.
Will return the same value until **read()** or **readHumidity()** is called again.
Note: read(), readTemperature() and readHumidity() blocks each other,
so you can call only one of them every second.
so you can call only one of them every second (see Convert Rate below).
#### Conversion delay
Not functional for now, need investigation.
Check datasheet for details.
- **void setConversionDelay(uint8_t cd = 11)** default is 11 milliseconds (datasheet P8).
Not tested what is the optimum.
- **uint8_t getConversionDelay()** returns set value.
@ -156,10 +187,11 @@ Not tested what is the optimum.
Adding offsets works well in the "normal range" but might introduce
under- or overflow at the ends of the sensor range.
These are not handled for temperature by the library (humidity since 0.1.7).
These are not handled for temperature by the library, humidity is constrained.
- **void setHumidityOffset(float offset)** idem.
- **void setTemperatureOffset(float offset)** idem.
This function can be used to set return temperature in Kelvin!
- **float getHumidityOffset()** idem.
- **float getTemperatureOffset()** idem.
@ -168,43 +200,139 @@ consider a mapping function for temperature and humidity.
e.g. https://github.com/RobTillaart/MultiMap
#### Configuration
To be elaborated (table / functions)
Check datasheet for details about the bit fields.
- **void setConfiguration(uint16_t mask)** set a bit mask
- **uint16_t getConfiguration()** returns current mask
#### Convert Rate
Check datasheet for details.
The idea is that longer conversion times stabilize the measurement.
- **void setConvertRate(uint8_t rate = 4)** set convert rate, see table below.
Default value = 4 meaning
- **uint8_t getConvertRate()** returns the set rate.
| value | measurement | frequency | notes |
|:------:|:-------------:|:-----------:|:-------:|
| 0 | 120 s | 1/120 Hz |
| 1 | 60 s | 1/60 Hz |
| 2 | 10 s | 0.1 Hz |
| 3 | 5 s | 0.2 Hz |
| 4 | 1 s | 1 Hz | default
| 5 | 500 ms | 2 Hz |
| 6 | 250 ms | 4 Hz |
| 7 | 125 ms | 8 Hz |
#### ALERT
Check datasheet for details.
Minimal API to set thresholds to trigger the ALERT pin.
No range check (e.g. low < high) is made.
ALERT pin triggers default on both temperature and humidity.
- **void setTemperatureHighLimit(float temperature)**
- **void setTemperatureLowLimit(float temperature)**
- **void setHumidityHighLimit(float humidity)**
- **void setHumidityLowLimit(float humidity)**
See also **getStatusRegister()**
#### Status register
Check datasheet for details.
- ** uint16_t getStatusRegister()**
| bit | name | description |
|:-----:|:-------:|:-------------|
| 15 | Busy | idem
| 14 | Thigh | temperature exceeded
| 13 | Tlow | temperature exceeded
| 12 | Hhigh | humidity exceeded
| 11 | Hlow | humidity exceeded
#### OneShot conversion
Check datasheet for details.
- **void oneShotConversion()** idem.
#### SoftwareReset
- **void softwareReset()** idem.
#### Meta data
- **uint16_t getManufacturer()** returns 0x5959.
- **uint16_t getManufacturer()** Returns 0x5959 according to the datasheet,
However in #3 a value of 8215 is seen. Seems to be an other manufacturer.
#### Register Access
Temporary wrappers to access the registers.
Read the datasheet for the details.
Check datasheet for details.
- **uint16_t readRegister(uint8_t reg)**
- **int writeRegister(uint8_t reg, uint16_t value)**
## Error codes
| value | define | notes |
|:-------:|:-------------------------|:--------|
| 0 | CHT8310_OK |
| -10 | CHT8310_ERROR_ADDR |
| -11 | CHT8310_ERROR_I2C |
| -12 | CHT8310_ERROR_CONNECT |
| -20 | CHT8310_ERROR_LASTREAD |
| -30 | CHT8310_ERROR_HUMIDITY |
## Future
#### Must
- elaborate documentation.
- test with hardware
- test with hardware
- implement configuration functions.
#### Should
#### Could
- implement more functionality
- keep in sync with CHT8305 where possible
- (unit) test where possible
- refactor.
- test different platforms
- AVR, ESP32, ESP8266, STM32, RP2040, ...
- add examples
- performance.
- improve error handling
- **int lastError()**
- forward return values
#### Could missing functionality
- Configuration register => 10 fields, see datasheet
- EM flag for resolution
- OneShot
#### Wont
- getters for ALERT limits? (user can track these).
## Support

View File

@ -0,0 +1,73 @@
//
// FILE: CHT8310_minimal.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
// set temperature limits
CHT.setTemperatureHighLimit(30);
CHT.setTemperatureLowLimit(15);
// set humidity limits
CHT.setHumidityHighLimit(70);
CHT.setHumidityLowLimit(40);
delay(1000);
}
void loop()
{
if (millis() - CHT.lastRead() >= 1000)
{
// READ DATA
CHT.read();
Serial.print(millis());
Serial.print('\t');
Serial.print(CHT.getStatusRegister(), HEX);
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
}
// -- END OF FILE --

View File

@ -0,0 +1,84 @@
//
// FILE: CHT8310_array.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
// all valid addresses
// if not connected ==> fail
CHT8310 CHT[4] = { CHT8310(0x40), CHT8310(0x44), CHT8310(0x48), CHT8310(0x4C) };
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
for (int i = 0; i < 4; i++)
{
CHT[i].begin();
delay(1000);
Serial.print(CHT[i].getAddress());
Serial.print("\t");
Serial.print(CHT[i].isConnected());
Serial.print("\t");
Serial.println(CHT[i].getManufacturer());
}
}
void loop()
{
for (int i = 0; i < 4; i++)
{
Serial.print(millis());
Serial.print("\t");
Serial.print(CHT[i].getAddress());
Serial.print("\t");
if (CHT[i].isConnected() == false)
{
Serial.println("not connected.");
continue;
}
else
{
CHT[i].read();
Serial.print(CHT[i].getHumidity());
Serial.print("\t");
Serial.println(CHT[i].getTemperature());
}
}
delay(2000);
}
// -- END OF FILE --

View File

@ -0,0 +1,101 @@
//
// FILE: CHT8310_isConnected.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
// check all valid addresses
// if not connected ==> fail
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40
CHT8310 CHT1(0x44);
CHT8310 CHT2(0x48);
CHT8310 CHT3(0x4C);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
CHT1.begin();
CHT2.begin();
CHT3.begin();
Serial.print("Connect:\t");
Serial.println(CHT.isConnected());
Serial.print("Address:\t");
Serial.println(CHT.getAddress());
Serial.print("Manufac:\t");
Serial.println(CHT.getManufacturer());
delay(1000);
Serial.print("Connect:\t");
Serial.println(CHT1.isConnected());
Serial.print("Address:\t");
Serial.println(CHT1.getAddress());
Serial.print("Manufac:\t");
Serial.println(CHT1.getManufacturer());
delay(1000);
Serial.print("Connect:\t");
Serial.println(CHT2.isConnected());
Serial.print("Address:\t");
Serial.println(CHT2.getAddress());
Serial.print("Manufac:\t");
Serial.println(CHT2.getManufacturer());
delay(1000);
Serial.print("Connect:\t");
Serial.println(CHT3.isConnected());
Serial.print("Address:\t");
Serial.println(CHT3.getAddress());
Serial.print("Manufac:\t");
Serial.println(CHT3.getManufacturer());
delay(1000);
}
void loop()
{
if (millis() - CHT.lastRead() >= 2000)
{
// READ DATA
CHT.read();
Serial.print(millis());
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
}
// -- END OF FILE --

View File

@ -1,8 +1,8 @@
//
// FILE: CHT8305_minimal.ino
// FILE: CHT8310_minimal.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8305
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//

View File

@ -0,0 +1,95 @@
//
// FILE: CHT8310_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: measure performance CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
delay(1000);
for (uint32_t speed = 50000; speed <= 600000; speed += 50000)
{
Wire.setClock(speed);
testPerformance(speed);
}
}
void testPerformance(uint32_t speed)
{
Serial.println();
Serial.println(speed);
delay(100);
start = micros();
CHT.read();
stop = micros();
Serial.print("READ:\t");
Serial.println(stop - start);
delay(500);
start = micros();
CHT.readTemperature();
stop = micros();
Serial.print("READ T:\t");
Serial.println(stop - start);
delay(500);
start = micros();
CHT.readHumidity();
stop = micros();
Serial.print("READ H:\t");
Serial.println(stop - start);
delay(500);
start = micros();
uint16_t m = CHT.getManufacturer();
stop = micros();
Serial.print("M:");
Serial.print(m, HEX);
Serial.print(":\t");
Serial.println(stop - start);
delay(500);
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,73 @@
Example output ESP32-C3
50000
READ: 2166
READ T: 1060
READ H: 1058
M:8215: 1052
100000
READ: 1163
READ T: 582
READ H: 582
M:8215: 578
150000
READ: 855
READ T: 427
READ H: 427
M:8215: 423
200000
READ: 653
READ T: 327
READ H: 327
M:8215: 323
250000
READ: 561
READ T: 281
READ H: 281
M:8215: 277
300000
READ: 491
READ T: 245
READ H: 245
M:8215: 241
350000
READ: 449
READ T: 225
READ H: 225
M:8215: 221
400000
READ: 417
READ T: 209
READ H: 209
M:8215: 204
450000
READ: 389
READ T: 195
READ H: 195
M:8215: 191
500000
READ: 371
READ T: 186
READ H: 186
M:8215: 181
550000
READ: 353
READ T: 176
READ H: 177
M:8215: 172
600000
READ: 344
READ T: 172
READ H: 172
M:8215: 168

View File

@ -0,0 +1,64 @@
//
// FILE: CHT8310_read_separate.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
delay(1000);
}
void loop()
{
if (millis() - CHT.lastRead() >= 2000)
{
// READ DATA in separate calls
CHT.readTemperature();
CHT.readHumidity();
Serial.print(millis());
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
}
// -- END OF FILE --

View File

@ -0,0 +1,61 @@
//
// FILE: CHT8310_setConvertRate.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
CHT.setConvertRate(7); // fastest, expect more noisy readings
delay(1000);
}
void loop()
{
// READ DATA at full speed to show "noisyness"
CHT.read();
Serial.print(millis());
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
// -- END OF FILE --

View File

@ -0,0 +1,71 @@
//
// FILE: CHT8310_softwareReset.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
// set register to non default
CHT.setConvertRate(7);
Serial.println(CHT.getConvertRate()); // should print 7
CHT.softwareReset();
delay(1000);
// read back register
int cr = CHT.getConvertRate();
if (cr == 4)
{
Serial.print("Device reset success: \t");
Serial.println(cr); // should print 4
}
else
{
Serial.print("Device reset failed: \t");
Serial.println(cr);
}
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,69 @@
//
// FILE: CHT8310_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
delay(1000);
}
void loop()
{
if (millis() - CHT.lastRead() >= 1000)
{
// READ DATA
int status = CHT.read();
Serial.print(millis());
Serial.print('\t');
Serial.print(status);
Serial.print('\t');
Serial.print(CHT.readRegister(1));
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.print(CHT.readRegister(0));
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
}
// -- END OF FILE --

View File

@ -0,0 +1,70 @@
//
// FILE: CHT8310_test_offset.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for CHT8310 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/CHT8310
// Always check datasheet - front view
//
// +---------------+
// VCC ----| VCC |
// SDA ----| SDA CHT8310 | CHECK DATASHEET.
// GND ----| GND |
// SCL ----| SCL |
// ? ----| AD0 |
// | |
// ----| |
// +---------------+
//
// check datasheet
// VCC RED
// GND BLACK
// SDA YELLOW
// SCL WHITE
#include "CHT8310.h"
CHT8310 CHT(0x40); // CHT8310_DEFAULT_ADDRESS = 0x40 TODO
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("CHT8310_LIB_VERSION: ");
Serial.println(CHT8310_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
CHT.begin();
// should print 0.00 twice
Serial.println(CHT.getTemperatureOffset());
Serial.println(CHT.getHumidityOffset());
Serial.println();
CHT.setTemperatureOffset(+273.15); // adjusts temperature to Kelvin
CHT.setHumidityOffset(-50); // extreme just for demo
delay(1000);
}
void loop()
{
if (millis() - CHT.lastRead() >= 1000)
{
// READ DATA
CHT.read();
Serial.print(millis());
Serial.print('\t');
Serial.print(CHT.getHumidity());
Serial.print('\t');
Serial.println(CHT.getTemperature());
}
}
// -- END OF FILE --

View File

@ -7,8 +7,12 @@ CHT8310 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
getAddress KEYWORD2
read KEYWORD2
readTemperature KEYWORD2
readHumidity KEYWORD2
lastRead KEYWORD2
getTemperature KEYWORD2
getHumidity KEYWORD2
@ -21,8 +25,34 @@ setTemperatureOffset KEYWORD2
getHumidityOffset KEYWORD2
getTemperatureOffset KEYWORD2
setConfiguration KEYWORD2
getConfiguration KEYWORD2
setConvertRate KEYWORD2
getConvertRate KEYWORD2
setTemperatureHighLimit KEYWORD2
setTemperatureLowLimit KEYWORD2
setHumidityHighLimit KEYWORD2
setHumidityLowLimit KEYWORD2
getStatusRegister KEYWORD2
oneShotConversion KEYWORD2
softwareReset KEYWORD2
getManufacturer KEYWORD2
# Constants (LITERAL1)
CHT8310_LIB_VERSION LITERAL1
CHT8310_DEFAULT_ADDRESS LITERAL1
CHT8310_OK LITERAL1
CHT8310_ERROR_ADDR LITERAL1
CHT8310_ERROR_I2C LITERAL1
CHT8310_ERROR_CONNECT LITERAL1
CHT8310_ERROR_LASTREAD LITERAL1
CHT8310_ERROR_HUMIDITY LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/CHT8310.git"
},
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=CHT8310
version=0.1.0
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for CHT8310 temperature and humidity sensor.

View File

@ -48,18 +48,6 @@ unittest(test_constants_I)
}
unittest(test_constants_II)
{
assertEqual(0x00, CHT8310_REG_TEMPERATURE);
assertEqual(0x01, CHT8310_REG_HUMIDITY);
assertEqual(0xFF, CHT8310_REG_MANUFACTURER);
}
unittest(test_offset)
{
CHT8310 cht(0x40);