DHT22 是一款温度与湿度传感器,Arduino 下使用 DHT22 推荐使用这个库
https://github.com/adafruit/DHT-sensor-library
板子连线如图,数据引脚连接 Arduino 的 D2
完整代码:
#include "DHT.h" DHT dht(2, DHT22); void setup() { Serial.begin(9600); dht.begin(); } void loop() { delay(2000); float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); Serial.print("Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.print(" "); Serial.print("Temperature: "); Serial.print(temperature); Serial.print("*C"); Serial.println(); }
串口输出:
Humidity: 27.90% Temperature: 24.90*C
Humidity: 27.90% Temperature: 25.00*C
Humidity: 27.90% Temperature: 24.90*C