DHT11
DHT11
Only three pins are available for use: VCC, GND, and DATA. The
communication process begins with the DATA line sending start signals to
DHT11, and DHT11 receives the signals and returns an answer signal. Then
the host receives the answer signal and begins to receive 40-bit humiture
data (8-bit humidity integer + 8-bit humidity decimal + 8-bit temperature
integer + 8-bit temperature decimal + 8-bit checksum).
Features
Humidity and temperature are closely related from the physical quantity
itself to the actual people’s life. The temperature and humidity of human
environment will directly affect the thermoregulatory function and heat
transfer effect of human body. It will further affect the thinking activity and
mental state, thus affecting the efficiency of our study and work.
Humidity is the concentration of water vapor present in the air. The relative
humidity of air is commonly used in life and is expressed in %RH. Relative
humidity is closely related to temperature. For a certain volume of sealed
gas, the higher the temperature, the lower the relative humidity, and the
lower the temperature, the higher the relative humidity.
A basic digital temperature and humidity sensor, the DHT11, uses a capacitive
humidity sensor and thermistor to measure the surrounding air and outputs a
digital signal on the data pins (no analog input pins are required).
1 Raspberry Pi Pico W 1
3 Breadboard 1
Schematic
from machine import Pin, I2C
import utime as time
from dht import DHT11, InvalidPulseCount
while True:
try:
sensor.measure()
string = "Temperature:{}\nHumidity: {}".format(sensor.temperature, sensor.humidity)
print(string)
time.sleep(4)
except InvalidPulseCount as e:
print('Bad pulse count - retrying ...')
After the code is run, you will see the Shell continuously print out the temperature and humidity, and as
the program runs steadily, these two values will become more and more accurate.
How it works?
Initialize the DHT11 object. This device only needs a digital input to be used.
Use sensor.measure() to read the current temperature and humidity, which will
be stored in sensor.temperature , sensor.humidity . They are then printed out.
Finally the DHT11 sampling rate is 1HZ, a time.sleep(1) is needed in the loop.
while True:
try:
sensor.measure()
string = "Temperature:{}\nHumidity: {}".format(sensor.temperature, sensor.humidity)
print(string)
time.sleep(4)
except InvalidPulseCount as e:
print('Bad pulse count - retrying ...')