Skip to content

Commit d3cff61

Browse files
authored
fix #100, negative representation (#101)
- fix #100, different negative algorithm support - bump version
1 parent 01f233c commit d3cff61

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.0] - 2024-11-22
10+
- fix #100, different negative algorithm support
11+
- time to bump version
12+
13+
----
14+
915
## [0.4.21] - 2024-10-09
1016
- add dhtnew_pulse_diag_ext.ino for extended diagnosis.
1117

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ Feedback (both positive and negative) about the AM232X sensors is welcome.
5757
**Note: check the datasheet how to connect!**
5858

5959

60+
### 0.5.0 Negative temperature
61+
62+
Apparently there are DHT22's which use another representation for negative temperatures.
63+
Since 0.5.0 the library automatically detects which representation is used by the sensor
64+
and chooses the correct algorithm to decode the negative temperature.
65+
66+
See issue #100 (solution) and #57 and #52 before.
67+
See also https://arduino.stackexchange.com/questions/86448/dht22-sensor-reading-code-interprets-negative-values-weirdly
68+
69+
6070
### Related
6171

6272
- https://github.com/RobTillaart/DHTNew
6373
- https://github.com/RobTillaart/DHTStable
6474
- https://github.com/RobTillaart/DHT_Simulator
6575
- https://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html (interesting)
66-
- https://github.com/RobTillaart/Temperature (conversions, dewPoint, heatindex etc)
76+
- https://github.com/RobTillaart/Temperature (conversions, dewPoint, heat index etc.)
6777

6878

6979
## DHT PIN layout from left to right

dhtnew.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: dhtnew.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.4.21
4+
// VERSION: 0.5.0
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: https://github.com/RobTillaart/DHTNEW
77
//
@@ -193,17 +193,29 @@ int DHTNEW::_read()
193193
else // DHT22, DHT33, DHT44, compatible + Si7021
194194
{
195195
_humidity = (_bits[0] * 256 + _bits[1]) * 0.1;
196-
int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]);
197-
if (t == 0)
196+
197+
// positive temperature?
198+
if ((_bits[2] & 0x80) != 0x80 )
198199
{
199-
_temperature = 0.0; // prevent -0.0;
200+
int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]);
201+
if (t == 0)
202+
{
203+
_temperature = 0.0; // prevent -0.0;
204+
}
205+
_temperature = t * 0.1;
200206
}
201-
else
207+
else // negative temperature
202208
{
203-
_temperature = t * 0.1;
204-
if((_bits[2] & 0x80) == 0x80 )
209+
// See issue #100 - 2 different representations
210+
if ((_bits[2] & 0x40) != 0x40 )
211+
{
212+
int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]);
213+
_temperature = t * -0.1;
214+
}
215+
else
205216
{
206-
_temperature = -_temperature;
217+
int16_t t = (_bits[2] << 8) + _bits[3]; // 16 bits, as raw int
218+
_temperature = t * 0.1;
207219
}
208220
}
209221
}

dhtnew.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: dhtnew.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.4.21
5+
// VERSION: 0.5.0
66
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
77
// URL: https://github.com/RobTillaart/DHTNEW
88
//
@@ -18,7 +18,7 @@
1818
#include "Arduino.h"
1919

2020

21-
#define DHTNEW_LIB_VERSION (F("0.4.21"))
21+
#define DHTNEW_LIB_VERSION (F("0.5.0"))
2222

2323

2424
#define DHTLIB_OK 0

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/RobTillaart/DHTNEW.git"
1515
},
16-
"version": "0.4.21",
16+
"version": "0.5.0",
1717
"license": "MIT",
1818
"frameworks": "*",
1919
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=DHTNEW
2-
version=0.4.21
2+
version=0.5.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for DHT temperature and humidity sensor, with automatic sensortype recognition.

0 commit comments

Comments
 (0)