diff --git a/CHANGELOG.md b/CHANGELOG.md index 65caf11..6f0a235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.2.0] - 2023-12-04 +- refactor API, more in line with other Wire based libs. +- update readme.md +- added **isConnected()** +- added asynchronous interface. +- rewrote the synchronous interface. +- add examples. + +---- + ## [0.1.3] - 2023-11-22 - update readme.md - fix version number - ## [0.1.2] - 2022-11-26 - Add RP2040 support to build-CI. - Add CHANGELOG.md diff --git a/README.md b/README.md index b4ee932..a806314 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,22 @@ Arduino library specific for AVR tiny processors. ## Description -**Experimental** +The tinySHT2x is an **Experimental** Arduino library to read the SHT2X sensor. +This sensor provides temperature and humidity. + +The library is meant for AVR only, tiny platform, so it is minimal (not?) portable. + + +#### 0.2.0 Breaking change + +Version 0.2.0 introduced a breaking change. +Wire parameter has moved from **begin()** to the Constructor. +The user has to call **Wire.begin()** before calling **begin()**. -Not portable, AVR only #### Related -Based upon https://github.com/RobTillaart/SHT2x +- https://github.com/RobTillaart/SHT2x (based upon). ## Interface @@ -31,6 +40,33 @@ Based upon https://github.com/RobTillaart/SHT2x #include "tinySHT2x.h" ``` +#### Functions + +- **tinySHT2x(TwoWire \* wire = &Wire)** Constructor +- **bool begin()** initializes internals. Returns true if device can be found. +- **bool isConnected()** Returns true if device can be found. +- **bool reset()** sends SOFT_RESET command to sensor. +returns false if I2C failed to send it. + + +#### Async interface + +With the Async interface the user must watch keep track of the appropriate +delay between the request and read. +For temperature this is typical around 70 millis and for humidity 30 millis(). + +- **void requestTemperature()** sends GET_TEMPERATURE command to sensor. +- **void requestHumidity()** sends GET_HUMIDITY command to sensor. +- **float readTemperature()** fetches data from the sensor. +- **float readHumidity()** fetches data from the sensor. + +#### Sync interface + +- **float getTemperature(uint8_t del = 70)** requests and read the temperature. +It uses a delay of 70 milliseconds which can be tuned by the user. +- **float getHumidity(uint8_t del = 30)** requests and read the humidity. +It uses a delay of30 milliseconds which can be tuned by the user. + - see https://github.com/RobTillaart/SHT2x @@ -45,6 +81,9 @@ Based upon https://github.com/RobTillaart/SHT2x #### Could +- investigate async interface + - splitting requestHumidity() and getHumidity(). + - idem for temperature. - Can a tiny have another Wire than Wire? - Check the status bit (temperature / humidity flag) - datasheet page 8, LSB bit 1 - bit 0 not used) diff --git a/documents/Download Center - Sensirion.url b/documents/Download Center - Sensirion.url new file mode 100644 index 0000000..4bd37de --- /dev/null +++ b/documents/Download Center - Sensirion.url @@ -0,0 +1,2 @@ +[InternetShortcut] +URL=https://www.sensirion.com/en/download-center/humidity-sensors/humidity-temperature-sensor-sht2x-digital-i2c-accurate/ diff --git a/documents/SHT20_datasheet.pdf b/documents/SHT20_datasheet.pdf new file mode 100644 index 0000000..34678b2 Binary files /dev/null and b/documents/SHT20_datasheet.pdf differ diff --git a/documents/SHT21_datasheet.pdf b/documents/SHT21_datasheet.pdf new file mode 100644 index 0000000..6f1f393 Binary files /dev/null and b/documents/SHT21_datasheet.pdf differ diff --git a/documents/Sensirion_Humidity_SHT21_Sample_Code_V1.2.pdf b/documents/Sensirion_Humidity_SHT21_Sample_Code_V1.2.pdf new file mode 100644 index 0000000..fbe4ed6 Binary files /dev/null and b/documents/Sensirion_Humidity_SHT21_Sample_Code_V1.2.pdf differ diff --git a/documents/Sensirion_Humidity_Sensors_SHT25_Datasheet.pdf b/documents/Sensirion_Humidity_Sensors_SHT25_Datasheet.pdf new file mode 100644 index 0000000..589157d Binary files /dev/null and b/documents/Sensirion_Humidity_Sensors_SHT25_Datasheet.pdf differ diff --git a/examples/tinySHT2x_demo/tinySHT2x_demo.ino b/examples/tinySHT2x_demo/tinySHT2x_demo.ino index f3b0fb2..4371224 100644 --- a/examples/tinySHT2x_demo/tinySHT2x_demo.ino +++ b/examples/tinySHT2x_demo/tinySHT2x_demo.ino @@ -16,6 +16,7 @@ void setup() Serial.begin(115200); Serial.println(__FILE__); + Wire.begin(); sht.begin(); } @@ -29,5 +30,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/tinySHT2x_demo_async/.arduino-ci.yml b/examples/tinySHT2x_demo_async/.arduino-ci.yml new file mode 100644 index 0000000..8d78299 --- /dev/null +++ b/examples/tinySHT2x_demo_async/.arduino-ci.yml @@ -0,0 +1,12 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + # - due + # - zero + - leonardo + # - m4 + #- esp32 + # - esp8266 + - mega2560 + # - trinket diff --git a/examples/tinySHT2x_demo_async/tinySHT2x_demo_async.ino b/examples/tinySHT2x_demo_async/tinySHT2x_demo_async.ino new file mode 100644 index 0000000..25fcc07 --- /dev/null +++ b/examples/tinySHT2x_demo_async/tinySHT2x_demo_async.ino @@ -0,0 +1,38 @@ +// +// FILE: tinySHT2x_demo_async.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// URL: https://github.com/RobTillaart/tinySHT2x + + +#include "Wire.h" +#include "tinySHT2x.h" + +tinySHT2x sht; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + sht.begin(); +} + + +void loop() +{ + sht.requestTemperature(); + delay(65); // tune to work + Serial.print(sht.readTemperature()); + Serial.print("\t"); + + sht.requestHumidity(); + delay(28); // tune to work + Serial.println(sht.getHumidity()); + delay(1000); +} + + +// -- END OF FILE -- diff --git a/examples/tinySHT2x_demo_delay/.arduino-ci.yml b/examples/tinySHT2x_demo_delay/.arduino-ci.yml new file mode 100644 index 0000000..8d78299 --- /dev/null +++ b/examples/tinySHT2x_demo_delay/.arduino-ci.yml @@ -0,0 +1,12 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + # - due + # - zero + - leonardo + # - m4 + #- esp32 + # - esp8266 + - mega2560 + # - trinket diff --git a/examples/tinySHT2x_demo_delay/tinySHT2x_demo_delay.ino b/examples/tinySHT2x_demo_delay/tinySHT2x_demo_delay.ino new file mode 100644 index 0000000..d90e34f --- /dev/null +++ b/examples/tinySHT2x_demo_delay/tinySHT2x_demo_delay.ino @@ -0,0 +1,33 @@ +// +// FILE: tinySHT2x_demo_delay.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// URL: https://github.com/RobTillaart/tinySHT2x + + +#include "Wire.h" +#include "tinySHT2x.h" + +tinySHT2x sht; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + sht.begin(); +} + + +void loop() +{ + Serial.print(sht.getTemperature(65)); // adjust delay if needed + Serial.print("\t"); + Serial.println(sht.getHumidity(28)); // adjust delay if needed + delay(1000); +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index 58935f2..230f953 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,9 +7,16 @@ tinySHT2x KEYWORD1 # Methods and Functions (KEYWORD2) begin KEYWORD2 +isConnected KEYWORD2 +reset KEYWORD2 + +requestTemperature KEYWORD2 +requestHumidity KEYWORD2 +readTemperature KEYWORD2 +readHumidity KEYWORD2 + getTemperature KEYWORD2 getHumidity KEYWORD2 -reset KEYWORD2 # Instances (KEYWORD2) diff --git a/library.json b/library.json index e5c1559..8c26b96 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/tinySHT2x.git" }, - "version": "0.1.3", + "version": "0.2.0", "license": "MIT", "frameworks": "arduino", "platforms": "avr", diff --git a/library.properties b/library.properties index 59a32c5..da097b1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=tinySHT2x -version=0.1.3 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for the SHT20, SHT21 and SHT25 temperature and humidity sensor. Optimized for AVR tiny. diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 58f9857..fef4f8d 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -56,16 +56,19 @@ unittest(test_constant) unittest(test_constructor) { tinySHT2x sht; - + + Wire.begin(); + sht.begin(); assertTrue(sht.reset()); - // need godmode for these - // assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getTemperature(), 0.01); - // assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getHumidity(), 0.01); + // need godmode for these + // assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getTemperature(), 0.01); + // assertEqualFloat(TINY_SHT2x_NO_VALUE, sht.getHumidity(), 0.01); } unittest_main() -// -------- +// -- END OF FILE -- + diff --git a/tinySHT2x.cpp b/tinySHT2x.cpp index 85a3a32..0842c1a 100644 --- a/tinySHT2x.cpp +++ b/tinySHT2x.cpp @@ -1,10 +1,10 @@ // -// FILE: tinytinySHT2x.cpp +// FILE: tinySHT2x.cpp // AUTHOR: Rob Tillaart, Viktor Balint -// VERSION: 0.1.3 +// VERSION: 0.2.0 // DATE: 2021-09-27 // PURPOSE: Arduino library for the SHT2x temperature and humidity sensor optimized for AVR tiny -// URL: https://github.com/RobTillaart/tinytinySHT2x +// URL: https://github.com/RobTillaart/tinySHT2x @@ -16,25 +16,46 @@ #define SHT2x_ADDRESS 0x40 -tinySHT2x::tinySHT2x() +tinySHT2x::tinySHT2x(TwoWire *wire) { + _wire = wire; } -void tinySHT2x::begin(TwoWire *wire) +bool tinySHT2x::begin() { - _wire = wire; - _wire->begin(); + return isConnected(); +} + + +bool tinySHT2x::isConnected() +{ + _wire->beginTransmission(SHT2x_ADDRESS); + return (_wire->endTransmission() == 0); } -float tinySHT2x::getTemperature() +bool tinySHT2x::reset() +{ + return writeCmd(SHT2x_SOFT_RESET); +} + + +/////////////////////////////////// +// +// ASYNC INTERFACE +// +void tinySHT2x::requestTemperature() +{ + writeCmd(SHT2x_GET_TEMPERATURE_NO_HOLD); +} + + +float tinySHT2x::readTemperature() { uint8_t buffer[3]; uint16_t raw; - writeCmd(SHT2x_GET_TEMPERATURE_NO_HOLD); - delay(70); if (readBytes(3, (uint8_t*) &buffer[0], 90) == false) { return TINY_SHT2x_NO_VALUE; @@ -47,14 +68,17 @@ float tinySHT2x::getTemperature() } -float tinySHT2x::getHumidity() +void tinySHT2x::requestHumidity() +{ + writeCmd(SHT2x_GET_HUMIDITY_NO_HOLD); +} + + +float tinySHT2x::readHumidity() { uint8_t buffer[3]; uint16_t raw; - // HUMIDITY - writeCmd(SHT2x_GET_HUMIDITY_NO_HOLD); - delay(30); if (readBytes(3, (uint8_t*) &buffer[0], 30) == false) { return TINY_SHT2x_NO_VALUE; @@ -67,9 +91,23 @@ float tinySHT2x::getHumidity() } -bool tinySHT2x::reset() +/////////////////////////////////// +// +// SYNC INTERFACE +// +float tinySHT2x::getTemperature(uint8_t del) { - return writeCmd(SHT2x_SOFT_RESET); + requestTemperature(); + delay(del); + return readTemperature(); +} + + +float tinySHT2x::getHumidity(uint8_t del) +{ + requestHumidity(); + delay(del); + return readHumidity(); } @@ -77,7 +115,6 @@ bool tinySHT2x::reset() // // PRIVATE // - bool tinySHT2x::writeCmd(uint8_t cmd) { _wire->beginTransmission(SHT2x_ADDRESS); @@ -86,7 +123,7 @@ bool tinySHT2x::writeCmd(uint8_t cmd) } -bool tinySHT2x::readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration) +bool tinySHT2x::readBytes(uint8_t n, uint8_t * val, uint8_t maxDuration) { _wire->requestFrom((uint8_t)SHT2x_ADDRESS, (uint8_t) n); uint32_t start = millis(); diff --git a/tinySHT2x.h b/tinySHT2x.h index c72cb54..3ae6b99 100644 --- a/tinySHT2x.h +++ b/tinySHT2x.h @@ -2,17 +2,17 @@ // // FILE: tinySHT2x.h // AUTHOR: Rob Tillaart, Viktor Balint -// VERSION: 0.1.3 +// VERSION: 0.2.0 // DATE: 2021-09-27 // PURPOSE: Arduino library for the SHT2x temperature and humidity sensor optimized for AVR tiny // URL: https://github.com/RobTillaart/tinySHT2x -// + #include "Arduino.h" #include "Wire.h" -#define TINY_SHT2x_LIB_VERSION (F("0.1.3")) +#define TINY_SHT2x_LIB_VERSION (F("0.2.0")) #define TINY_SHT2x_NO_VALUE -999 @@ -20,16 +20,26 @@ class tinySHT2x { public: - tinySHT2x(); + tinySHT2x(TwoWire *wire = &Wire); - void begin(TwoWire *wire = &Wire); + bool begin(); + bool isConnected(); bool reset(); - float getTemperature(); - float getHumidity(); + + // ASYNC INTERFACE + void requestTemperature(); + void requestHumidity(); + float readTemperature(); + float readHumidity(); + + // SYNC INTERFACE + float getTemperature(uint8_t del = 70); + float getHumidity(uint8_t del = 30); + private: bool writeCmd(uint8_t cmd); - bool readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration); + bool readBytes(uint8_t n, uint8_t * val, uint8_t maxDuration); TwoWire* _wire; };