|
| 1 | +Temperature and Humidity |
| 2 | +======================== |
| 3 | + |
| 4 | +DHT (Digital Humidity & Temperature) sensors are low cost digital sensors with |
| 5 | +capacitive humidity sensors and thermistors to measure the surrounding air. |
| 6 | +They feature a chip that handles analog to digital conversion and provide a |
| 7 | +1-wire interface. Newer sensors additionally provide an I2C interface. |
| 8 | + |
| 9 | +The DHT11 (blue) and DHT22 (white) sensors provide the same 1-wire interface, |
| 10 | +however, the DHT22 requires a separate object as it has more complex |
| 11 | +calculation. DHT22 have 1 decimal place resolution for both humidity and |
| 12 | +temperature readings. DHT11 have whole number for both. |
| 13 | + |
| 14 | +A custom 1-wire protocol, which is different to Dallas 1-wire, is used to get |
| 15 | +the measurements from the sensor. The payload consists of a humidity value, |
| 16 | +a temperature value and a checksum. |
| 17 | + |
| 18 | +To use the 1-wire interface, construct the objects referring to their data pin:: |
| 19 | + |
| 20 | + >>> import dht |
| 21 | + >>> import machine |
| 22 | + >>> d = dht.DHT11(machine.Pin(4)) |
| 23 | + |
| 24 | + >>> import dht |
| 25 | + >>> import machine |
| 26 | + >>> d = dht.DHT22(machine.Pin(4)) |
| 27 | + |
| 28 | +Then measure and read their values with:: |
| 29 | + |
| 30 | + >>> d.measure() |
| 31 | + >>> d.temperature() |
| 32 | + >>> d.humidity() |
| 33 | + |
| 34 | +Values returned from ``temperature()`` are in degrees Celsius and values |
| 35 | +returned from ``humidity()`` are a percentage of relative humidity. |
| 36 | + |
| 37 | +The DHT11 can be called no more than once per second and the DHT22 once every |
| 38 | +two seconds for most accurate results. Sensor accuracy will degrade over time. |
| 39 | +Each sensor supports a different operating range. Refer to the product |
| 40 | +datasheets for specifics. |
| 41 | + |
| 42 | +In 1-wire mode, only three of the four pins are used and in I2C mode, all four |
| 43 | +pins are used. Older sensors may still have 4 pins even though they do not |
| 44 | +support I2C. The 3rd pin is simply not connected. |
| 45 | + |
| 46 | +Pin configurations: |
| 47 | + |
| 48 | +Sensor without I2C in 1-wire mode (eg. DHT11, DHT22, AM2301, AM2302): |
| 49 | + |
| 50 | + 1=VDD, 2=Data, 3=NC, 4=GND |
| 51 | + |
| 52 | +Sensor with I2C in 1-wire mode (eg. DHT12, AM2320, AM2321, AM2322): |
| 53 | + |
| 54 | + 1=VDD, 2=Data, 3=GND, 4=GND |
| 55 | + |
| 56 | +Sensor with I2C in I2C mode (eg. DHT12, AM2320, AM2321, AM2322): |
| 57 | + |
| 58 | + 1=VDD, 2=SDA, 3=GND, 4=SCL |
| 59 | + |
| 60 | +You should use pull-up resistors for the Data, SDA and SCL pins. |
| 61 | + |
| 62 | +To make newer I2C sensors work in backwards compatible 1-wire mode, you must |
| 63 | +connect both pins 3 and 4 to GND. This disables the I2C interface. |
| 64 | + |
| 65 | +DHT22 sensors are now sold under the name AM2302 and are otherwise identical. |
0 commit comments