GPIO - 測量溫濕度

材料準備

  • AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1

  • DHT11 or DHT22 or DHT21

範例説明

DHT11結合溫度與濕度的傳感器,工作電壓在3.3V~5V,在常溫下可量測濕度20% ~ 90%RH,精準度±5%RH,溫度可量測0 ~ 50℃,精準度±2 ℃。

Another choice of temperature and humidity sensor is DHT22 sensor, which has better precision. Its measurable range of the humidity is 0%~100%RH with ±5%RH precision, the measurable range of the temperature is -40~125 ℃ with ±0.2℃ precision. There are 4 pins on the sensor:

../../../../_images/image150.png

其中有一隻腳目前沒有作用,所以市面上也看的到重新包裝成3隻腳的版本:

../../../../_images/image247.png

上電之後,平常DHT都處於睡眠模式,要從DHT取得溫濕度,需要以下步驟:

  1. 喚醒DHT:Ameba將DATA pin 這根GPIO toggle low,此時DATA GPIO對AMEBA來說是digital out

  2. 等待DHT回應:DHT也將DATA pin這根GPIO toggle low,此時DATA GPIO對AMEBA來說是digital in

  3. DHT將溫濕度資料送出:DHT將5 bytes的溫濕度資料送出,此時DATA GPIO對AMEBA來說是digital in。 DHT會將5 bytes = 40 bits,以每個bit的方式送出,每個bit的表示方式是,DHT會先將DATA GPIO pull low 一段時間,再pull high,如果pull high的時間小於pull low的時間就是bit 0, 如果pull high的時間大於pull high的時間就是bi​​t 1

../../../../_images/image332.png

AMB21 / AMB22 接線的方式如下

../../../../_images/image424.png

AMB23 接線的方式如下

../../../../_images/image4-11.png

BW16 接線的方式如下

../../../../_images/image4-31.png

BW16-TypeC Wiring Diagram:

../../../../_images/image4-41.png

Open the sample code in “Files” “Examples” “AmebaGPIO” “DHT_Tester”. Compile and upload to Ameba, then press the reset button. The result would be shown on the Serial Monitor.

../../../../_images/image517.png

程式碼説明

dht.readHumidity() 可以讀取濕度,dht.readTemperature()可以讀取溫度。

每次讀值的時候,都直接拿現有的數據使用。如果發現暫存的溫濕度數據已經兩秒沒更新,才會主動向DHT要數據。