connect dht 11 sensor to raspberry pi 3b error

问题描述 投票:0回答:0

我正在尝试将我的树莓派 3B 连接到我的 DHT11 传感器,并在输入以下代码时不断收到此错误代码:

我已经使用以下代码导入了 adafruit 库:sudo pip3 install Adafruit_DHT

enter image description here

我试过的代码:

# Import required libraries
import Adafruit_DHT
import time

# Set sensor type and pin number
sensor = Adafruit_DHT.DHT11
pin = 4

# Loop to read data from sensor
while True:
    # Try to grab a sensor reading
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

    # Skip to the next loop iteration if no reading was taken
    if humidity is None or temperature is None:
        continue

    # Print the temperature and humidity
    print('Temperature: {0:0.1f} C'.format(temperature))
    print('Humidity: {0:0.1f} %'.format(humidity))

    # Wait a few seconds before repeating the loop
    time.sleep(2)


python raspberry-pi3 sensors adafruit
© www.soinside.com 2019 - 2024. All rights reserved.