DHT: Temperature and Humidity Sensor
This Python code demonstrates how to read temperature and humidity data from DHT11 and DHT22 sensors. The Arduino main control board is connected to a Windows or Linux computer, the DHT11 sensor is connected to D6, and the DHT22 sensor is connected to D7 of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Read dht temperature and humidity sensor
# Connection: Connect an Arduino main control board to a Windows or Linux computer, connect dht11 to D6, and dht22 to D7
import time
from pinpong.board import Board,Pin,DHT11,DHT22 # Import dht library
Board("uno").begin() # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin() # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin() # Initialization by specifying the port under Mac
dht11 = DHT11(Pin(Pin.D6))
dht22 = DHT22(Pin(Pin.D7))
while True:
temp = dht11.temp_c() # Read Celsius temperature
humi = dht11.humidity() # Read humidity
print("dht11 temperature=",temp," humidity=",humi)
temp = dht22.temp_c() # Read Celsius temperature
humi = dht22.humidity() # Read humidity
print("dht22 temperature=",temp," humidity=",humi)
time.sleep(1)