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要数据。