Skip to content

Commit b456484

Browse files
mcauserpfalcon
authored andcommitted
docs: Add DHT to ESP8266 Quick Ref and Tutorial
1 parent 0e4cae5 commit b456484

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

docs/esp8266/quickref.rst

+18
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ For low-level driving of an APA102::
291291
import esp
292292
esp.apa102_write(clock_pin, data_pin, rgbi_buf)
293293

294+
DHT driver
295+
----------
296+
297+
The DHT driver is implemented in software and works on all pins::
298+
299+
import dht
300+
import machine
301+
302+
d = dht.DHT11(machine.Pin(4))
303+
d.measure()
304+
d.temperature() # eg. 23 (°C)
305+
d.humidity() # eg. 41 (% RH)
306+
307+
d = dht.DHT22(machine.Pin(4))
308+
d.measure()
309+
d.temperature() # eg. 23.6 (°C)
310+
d.humidity() # eg. 41.3 (% RH)
311+
294312
WebREPL (web browser interactive prompt)
295313
----------------------------------------
296314

docs/esp8266/tutorial/dht.rst

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.

docs/esp8266/tutorial/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ to `<https://www.python.org>`__.
2929
powerctrl.rst
3030
onewire.rst
3131
neopixel.rst
32+
dht.rst
3233
nextsteps.rst

0 commit comments

Comments
 (0)