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:
其中有一只脚目前没有作用,所以市面上也看的到重新包装成3只脚的版本:
上电之后,平常DHT都处于睡眠模式,要从DHT取得温湿度,需要以下步骤:
唤醒DHT:Ameba将DATA pin 这根GPIO toggle low,此时DATA GPIO对AMEBA来说是digital out
等待DHT回应:DHT也将DATA pin这根GPIO toggle low,此时DATA GPIO对AMEBA来说是digital in
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的时间就是bit 1
AMB21 / AMB22 接线图:
AMB23 接线图:
BW16 接线图:
BW16-TypeC Wiring Diagram:
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.
程式码说明
dht.readHumidity()
可以读取湿度,dht.readTemperature()
可以读取温度。
每次读值的时候,都直接拿现有的数据使用。如果发现暂存的温湿度数据已经两秒没更新,才会主动向DHT要数据。