請鍵入下列程式: #include "DHT.h" #include <LiquidCrystal_I2C.h> #define Addr 0x27 LiquidCrystal_I2C LCD(Addr,16,2); DHT dht(2, DHT11);//第 2 之腳接收, 使用 DHT11 模式 void setup() { dht.begin(); LCD.begin(); } void loop(){ delay(2000); //讀取溫度或濕度大約需要250毫秒! float h = dht.readHumidity();//讀取濕度 float t = dht.readTemperature();//讀取攝氏溫度 float f = dht.readTemperature(true);//讀取華氏溫度 if (isnan(h) || isnan(t) || isnan(f)) { // nan 表示 not a number , 表示讀取失敗 LCD.clear(); LCD.setCursor(0,0);LCD.print("DHT sensor"); LCD.setCursor(0,1);LCD.print("read error!"); return; } LCD.setCursor(0,0);LCD.print("H:");LCD.print(h);LCD.print(" %"); LCD.setCursor(0,1);LCD.print("T:");LCD.print(t,1);//輸出小數一位 LCD.print(char(0xdf));LCD.print("C,"); LCD.print(f,1);LCD.print(char(0xdf));LCD.print("F"); } |