Simple library for the DHT11 temperature and humidity sensor. Forked from an existing Mbed DHT11 project.
Simple DHT11 temperature and humidity library.
Example usage
#include "mbed.h" #include "DHT11.h" DHT11 dht(D8); // Change pin name here if required main() { printf("T:%d, H:%d\r\n", dht.readTemperature(), dht.readHumidity()); }
The sensor may be read as often as desired, but temperature and humidity values are cached and will only be updated if they are more than 2 seconds old. This is the underlying sensor update rate.
Please note that this project has been modified only enough to make it work for its intended purpose. Various parts of this project still need work, and the source code should not be seen as an example of best practice.
Diff: DHT11.cpp
- Revision:
- 8:160047ca45bf
- Parent:
- 7:50f5c8efd967
- Child:
- 9:056d1e9b428c
--- a/DHT11.cpp Wed Sep 10 17:48:35 2014 +0000 +++ b/DHT11.cpp Wed Sep 10 18:00:31 2014 +0000 @@ -36,13 +36,13 @@ return BUS_BUSY; } - // Sending start signal + // Sending start signal, low signal for 20 ms io.output(); t.reset(); t.start(); do { io = 0; - } while (t.read_ms() < 18); + } while (t.read_ms() < 20); io = 1; io.input(); @@ -72,7 +72,6 @@ do {} while (io == 1); // Starting the pulse width sensing - io_irq.enable_irq(); do { @@ -89,8 +88,6 @@ + ((data & 0x0000ff0000) >> 16) + ((data & 0x000000ff00) >> 8); if (chksum != (data & 0x00000000ff)) { - io.input(); - io = 1; return CHKSUM_ERR; } else { t.reset(); @@ -148,10 +145,10 @@ // Disabling the interruptions io_irq.disable_irq(); - // Reading the length of + // Reading the positive pulse width t_pulse_us = t.read_us(); - // Detecting 0 if the pulse width ranges from 26 us to 28 us + // Detecting 0 if the pulse width ranges from 20 us to 30 us if (20 <= t_pulse_us && t_pulse_us <= 30) { // Shifting the data buffer and not adding 1 (because this bit is zero) data = data << 1; @@ -160,7 +157,7 @@ cnt++; } - // Detecting 1 if the pulse width ranges from 68 us to 72 us + // Detecting 1 if the pulse width ranges from 60 us to 80 us else if (60 <= t_pulse_us && t_pulse_us <= 80) { // Shifting the data buffer and adding 1 (because this bit is one) data = data << 1;