Skip to content

Commit ebd147b

Browse files
devsaurusmarcelstoer
authored andcommitted
DHT11 read sometimes failed with checksum error (#2679)
* DHT11 read sometimes failed with checksum error. The code assumed DHT11 devices only ever return zero in the temperature and humidity decimal fraction bytes. The datasheet doesn't guarantee this is the case, and by observation I have noticed that indeed the DHT11 may sometimes return another number, usually close to zero. This means that the code would fail with a checksum error, as the fraction bytes were not included when the checksum was calculated. These bytes are now taken into account and also returned as part of the measurement. This also means that the related dht.read() function is non-functional. If you have a DHT11 device that returns a non-zero decimal part, dht.read() will interpret it as a DHT22 result and return the wrong measurement. For this reason dht.read() should be retired. This patch does not address this issue.
1 parent f801bf1 commit ebd147b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

app/dht/dht.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int dht_read11(uint8_t pin)
156156

157157
// TEST CHECKSUM
158158
// dht_bytes[1] && dht_bytes[3] both 0
159-
uint8_t sum = dht_bytes[0] + dht_bytes[2];
159+
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
160160
if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM;
161161

162162
return DHTLIB_OK;

0 commit comments

Comments
 (0)