Reading Temperature and Humidity with DHT11 ------------------------------------------- The DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It applies dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure the product has extremely high reliability and excellent long-term stability. The sensor includes a resistive humidity sensing element and an NTC temperature measuring element, and is connected to a high-performance 8-bit microcontroller. Therefore, this product has the advantages of excellent quality, ultra-fast response, strong anti-interference ability, and extremely high cost performance. .. image:: /_static/products/mpython-s3/_static/image/classic/dht11.png :scale: 50 % :align: center Connecting the DHT11 digital temperature and humidity sensor to the mPython Board requires the mPython expansion board. In the mPython expansion board, the pins available for the DHT11 are P0/1/8/9/13/14/15/16; here we use pin 0. Plug the mPython Board into the mPython expansion board, and connect the DHT11 to the expansion board with a female-to-female jumper wire. The "+" on the DHT11 connects to the power port "V" of the expansion board, the "-" connects to the ground port "G" of the expansion board, and the "out" connects to pin "0" of the expansion board. .. image:: /_static/products/mpython-s3/_static/image/classic/dhtconnect.jpg :scale: 60 % :align: center Example: Display the temperature and humidity read by the DHT11 :: from mpython import * from dht import DHT11 dht=DHT11(Pin(Pin.P0)) while True: dht.measure() oled.fill(0) oled.DispChar("Temp:",0,10) oled.text("%d" % (dht.temperature()), 48, 14) oled.DispChar("Humi:",0,35) oled.text("%d" % (dht.humidity()), 48, 40) oled.show() sleep_ms(100) .. image:: /_static/products/mpython-s3/_static/image/classic/dhtexample.jpg :scale: 60 % :align: center Before use, import the mpython module and the DHT11 class:: from mpython import * from dht import DHT11 Instantiate the DHT11 class and set the mPython pin P0:: dht=DHT11(Pin(Pin.P0)) The DHT11 measures and returns temperature and humidity data:: dht.measure() dht.temperature() dht.humidity() .. Note:: ``dht.measure()`` is the DHT11 command to measure temperature and humidity data. After measuring, use ``dht.temperature()`` and ``dht.humidity()`` to obtain the measured temperature and humidity values.