# Raspberry Pi + DHT22/DHT11 使用方法 ``` 最後更新時間 * 2024/12/23: 新建立 ``` ## 硬體環境 本教學相容樹莓派所有型號,包括 * [Raspberry Pi 5 Model B](https://piepie.com.tw/product/raspberry-pi-5) * [Raspberry Pi 4 Model B](https://www.piepie.com.tw/28040/raspberry-pi-4-model-b) * [Raspberry Pi 3B+](https://www.piepie.com.tw/19429/57) * [Raspberry Pi 3A+](https://piepie.com.tw/product/raspberry-pi-3-model-a-plus) * [Raspberry Pi 3B](https://www.piepie.com.tw/10684/55) * [Raspberry Pi 2B](https://www.piepie.com.tw/4063/54) * [Raspberry Pi Zero W](https://www.piepie.com.tw/15425/raspberry-pi-zero-w-joins-family) * [Raspberry Pi Zero](https://www.piepie.com.tw/9975/raspberry-pi-zero) * [Raspberry Pi Zero 2 W](https://piepie.com.tw/product/raspberry-pi-zero-2-w)。 ## 系統環境 本教學測試的環境為 * [raspios_arm64-2024-11-19/bookworm](https://downloads.raspberrypi.org/raspios_arm64/images/raspios_arm64-2024-11-19/) ## 感測器 * DHT11 溫濕度感測器 * [DHT22 溫濕度感測器](https://piepie.com.tw/product/dht22-temperature-and-humidity-sensor) - [英文規格書(DHT22)](http://go.aws/2TpVTQQ) ## 接線(實際接線需要配合感測器上的標示!) ![dht22_bb](https://hackmd.io/_uploads/rJ-v4kOBye.png) ## 安裝步驟 ``` cd ~ sudo apt-get update sudo apt-get install -y pigpiod libgpiod2 python3 -m venv venv --system-site-packages source ~/venv/bin/activate pip3 install adafruit-circuitpython-dht ``` ## 範例程式 ### test_dht22.py ``` # test_dht22.py # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import adafruit_dht # Initial the dht device, with data pin connected to: dhtDevice = adafruit_dht.DHT22(board.D18) # you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. # This may be necessary on a Linux single board computer like the Raspberry Pi, # but it will not work in CircuitPython. # dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False) try: while True: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity print( "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( temperature_f, temperature_c, humidity ) ) time.sleep(2.0) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) except KeyboardInterrupt as error: print(error) except Exception as error: print(error) finally: dhtDevice.exit() ``` ### test_dht11.py ``` # test_dht11.py # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import adafruit_dht # Initial the dht device, with data pin connected to: dhtDevice = adafruit_dht.DHT11(board.D18) # you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. # This may be necessary on a Linux single board computer like the Raspberry Pi, # but it will not work in CircuitPython. # dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False) try: while True: # Print the values to the serial port temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity print( "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( temperature_f, temperature_c, humidity ) ) time.sleep(2.0) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) except KeyboardInterrupt as error: print(error) except Exception as error: print(error) finally: dhtDevice.exit() ``` ## 執行程式 ### 在終端機上執行 ``` python test_dht22.py ``` ![hackmd_dhtxx_15-python-test-dht22](https://hackmd.io/_uploads/rygA4y_rke.png) ### 在 Thonny 上執行(需要先切換到虛擬環境) 執行 Thonny。 ![hackmd_dhtxx_20-launch-thonny](https://hackmd.io/_uploads/rkLf7yDSkl.png) 如果在 Thonny 執行 test_dht22.py 出現錯誤訊息,是因為在 Thonny 沒有指到虛擬環境! ![hackmd_dhtxx_25-thonny-execute-dht-error](https://hackmd.io/_uploads/ryYzXyDrJe.png) 在 Thonny 指到正確虛擬環境的作法是,按右上角的 "switch to regular mode" ![hackmd_dhtxx_30-thonny-switch-regular-mode](https://hackmd.io/_uploads/SyymXJwBkl.png) 在 "Run" 選擇 "Configure interpreter" ![hackmd_dhtxx_35-thonny-run-interpreter](https://hackmd.io/_uploads/Sk2QQJPr1x.png) 點選 "Python excutable" ![hackmd_dhtxx_40-thonny-original-interpreter](https://hackmd.io/_uploads/BybVQ1DSye.png) 選擇之前建立的 "/home/pi/venv/bin/python3" ![hackmd_dhtxx_45-thonny-virtual-environment-interpreter](https://hackmd.io/_uploads/HyINmkvHJg.png) 完成後可以看到右下角的 Local Python3 指到 /home/pi/venv/bin/python3 ![hackmd_dhtxx_50-thonny-interpreter-changed](https://hackmd.io/_uploads/HyUCrJOr1e.png) 再次執行 test_dht22.py 就可以了~ ![hackmd_dhtxx_55-thonny-execute-dht-ok](https://hackmd.io/_uploads/H1v0HJ_Skg.png) 如果要中斷程式,請使用 `Run` > `Interrupt execution` ![hackmd_dhtxx_75-thonny-interrupt-dht](https://hackmd.io/_uploads/rkb5nXKIJe.png) 再次執行 test_dht22.py~ ![hackmd_dhtxx_80-thonny-execute-dht-again-ok](https://hackmd.io/_uploads/SkZj37KLkx.png) ## 參考資料 * [DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup) * [Using Virtual Environments in Thonny on a Raspberry Pi](https://core-electronics.com.au/guides/using-virtual-environments-in-thonny-on-a-raspberry-pi/) ###### tags: `教學`, `DHT22`, `DHT11`, `感測器`, `sensor`