Library for DHT11 DHT22 AM2302 and similar sensors. Now with value after decimal point for DHT22.
Dependents: Seeed_Grove_Temp_Humidity_Example DHT11_Temp_Humidity IoT_W5500_MQTT
Fork of DHT by
Revision 5:cce2105f7365, committed 2017-01-07
- Comitter:
- Sly_fox
- Date:
- Sat Jan 07 22:26:26 2017 +0000
- Parent:
- 4:0667c68ef5e0
- Commit message:
- Added comments on calculation of temperature and humidity for DHT22
Changed in this revision
DHT.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0667c68ef5e0 -r cce2105f7365 DHT.cpp --- a/DHT.cpp Sat Jan 07 21:58:20 2017 +0000 +++ b/DHT.cpp Sat Jan 07 22:26:26 2017 +0000 @@ -77,7 +77,7 @@ // start the transfer DHT_io.output(); DHT_io = 0; - wait_ms(18); + wait_ms(18); // for DHT22 minimum 800µs but for DHT11 etc. 18ms are required DHT_io = 1; wait_us(30); DHT_io.input(); @@ -133,6 +133,10 @@ } + + + + float DHT::CalcTemperature() { float v; @@ -142,11 +146,15 @@ v = DHT_data[2]; return float(v); case DHT22: - v = DHT_data[2] & 0x7F; + // to calculate temperture of DHT22 in Celsius = (DHT_data[2]*256 + DHT_data[3]) / 10 + // if first bit of DHT_data[2] is 1 then the temperature is negative. + + + v = DHT_data[2] & 0x7F; // mask of the bit for negative temperture v *= 256; v += DHT_data[3]; v /= 10; - if (DHT_data[2] & 0x80) + if (DHT_data[2] & 0x80) // if first bit of DHT_data[2] is 1 then the temperature is negative. v *= -1; return float(v); } @@ -213,6 +221,7 @@ v = DHT_data[0]; return float(v); case DHT22: + // to calculate humidity of DHT22 = (DHT_data[0]*256 + DHT_data[1]) / 10 v = DHT_data[0]; v *= 256; v += DHT_data[1];