HOW to Detect Temperature and Humidity with ESP32
Description
This DHT11 temperature and humidity sensor is a composite sensor which contains a calibrated digital signal output of the temperature and humidity.
DHT11 temperature and humidity sensor uses the acquisition technology of the digital module and temperature and humidity sensing technology, ensuring high reliability and excellent long-term stability. It includes a resistive element and a NTC temperature measuring device.
Working Principle
The communication and synchronization between the single-chip microcomputer and XHT11 adopts the single bus data format. The communication time is about 4ms. The data is divided into fractional part and integer part.
Operation process: A complete data transmission is 40bit, high bit first out. Data format: 8bit humidity integer data + 8bit humidity decimal data + 8bit temperature integer data + 8bit temperature decimal data + 8bit checksum
8-bit checksum: 8-bit humidity integer data + 8-bit humidity decimal data + 8-bit temperature integer data + 8-bit temperature decimal data “Add the last 8 bits of the result.
Required Components
|
|
|
|
|
---|---|---|---|---|
ESP32Board*1 |
ESP32 Expansion Board*1 |
XHT11 Temperature and |
3P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
# Import machine, time and dht modules.
import machine
import time
import dht
#Associate DHT11 with Pin(15).
DHT = dht.DHT11(machine.Pin(15))
# Obtain temperature and humidity data once per second and print them out.
while True:
DHT.measure() # Start DHT11 to measure data once.
# Call the built-in function of DHT to obtain temperature
# and humidity data and print them in “Shell”.
print('temperature:',DHT.temperature(),'℃','humidity:',DHT.humidity(),'%')
time.sleep_ms(1000)
Test Result
Connect the wires according to the experimental wiring diagram and power on. Click “Run current script”, the code starts executing, the shell displays the temperature and humidity data of the current environment, as shown below. Press“Ctrl+C”or click
“Stop/Restart backend”to exit the program.