Skip to content

Commit

Permalink
fix #8 resetSensor() (#9)
Browse files Browse the repository at this point in the history
* fix #8 resetSensor()
* update readme.md
  • Loading branch information
RobTillaart authored Nov 1, 2022
1 parent 2fc2c11 commit bf043d8
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 43 deletions.
19 changes: 18 additions & 1 deletion .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:

packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
Expand All @@ -8,4 +23,6 @@ compile:
- m4
- esp32
# - esp8266
# - mega2560
# - mega2560
- rpipico

44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.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)

26 changes: 7 additions & 19 deletions DHT20.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
//
// FILE: DHT20.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
//
// HISTORY:
// 0.1.0 2022-01-11 initial version (based upon DHT20 datasheet)
// 0.1.1 2022-09-10 add hardware schema to readme.md.
// fix async interface (first version)
// 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.3 2022-09-17 add wrapper status functions
// improve performance read()
// refactor, update readme.md
// 0.1.4 2022-09-18 add resetSensor() code.
// add comments in .h file
// add examples
// stabilize readStatus()
// update readme.md
// HISTORY: see changelog.md


#include "DHT20.h"
Expand Down Expand Up @@ -117,13 +102,16 @@ int DHT20::read()
status = readData();
if (status < 0) return status;

// convert it to meaningfull data
// convert it to meaningful data
return convert();
}


int DHT20::requestData()
{
// reset sensor if needed.
resetSensor();

// GET CONNECTION
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xAC);
Expand Down Expand Up @@ -331,7 +319,7 @@ bool DHT20::_resetRegister(uint8_t reg)
if (_wire->endTransmission() != 0) return false;
delay(5);

int bytes = _wire->requestFrom(DHT20_ADDRESS, 3);
int bytes = _wire->requestFrom(DHT20_ADDRESS, (uint8_t)3);
for (int i = 0; i < bytes; i++)
{
value[i] = _wire->read();
Expand Down
4 changes: 2 additions & 2 deletions DHT20.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
// VERSION: 0.1.4
// VERSION: 0.2.0
// HISTORY: See DHT20.cpp
// URL: https://github.com/RobTillaart/DHT20
//
Expand All @@ -21,7 +21,7 @@
#include "Arduino.h"
#include "Wire.h"

#define DHT20_LIB_VERSION (F("0.1.4"))
#define DHT20_LIB_VERSION (F("0.2.0"))

#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
Expand Down
66 changes: 49 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ Arduino library for I2C DHT20 temperature and humidity sensor.

## Description

The DHT20 is a humidity an 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,
or **begin(dataPin, clockPin)** for **ESP32** and similar platforms.

Thereafter one has to call the **read()** function to do the actual reading,
and with **getTemperature()** and **getHumidity()** to get the measured values.
Calling these latter again will return the same values until a new **read()** is called.
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.
Expand All @@ -27,9 +32,30 @@ 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.


### Connection

Always check datasheet
Always check datasheet!

Front view
```
Expand All @@ -43,7 +69,7 @@ Front view

### Tested

Verified to work with Arduino UNO and ESP32.
Verified to work with Arduino UNO and ESP32 and ESP8266 (see #8)
Please let me know if other platforms work (or not).


Expand All @@ -61,29 +87,32 @@ Please let me know if other platforms work (or not).
### Core

- **int8_t read()** read the sensor and store the values internally.
It returns the status of the read which should be 0.
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)** set an offset to calibrate (1st order) the sensor.
- **float getHumOffset()** return current offset, default 0.
- **void setTempOffset(float offset)** set an offset to calibrate (1st order) the sensor.
- **float getTempOffset()** return current offset, default 0.
- **void setHumOffset(float offset)** set an offset to calibrate the sensor (1st order).
- **float getHumOffset()** return current humidity offset, default 0.
- **void setTempOffset(float offset)** set an offset to calibrate the sensor (1st order).
- **float getTempOffset()** return current temperature offset, default 0.


### Asynchronous interface

There are two timings that need to be considdered,
- time between requests = 1000 ms
- time between request and data ready = 80 ms
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 that there should be at least **1000 milliseconds** between subsequent requests.
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.
Expand Down Expand Up @@ -116,14 +145,17 @@ This function blocks a few milliseconds to optimize communication.

#### Experimental 0.1.4 resetSensor

Use with care, as this is not tested.
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

Expand Down Expand Up @@ -152,20 +184,20 @@ See examples
## Future

#### must

- improve documentation.
- investigate the bug from #8 further

#### should


#### could

- improve unit tests.
- investigate
- sensor calibration (website aosong?)
- investigate optimizing timing in readStatus()
- delay(1) ==> microSeconds(???).
- separate changelog.md
- connected flag?
- add **uint8_t getAddress()** to return the address (convenience).

#### won't

Expand Down
2 changes: 0 additions & 2 deletions examples/DHT20/DHT20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ void setup()
{
DHT.begin(); // ESP32 default pins 21 22

Wire.setClock(400000);

Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Expand Down
18 changes: 18 additions & 0 deletions examples/DHT20/performance_0.2.0.txt
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHT20.git"
},
"version": "0.1.4",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DHT20
version=0.1.4
version=0.2.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for I2C DHT20 temperature and humidity sensor.
Expand Down

0 comments on commit bf043d8

Please sign in to comment.