我的是三隻腳版本:
正極(PIN1 3.3V)
負極(PIN9 接地)
Data(中間那條線的接PIN7 - GPIO4)
libgpiod2這個在程式執行過程中,需要用到先裝
sudo apt-get install libgpiod2
sudo pip3 install adafruit-circuitpython-dht
測試
>>> import adafruit_dht
>>> import board
>>> dht22 = adafruit_dht.DHT22(board.D4)
>>> dht22.temperature
>>> dht22.humidity
撰寫程式將以下存成DHT22.py
import time
import adafruit_dht
import board
dht22 = adafruit_dht.DHT22(board.D4)
while True:
try:
temperature = dht22.temperature
humidity = dht22.humidity
print("Temperature: {:.1f} *C \t Humidity: {}%".format(temperature, humidity))
except RuntimeError as e:
print("Reading from DHT22 ERROR: ", e.args)
time.sleep(2)
執行
沒有留言:
張貼留言