first commit

This commit is contained in:
Alejandro Guerrero 2026-09-27 17:21:39 +02:00
commit e6a42e28e9
4355 changed files with 4626605 additions and 0 deletions

View file

@ -0,0 +1,73 @@
# Change Log DHT20
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.1] - 2024-01-26
- update readme.md
- update examples
## [0.3.0] - 2023-10-25
- simplify begin()
- updated all examples
- update readme.md
----
## [0.2.3] - 2023-07-27
- fix #12 add time out to read function
## [0.2.2] - 2022-12-21
- update keywords.txt
- add defaults to offset functions.
- update readme.md
- update GitHub actions
- update license 2023
## [0.2.1] - 2022-12-21
- first part of the **readStatus()** not part of the protocol
- different interpretation of datasheet page 10 section 7.4
- the state is sent after a read command as first byte
- see https://github.com/RobTillaart/DHT20/issues/8 + page 11 datasheet
- add **getAddress()** convenience function.
## [0.2.0] - 2022-10-30
- add changelog.md
- add rp2040 to build-CI
- add workaround for #8 to readme.md
- fix requestFrom() ambiguity
- fix #8 - embed **resetSensor()** into **read()**.
This makes reads slightly slower.
- update readme.md
----
## [0.1.4] - 2022-09-18
- add resetSensor() code.
- add comments in .h file
- add examples
- stabilize readStatus()
- update readme.md
## [0.1.3] - 2022-09-17
- add wrapper status functions
- improve performance read()
- refactor, update readme.md
## [0.1.2] - 2022-09-16
- fix #4 DHT20_ERROR_BYTES_ALL_ZERO error condition.
- fix keywords
- add readStatus() fix _readStatus()
- add setWireTimeout(250000, true); // in comments
## [0.1.1] - 2022-09-10
- add hardware schema to readme.md.
- fix async interface (first version)
## [0.1.0] - 2022-01-11
- initial version (based upon DHT20 datasheet)

330
libraries/DHT20/DHT20.cpp Normal file
View file

@ -0,0 +1,330 @@
//
// FILE: DHT20.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
#include "DHT20.h"
// set DHT20_WIRE_TIME_OUT to 0 to disable.
// note this timeout is commented in code below.
#define DHT20_WIRE_TIME_OUT 250000 // microseconds
const uint8_t DHT20_ADDRESS = 0x38;
DHT20::DHT20(TwoWire *wire)
{
_wire = wire;
// reset() ?
_temperature = 0;
_humidity = 0;
_humOffset = 0;
_tempOffset = 0;
_status = DHT20_OK;
_lastRequest = 0;
_lastRead = 0;
}
bool DHT20::begin()
{
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}
bool DHT20::isConnected()
{
_wire->beginTransmission(DHT20_ADDRESS);
int rv = _wire->endTransmission();
return rv == 0;
}
uint8_t DHT20::getAddress()
{
return DHT20_ADDRESS;
}
// See datasheet 7.4 Sensor Reading Process, point 1
// use with care.
uint8_t DHT20::resetSensor()
{
uint8_t count = 255;
if ((readStatus() & 0x18) != 0x18)
{
count++;
if (_resetRegister(0x1B)) count++;
if (_resetRegister(0x1C)) count++;
if (_resetRegister(0x1E)) count++;
delay(10);
}
return count;
}
////////////////////////////////////////////////
//
// READ THE SENSOR
//
int DHT20::read()
{
// do not read to fast == more than once per second.
if (millis() - _lastRead < 1000)
{
return DHT20_ERROR_LASTREAD;
}
int status = requestData();
if (status < 0) return status;
// wait for measurement ready
uint32_t start = millis();
while (isMeasuring())
{
if (millis() - start >= 1000)
{
return DHT20_ERROR_READ_TIMEOUT;
}
yield();
}
// read the measurement
status = readData();
if (status < 0) return status;
// convert it to meaningful data
return convert();
}
int DHT20::requestData()
{
// reset sensor if needed.
resetSensor();
// GET CONNECTION
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xAC);
_wire->write(0x33);
_wire->write(0x00);
int rv = _wire->endTransmission();
_lastRequest = millis();
return rv;
}
int DHT20::readData()
{
// GET DATA
const uint8_t length = 7;
int bytes = _wire->requestFrom(DHT20_ADDRESS, length);
if (bytes == 0) return DHT20_ERROR_CONNECT;
if (bytes < length) return DHT20_MISSING_BYTES;
bool allZero = true;
for (int i = 0; i < bytes; i++)
{
_bits[i] = _wire->read();
// if (_bits[i] < 0x10) Serial.print(0);
// Serial.print(_bits[i], HEX);
// Serial.print(" ");
allZero = allZero && (_bits[i] == 0);
}
// Serial.println();
if (allZero) return DHT20_ERROR_BYTES_ALL_ZERO;
_lastRead = millis();
return bytes;
}
int DHT20::convert()
{
// CONVERT AND STORE
_status = _bits[0];
uint32_t raw = _bits[1];
raw <<= 8;
raw += _bits[2];
raw <<= 4;
raw += (_bits[3] >> 4);
_humidity = raw * 9.5367431640625e-5; // ==> / 1048576.0 * 100%;
raw = (_bits[3] & 0x0F);
raw <<= 8;
raw += _bits[4];
raw <<= 8;
raw += _bits[5];
_temperature = raw * 1.9073486328125e-4 - 50; // ==> / 1048576.0 * 200 - 50;
// TEST CHECKSUM
uint8_t _crc = _crc8(_bits, 6);
// Serial.print(_crc, HEX);
// Serial.print("\t");
// Serial.println(_bits[6], HEX);
if (_crc != _bits[6]) return DHT20_ERROR_CHECKSUM;
return DHT20_OK;
}
////////////////////////////////////////////////
//
// TEMPERATURE & HUMIDITY & OFFSET
//
float DHT20::getHumidity()
{
return _humidity + _humOffset;
};
float DHT20::getTemperature()
{
return _temperature + _tempOffset;
};
void DHT20::setHumOffset(float offset)
{
_humOffset = offset;
};
void DHT20::setTempOffset(float offset)
{
_tempOffset = offset;
};
float DHT20::getHumOffset()
{
return _humOffset;
};
float DHT20::getTempOffset()
{
return _tempOffset;
};
////////////////////////////////////////////////
//
// STATUS
//
uint8_t DHT20::readStatus()
{
_wire->requestFrom(DHT20_ADDRESS, (uint8_t)1);
delay(1); // needed to stabilize timing
return (uint8_t) _wire->read();
}
bool DHT20::isCalibrated()
{
return (readStatus() & 0x08) == 0x08;
}
bool DHT20::isMeasuring()
{
return (readStatus() & 0x80) == 0x80;
}
bool DHT20::isIdle()
{
return (readStatus() & 0x80) == 0x00;
}
int DHT20::internalStatus()
{
return _status;
};
////////////////////////////////////////////////
//
// TIMING
//
uint32_t DHT20::lastRead()
{
return _lastRead;
};
uint32_t DHT20::lastRequest()
{
return _lastRequest;
};
////////////////////////////////////////////////
//
// PRIVATE
//
uint8_t DHT20::_crc8(uint8_t *ptr, uint8_t len)
{
uint8_t crc = 0xFF;
while(len--)
{
crc ^= *ptr++;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 0x80)
{
crc <<= 1;
crc ^= 0x31;
}
else
{
crc <<= 1;
}
}
}
return crc;
}
// Code based on demo code sent by www.aosong.com
// no further documentation.
// 0x1B returned 18, 0, 4
// 0x1C returned 18, 65, 0
// 0x1E returned 18, 8, 0
// 18 seems to be status register
// other values unknown.
bool DHT20::_resetRegister(uint8_t reg)
{
uint8_t value[3];
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(reg);
_wire->write(0x00);
_wire->write(0x00);
if (_wire->endTransmission() != 0) return false;
delay(5);
int bytes = _wire->requestFrom(DHT20_ADDRESS, (uint8_t)3);
for (int i = 0; i < bytes; i++)
{
value[i] = _wire->read();
// Serial.println(value[i], HEX);
}
delay(10);
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xB0 | reg);
_wire->write(value[1]);
_wire->write(value[2]);
if (_wire->endTransmission() != 0) return false;
delay(5);
return true;
}
// -- END OF FILE --

117
libraries/DHT20/DHT20.h Normal file
View file

@ -0,0 +1,117 @@
#pragma once
//
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
// VERSION: 0.3.1
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "Arduino.h"
#include "Wire.h"
#define DHT20_LIB_VERSION (F("0.3.1"))
#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
#define DHT20_ERROR_CONNECT -11
#define DHT20_MISSING_BYTES -12
#define DHT20_ERROR_BYTES_ALL_ZERO -13
#define DHT20_ERROR_READ_TIMEOUT -14
#define DHT20_ERROR_LASTREAD -15
class DHT20
{
public:
// CONSTRUCTOR
// fixed address 0x38
DHT20(TwoWire *wire = &Wire);
bool begin();
bool isConnected();
uint8_t getAddress();
// ASYNCHRONUOUS CALL
// trigger acquisition.
int requestData();
// read the raw data.
int readData();
// converts raw data bits to temperature and humidity.
int convert();
// SYNCHRONOUS CALL
// blocking read call to read + convert data
int read();
// access the converted temperature & humidity
float getHumidity();
float getTemperature();
// OFFSET 1st order adjustments
void setHumOffset(float offset = 0);
void setTempOffset(float offset = 0);
float getHumOffset();
float getTempOffset();
// READ STATUS
uint8_t readStatus();
// 3 wrapper functions around readStatus()
bool isCalibrated();
bool isMeasuring();
bool isIdle();
// status from last read()
int internalStatus();
// TIMING
uint32_t lastRead();
uint32_t lastRequest();
// RESET (new since 0.1.4)
// use with care
// returns number of registers reset => must be 3
// 3 = OK
// 0,1,2 = error.
// 255 = no reset needed.
// See datasheet 7.4 Sensor Reading Process, point 1
// use with care
uint8_t resetSensor();
private:
float _humidity;
float _temperature;
float _humOffset;
float _tempOffset;
uint8_t _status;
uint32_t _lastRequest;
uint32_t _lastRead;
uint8_t _bits[7];
uint8_t _crc8(uint8_t *ptr, uint8_t len);
// use with care
bool _resetRegister(uint8_t reg);
TwoWire* _wire;
};
// -- END OF FILE --

21
libraries/DHT20/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

310
libraries/DHT20/README.md Normal file
View file

@ -0,0 +1,310 @@
[![Arduino CI](https://github.com/RobTillaart/DHT20/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/DHT20.svg)](https://github.com/RobTillaart/DHT20/issues)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHT20/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DHT20.svg?maxAge=3600)](https://github.com/RobTillaart/DHT20/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/DHT20.svg)](https://registry.platformio.org/libraries/robtillaart/DHT20)
# DHT20
Arduino library for I2C DHT20 temperature and humidity sensor.
## Description
The DHT20 is a humidity and temperature sensor.
The sensor has a fixed address of **0x38**.
It is not known if the address can be changed.
The library must be initiated by calling the **begin()** function.
Thereafter one has to call the **read()** function to do the actual reading,
and call **getTemperature()** and **getHumidity()** to get the measured values.
Calling these latter again will return the same values until a new **read()** is done.
The **read()** call of this sensor is blocking for 80+ milliseconds (datasheet 7.4)
so the library also has a asynchronous interface. See below.
Since 0.1.3 and 0.1.4 the performance of **read()** has been optimized,
still blocking but less long for about 45 milliseconds.
### 0.2.0
In #8 a bug is described that the sensor "freezes".
Cause is not well understood.
Two solutions / workarounds are found:
- call **resetSensor()** before EVERY **read()**.
This is the preferred solution.
- use **Wire.setClock(200000)** 100 K and lower speeds freezes the sensor.
With clock set to 200 K and above the sensor seems to work for longer periods.
Tested several speeds on UNO, no pull ups, < 10 cm wire.
Note: setting the I2C clock possibly interferes with other devices on the I2C bus,
so it is not a solution in the end.
The 0.2.0 version embeds the **resetSensor()** into **requestData()** to
reset the sensor if needed in both synchronous and asynchronous calls.
This keeps the API simple. The reads are 1-2 ms slower than 0.1.4. (< 50 ms).
Still far below the 80 ms mentioned in the datasheet.
#### 0.3.0 Breaking change
Version 0.3.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.
#### Tested
Verified to work with Arduino UNO and ESP32 and ESP8266 (see #8)
Please let me know if other platforms work (or not).
## I2C
#### Address
The sensor has a fixed address of **0x38**.
It is not known if the address can be changed.
#### I2C multiplexing
Sometimes you need to control more devices than possible with the default
address range the device provides.
This is possible with an I2C multiplexer e.g. TCA9548 which creates up
to eight channels (think of it as I2C subnets) which can use the complete
address range of the device.
Drawback of using a multiplexer is that it takes more administration in
your code e.g. which device is on which channel.
This will slow down the access, which must be taken into account when
deciding which devices are on which channel.
Also note that switching between channels will slow down other devices
too if they are behind the multiplexer.
- https://github.com/RobTillaart/TCA9548
#### Connection
Always check datasheet!
Front view
```
+--------------+
VDD ----| 1 |
SDA ----| 2 DHT20 |
GND ----| 3 |
SCL ----| 4 |
+--------------+
```
#### Performance
The datasheet states 400 KHz as the maximum speed.
Below the results of a small test that works well up to 800 KHz.
- Arduino UNO + 10 cm wires + no pull up + DHT20_I2C_speed.ino
Speed in KHz, Time in microseconds.
**read()**
| Speed | Time | notes |
|:-------:|:-------:|:--------|
| 100 | 44588 | default I2C speed |
| 200 | 43988 |
| 400 | 44040 | datasheet maximum |
| 600 | 43224 |
| 800 | 43988 |
**ASYNC: requestData()**
| Speed | Time | notes |
|:-------:|:-------:|:--------|
| 100 | 1676 | default I2C speed |
| 200 | 1384 |
| 400 | 1240 | datasheet maximum |
| 600 | 1188 |
| 800 | 1168 |
**ASYNC: readData()**
| Speed | Time | notes |
|:-------:|:-------:|:--------|
| 100 | 832 | default I2C speed |
| 200 | 464 |
| 400 | 284 | datasheet maximum |
| 600 | 212 |
| 800 | 188 |
The numbers indicate that the conversion takes > 40 milliseconds.
Requesting the measurement and fetching the data < 2.5 milliseconds.
Using the asynchronous interface frees up a lot of clock cycles.
Going beyond 400 KHz (datasheet max) does not save much extra time,
and should only be used if you are in a need for speed.
## Interface
```cpp
#include "DHT20.h"
```
#### Constructor
- **DHT20(TwoWire \*wire = &Wire)** constructor, using a specific Wire (I2C bus).
- **bool begin()** initializer. Returns true if connected.
The user must call **Wire.begin()** before calling this function.
- **bool isConnected()** returns true if the address of the DHT20 can be seen on the I2C bus.
- **uint8_t getAddress()** returns the (fixed) address - convenience.
#### Core
- **int8_t read()** read the sensor and store the values internally.
Returns the status of the read which should be 0 == **DHT20_OK**.
- **float getHumidity()** returns last Humidity read.
Multiple calls will return same value until a new **read()** is made.
- **float getTemperature()** returns last Temperature read.
Multiple calls will return same value until a new **read()** is made.
#### Offset
- **void setHumOffset(float offset = 0)** set an offset to calibrate the sensor (1st order).
Default offset is 0.
- **float getHumOffset()** return current humidity offset, default 0.
- **void setTempOffset(float offset = 0)** set an offset to calibrate the sensor (1st order).
Default offset is 0.
- **float getTempOffset()** return current temperature offset, default 0.
#### Asynchronous interface
There are two timings that need to be considered (from datasheet):
- time between requests = 1000 ms.
- time between request and data ready = 80 ms.
The async interface allows one to continue processing after a **requestData()** has been made.
Note there should be at least **1000 milliseconds** between subsequent requests.
With **bool isMeasuring()** one can check if a new measurement is ready.
Alternative is to delay for up to 80 ms.
If so the sensor can be read with **readData()**.
To interpret the read bits to temperature, humidity and status one needs to call **convert()** as last step.
- **int requestData()** signals the sensor to make a new measurement.
Note there must be at least 1000 milliseconds between requests!
- **int readData()** does the actual reading of the data.
- **int convert()** converts the read bits to temperature and humidity.
See the example **DHT20_async.ino**
#### Status
- **uint8_t readStatus()** forced read of the status only.
This function blocks a few milliseconds to optimize communication.
- **bool isCalibrated()** idem, wrapper around **readStatus()**
- **bool isMeasuring()** idem, wrapper around **readStatus()**
- **bool isIdle()** idem, wrapper around **readStatus()**
- **int internalStatus()** returns the internal status of the sensor. (for debug).
| status bit | meaning |
|:------------:|:---------------------------|
| 7 | 1 = measurement, 0 = idle |
| 6 - 4 | unknown |
| 3 | 1 = calibrated, 0 = not |
| 2 - 0 | unknown |
#### Experimental 0.1.4 resetSensor
Use with care!
- **uint8_t resetSensor()** if at startup the sensor does not return a status of 0x18,
three registers 0x1B, 0x1C and 0x1E need to be reset.
See datasheet 7.4 Sensor Reading Process, point 1.
There is no documentation about the meaning of these registers.
The code is based upon example code for the AHT20 (from manufacturer).
The call is needed to get the **read()** working well so it has been embedded into
the read calls. (0.2.0)
#### Timing
- **uint32_t lastRead()** last time the sensor is read in milliseconds since start.
- **uint32_t lastRequest()** last time a request is made to make a measurement.
#### Return codes
| name | value | notes |
|:-----------------------------|:-------:|:--------|
| DHT20_OK | 00 | OK
| DHT20_ERROR_CHECKSUM | -10 | values might be OK if they are like recent previous ones.
| DHT20_ERROR_CONNECT | -11 | check connection
| DHT20_MISSING_BYTES | -12 | check connection
| DHT20_ERROR_BYTES_ALL_ZERO | -13 | check connection
| DHT20_ERROR_READ_TIMEOUT | -14 |
| DHT20_ERROR_LASTREAD | -15 | wait 1 second between reads
## Future
#### Must
- improve documentation.
- sync AM2315C developments
- see https://github.com/RobTillaart/AM2315C
- investigate the bug from #8 further
(is done in 0.2.1 see issue #8)
#### Should
#### Could
- improve unit tests.
- investigate
- sensor calibration (website aosong?)
- can sensor address be changed?
- investigate optimizing timing in readStatus()
- delay(1) ==> microSeconds(???).
- connected flag?
- keep in sync DHT12 ?
#### Wont
- **void setIgnoreChecksum(bool = false)** ignore checksum flag speeds up communication a bit
- **bool getIgnoreChecksum()** get checksum flag. for completeness.
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,

Binary file not shown.

View file

@ -0,0 +1,97 @@
//
// FILE: DHT20.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
DHT.begin(); // ESP32 default pins 21 22
delay(1000);
}
void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// READ DATA
uint32_t start = micros();
int status = DHT.read();
uint32_t stop = micros();
if ((count % 10) == 0)
{
count = 0;
Serial.println();
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus");
}
count++;
Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Serial.print("\t\t");
Serial.print(stop - start);
Serial.print("\t\t");
switch (status)
{
case DHT20_OK:
Serial.print("OK");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error");
break;
}
Serial.print("\n");
}
}
// -- END OF FILE --

View file

@ -0,0 +1,17 @@
...Arduino\libraries\DHT20\examples\DHT20\DHT20.ino
DHT20 LIBRARY VERSION: 0.1.2
IDE 1.8.19
Arduino UNO
Type Humidity (%) Temp (°C) Time (µs) Status
DHT20 70.8 21.4 86701 OK
DHT20 71.7 21.4 86702 OK
DHT20 69.2 21.4 86701 OK
DHT20 66.4 21.4 86702 OK
DHT20 64.4 21.4 86701 OK
DHT20 62.8 21.4 86701 OK
DHT20 61.7 21.4 86702 OK
DHT20 60.7 21.4 86701 OK
DHT20 60.1 21.4 86702 OK
DHT20 59.6 21.4 86702 OK

View file

@ -0,0 +1,17 @@
...Arduino\libraries\DHT20\examples\DHT20\DHT20.ino
DHT20 LIBRARY VERSION: 0.1.3
IDE 1.8.19
Arduino UNO
Type Humidity (%) Temp (°C) Time (µs) Status
DHT20 61.4 21.5 54684 OK
DHT20 61.4 21.5 54692 OK
DHT20 61.4 21.5 54692 OK
DHT20 61.4 21.5 54688 OK
DHT20 61.4 21.5 54704 OK
DHT20 61.4 21.5 54692 OK
DHT20 61.4 21.5 54692 OK
DHT20 61.4 21.5 46496 OK
DHT20 69.7 22.2 46492 OK
DHT20 79.9 22.3 54688 OK

View file

@ -0,0 +1,18 @@
...Arduino\libraries\DHT20\examples\DHT20\DHT20.ino
DHT20 LIBRARY VERSION: 0.1.4
IDE 1.8.19
Arduino UNO
Type Humidity (%) Temp (°C) Time (µs) Status
DHT20 56.6 21.7 44136 OK
DHT20 56.6 21.7 44136 OK
DHT20 56.7 21.7 44144 OK
DHT20 57.0 21.7 44140 OK
DHT20 57.4 21.7 44136 OK
DHT20 57.7 21.7 44140 OK
DHT20 58.1 21.7 44148 OK
DHT20 58.5 21.7 44140 OK
DHT20 58.7 21.7 44132 OK
DHT20 59.2 21.8 44140 OK

View file

@ -0,0 +1,18 @@
...Arduino\libraries\DHT20\examples\DHT20\DHT20.ino
DHT20 LIBRARY VERSION: 0.2.0
IDE 1.8.19
Arduino UNO
Type Humidity (%) Temp (°C) Time (µs) Status
DHT20 76.2 19.2 45964 OK
DHT20 76.1 19.2 45960 OK
DHT20 75.8 19.2 45956 OK
DHT20 75.6 19.2 45952 OK
DHT20 75.4 19.2 45972 OK
DHT20 75.2 19.2 45976 OK
DHT20 75.1 19.2 45964 OK
DHT20 74.9 19.2 45960 OK
DHT20 74.8 19.2 45956 OK
DHT20 74.6 19.2 45960 OK

View file

@ -0,0 +1,104 @@
//
// FILE: DHT20_I2C_speed.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
// NOTE datasheet states 400 KHz as maximum
#include "DHT20.h"
DHT20 DHT;
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Serial.println("\nNOTE: datasheet states 400 KHz as maximum.\n");
Wire.begin();
DHT.begin(); // ESP32 default pins 21, 22
delay(2000);
for (uint32_t speed = 50000; speed < 850000; speed += 50000)
{
Wire.setClock(speed);
Serial.print(speed);
Serial.print("\t");
Serial.print(DHT.read()); // status
Serial.print("\t");
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t");
Serial.print(DHT.getTemperature(), 1);
Serial.println();
delay(1000);
}
Serial.println();
for (uint32_t speed = 50000; speed < 850000; speed += 50000)
{
Wire.setClock(speed);
start = micros();
DHT.read();
stop = micros();
Serial.print(speed);
Serial.print("\t");
Serial.print(stop - start); // time
Serial.print("\t");
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t");
Serial.print(DHT.getTemperature(), 1);
Serial.println();
delay(1000);
}
Serial.println();
for (uint32_t speed = 50000; speed < 850000; speed += 50000)
{
DHT.requestData();
while (DHT.isMeasuring());
Wire.setClock(speed);
start = micros();
DHT.readData();
stop = micros();
DHT.convert();
Serial.print(speed);
Serial.print("\t");
Serial.print(stop - start); // time
Serial.print("\t");
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t");
Serial.print(DHT.getTemperature(), 1);
Serial.println();
delay(1000);
}
Serial.println("\ndone...");
}
void loop()
{
}
// -- END OF FILE --

View file

@ -0,0 +1,61 @@
Board: UNO
IDE: 1.8.19
DHT20_I2C_speed.ino
DHT20 LIBRARY VERSION: 0.2.2
NOTE: datasheet states 400 KHz as maximum.
50000 0 48.7 17.3
100000 0 48.7 17.3
150000 0 48.7 17.3
200000 0 48.6 17.3
250000 0 48.6 17.3
300000 0 48.6 17.3
350000 0 48.6 17.3
400000 0 48.6 17.3
450000 0 48.6 17.3
500000 0 48.6 17.3
550000 0 48.6 17.3
600000 0 48.6 17.3
650000 0 48.6 17.3
700000 0 48.6 17.3
750000 0 48.6 17.3
800000 0 48.6 17.3
50000 46916 48.6 17.4
100000 44600 48.6 17.4
150000 44264 48.6 17.3
200000 43996 48.6 17.3
250000 44308 48.6 17.3
300000 43624 48.6 17.3
350000 44248 48.6 17.3
400000 44040 48.6 17.3
450000 43724 48.5 17.4
500000 43612 48.6 17.3
550000 43388 48.6 17.4
600000 43204 48.6 17.4
650000 44200 48.6 17.3
700000 44108 48.6 17.4
750000 43984 48.6 17.4
800000 43984 48.5 17.4
50000 1576 48.6 17.3
100000 836 48.6 17.4
150000 588 48.5 17.3
200000 472 48.6 17.4
250000 400 48.5 17.3
300000 336 48.6 17.4
350000 296 48.6 17.4
400000 284 48.6 17.3
450000 252 48.6 17.4
500000 244 48.6 17.3
550000 228 48.6 17.4
600000 216 48.6 17.3
650000 204 48.6 17.3
700000 200 48.5 17.4
750000 188 48.6 17.4
800000 188 48.6 17.4
done...

View file

@ -0,0 +1,69 @@
//
// FILE: DHT20_async.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint32_t counter = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(400000);
DHT.begin(); // ESP32 default 21, 22
delay(2000);
// start with initial request
Serial.println(DHT.requestData());
}
void loop()
{
uint32_t now = millis();
if (now - DHT.lastRead() > 1000)
{
DHT.readData();
DHT.convert();
Serial.print(DHT.getHumidity(), 1);
Serial.print(" %RH \t");
Serial.print(DHT.getTemperature(), 1);
Serial.print(" °C\t");
Serial.print(counter);
Serial.print("\n");
// new request
counter = 0;
DHT.requestData();
}
// other code here
counter++; // dummy counter to show async behaviour
}
// -- END OF FILE --

View file

@ -0,0 +1,174 @@
//
// FILE: DHT20_lcd.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
uint32_t stop, start;
///////////////////////////////////////////////////////////////////////////
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define BL_OFF 0
#define BL_ON 1
/*
#define BACKLIGHT_PIN 7
#define En_pin 4
#define Rw_pin 5
#define Rs_pin 6
#define D4_pin 0
#define D5_pin 1
#define D6_pin 2
#define D7_pin 3
#define LED_OFF 0
#define LED_ON 1
*/
int displayAddress = 0x27;
// LCD object is created as a placeholder as the actual address is determined in setupDisplay()
// constructor needs an I2C address. Better solution would be a function to set the address
// runtime in the class.
LiquidCrystal_I2C lcd(displayAddress);
void setupDisplay()
{
lcd = LiquidCrystal_I2C(displayAddress, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
lcd.begin(20, 4);
// lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(BL_ON);
}
void display()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP:");
lcd.setCursor(6, 0);
lcd.print(DHT.getTemperature());
lcd.setCursor(0, 1);
lcd.print("HUM:");
lcd.setCursor(6, 1);
lcd.print(DHT.getHumidity());
lcd.setCursor(0, 3);
lcd.print("TIME:");
lcd.setCursor(6, 3);
lcd.print(stop - start);
}
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
DHT.begin(); // ESP32 default pins 21 22
setupDisplay();
delay(1000);
}
void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// READ DATA
start = micros();
int status = DHT.read();
stop = micros();
display();
if ((count % 10) == 0)
{
count = 0;
Serial.println();
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus");
}
count++;
Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Serial.print("\t\t");
Serial.print(stop - start);
Serial.print("\t\t");
switch (status)
{
case DHT20_OK:
Serial.print("OK");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error");
break;
}
Serial.print("\n");
}
}
// -- END OF FILE --

View file

@ -0,0 +1,109 @@
//
// FILE: DHT20_offset.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(400000);
DHT.begin(); // ESP32 default pins 21 22
delay(1000);
}
void loop()
{
// set an offset after 10 seconds.
if (millis() > 10000)
{
DHT.setTempOffset(1.7);
DHT.setHumOffset(-2.3);
}
// make a measurement every 2 seconds
if (millis() - DHT.lastRead() >= 2000)
{
// READ DATA
uint32_t start = micros();
int status = DHT.read();
uint32_t stop = micros();
if ((count % 10) == 0)
{
count = 0;
Serial.println();
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus\tOffset");
}
count++;
Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Serial.print("\t\t");
Serial.print(stop - start);
Serial.print("\t\t");
switch (status)
{
case DHT20_OK:
Serial.print("OK");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error");
break;
}
Serial.print("\t");
Serial.print(DHT.getHumOffset());
Serial.print("\t");
Serial.print(DHT.getTempOffset());
Serial.print("\n");
}
}
// -- END OF FILE --

View file

@ -0,0 +1,45 @@
//
// FILE: DHT20_plotter.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT(&Wire);
void setup()
{
Wire.begin();
DHT.begin(); // ESP32 default pins 21 22
Serial.begin(115200);
Serial.println("Humidity, Temperature");
}
void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// note no error checking
DHT.read();
Serial.print(DHT.getHumidity(), 1);
Serial.print(", ");
Serial.println(DHT.getTemperature(), 1);
}
}
// -- END OF FILE --

View file

@ -0,0 +1,49 @@
//
// FILE: DHT20_read_status.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT;
uint8_t count = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(400000);
DHT.begin(); // ESP32 default pins 21 22
delay(2000);
}
void loop()
{
int status = DHT.readStatus();
Serial.println(status);
delay(1000);
}
// -- END OF FILE --

View file

@ -0,0 +1,80 @@
//
// FILE: DHT20_test_esp.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT(&Wire1); // 2nd I2C interface
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire1.begin(12, 13); // select your pin numbers here
delay(2000);
Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT20, \t");
int status = DHT.read();
switch (status)
{
case DHT20_OK:
Serial.print("OK,\t");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes,\t");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print(",\t");
Serial.println(DHT.getTemperature(), 1);
delay(2000);
}
// -- END OF FILE --

View file

@ -0,0 +1,82 @@
//
// FILE: DHT20_test_rp2040.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
// URL: https://github.com/RobTillaart/DHT20
//
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
#include "DHT20.h"
DHT20 DHT(&Wire); // or use 2nd I2C interface &Wire1
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();
Wire1.setSDA(12); // select your pin numbers here
Wire1.setSCL(13); // select your pin numbers here
Wire1.begin();
delay(2000);
Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT20, \t");
int status = DHT.read();
switch (status)
{
case DHT20_OK:
Serial.print("OK,\t");
break;
case DHT20_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHT20_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHT20_MISSING_BYTES:
Serial.print("Missing bytes,\t");
break;
case DHT20_ERROR_BYTES_ALL_ZERO:
Serial.print("All bytes read zero");
break;
case DHT20_ERROR_READ_TIMEOUT:
Serial.print("Read time out");
break;
case DHT20_ERROR_LASTREAD:
Serial.print("Error read too fast");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print(",\t");
Serial.println(DHT.getTemperature(), 1);
delay(2000);
}
// -- END OF FILE --

View file

@ -0,0 +1,47 @@
# Syntax Colouring Map For DHT20
# Data types (KEYWORD1)
DHT20 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
getAddress KEYWORD2
requestData KEYWORD2
readData KEYWORD2
convert KEYWORD2
read KEYWORD2
getHumidity KEYWORD2
getTemperature KEYWORD2
setHumOffset KEYWORD2
setTempOffset KEYWORD2
getHumOffset KEYWORD2
getTempOffset KEYWORD2
readStatus KEYWORD2
isCalibrated KEYWORD2
isMeasuring KEYWORD2
isIdle KEYWORD2
internalStatus KEYWORD2
lastRead KEYWORD2
lastRequest KEYWORD2
resetSensor KEYWORD2
# Constants (LITERAL1)
DHT20_LIB_VERSION LITERAL2
DHT20_OK LITERAL2
DHT20_ERROR_CHECKSUM LITERAL1
DHT20_ERROR_CONNECT LITERAL1
DHT20_MISSING_BYTES LITERAL1
DHT20_ERROR_BYTES_ALL_ZERO LITERAL1
DHT20_ERROR_READ_TIMEOUT LITERAL1
DHT20_ERROR_LASTREAD LITERAL1

View file

@ -0,0 +1,23 @@
{
"name": "DHT20",
"keywords": "Temperature, Humidity, DHT20, I2C",
"description": "Arduino library for I2C DHT20 temperature and humidity sensor. ",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/DHT20.git"
},
"version": "0.3.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
"headers": "DHT20.h"
}

View file

@ -0,0 +1,11 @@
name=DHT20
version=0.3.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for I2C DHT20 temperature and humidity sensor.
paragraph=DHT20
category=Sensors
url=https://github.com/RobTillaart/DHT20
architectures=*
includes=DHT20.h
depends=

View file

@ -0,0 +1,102 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2022-01-11
// PURPOSE: unit tests for the DHT20 library
// https://github.com/RobTillaart/DHT20
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
// ----------------------------
// assertEqual(expected, actual)
// assertNotEqual(expected, actual)
// assertLess(expected, actual)
// assertMore(expected, actual)
// assertLessOrEqual(expected, actual)
// assertMoreOrEqual(expected, actual)
// assertTrue(actual)
// assertFalse(actual)
// assertNull(actual)
// assertNotNull(actual)
#include <ArduinoUnitTests.h>
#include "DHT20.h"
unittest_setup()
{
fprintf(stderr, "DHT20_LIB_VERSION: %s \n", (char *) DHT20_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constants)
{
assertEqual( 0, DHT20_OK);
assertEqual(-10, DHT20_ERROR_CHECKSUM);
assertEqual(-11, DHT20_ERROR_CONNECT);
assertEqual(-12, DHT20_MISSING_BYTES);
assertEqual(-13, DHT20_ERROR_BYTES_ALL_ZERO);
assertEqual(-14, DHT20_ERROR_READ_TIMEOUT);
assertEqual(-15, DHT20_ERROR_LASTREAD);
}
unittest(test_constructor)
{
DHT20 DHT(&Wire);
assertEqualFloat(0, DHT.getTemperature(), 0.001);
assertEqualFloat(0, DHT.getHumidity(), 0.001);
assertEqualFloat(0, DHT.getTempOffset(), 0.001);
assertEqualFloat(0, DHT.getHumOffset(), 0.001);
Wire.begin();
DHT.begin();
assertEqual(DHT20_ERROR_LASTREAD, DHT.read());
// assertEqualFloat(0, DHT.getTemperature(), 0.001);
// assertEqualFloat(0, DHT.getHumidity(), 0.001);
assertEqualFloat(0, DHT.getTempOffset(), 0.001);
assertEqualFloat(0, DHT.getHumOffset(), 0.001);
}
unittest(test_offset)
{
DHT20 DHT(&Wire);
Wire.begin();
DHT.begin();
assertEqualFloat(0, DHT.getTempOffset(), 0.001);
assertEqualFloat(0, DHT.getHumOffset(), 0.001);
for (float offset = -1.5; offset < 1.5; offset += 0.5)
{
DHT.setHumOffset(offset);
DHT.setTempOffset(offset);
assertEqualFloat(offset, DHT.getTempOffset(), 0.001);
assertEqualFloat(offset, DHT.getHumOffset(), 0.001);
assertEqualFloat(offset, DHT.getTemperature(), 0.001);
assertEqualFloat(offset, DHT.getHumidity(), 0.001);
}
}
unittest_main()
// -- END OF FILE --